Search in sources :

Example 1 with AllowPriorityOverride

use of dev.frankheijden.insights.api.annotations.AllowPriorityOverride in project Insights by InsightsPlugin.

the class BlockListener method onBlockPlace.

/**
 * Handles the BlockPlaceEvent for players.
 * Chunk limitations are applied in here on the lowest (first) event priority.
 */
@AllowPriorityOverride
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    var block = event.getBlock();
    var location = block.getLocation();
    var material = block.getType();
    var player = event.getPlayer();
    // In case of BlockMultiPlaceEvent, we may need to take a different delta.
    // A closing tripwire hook triggers a BlockMultiPlaceEvent for all attached strings,
    // but since they already exist in the world we do not need to count them.
    var delta = 0;
    if (event instanceof BlockMultiPlaceEvent && material != Material.TRIPWIRE_HOOK) {
        List<BlockState> replacedBlockStates = ((BlockMultiPlaceEvent) event).getReplacedBlockStates();
        for (BlockState state : replacedBlockStates) {
            if (BlockUtils.isSameChunk(location.getBlockX(), location.getBlockZ(), state.getX(), state.getZ())) {
                delta++;
            }
        }
    } else {
        delta = 1;
    }
    if (handleAddition(player, location, ScanObject.of(material), delta, false)) {
        event.setCancelled(true);
    }
}
Also used : BlockMultiPlaceEvent(org.bukkit.event.block.BlockMultiPlaceEvent) BlockState(org.bukkit.block.BlockState) AllowPriorityOverride(dev.frankheijden.insights.api.annotations.AllowPriorityOverride) EventHandler(org.bukkit.event.EventHandler)

Aggregations

AllowPriorityOverride (dev.frankheijden.insights.api.annotations.AllowPriorityOverride)1 BlockState (org.bukkit.block.BlockState)1 EventHandler (org.bukkit.event.EventHandler)1 BlockMultiPlaceEvent (org.bukkit.event.block.BlockMultiPlaceEvent)1