Search in sources :

Example 1 with BlockMultiPlaceEvent

use of org.bukkit.event.block.BlockMultiPlaceEvent in project Arclight by IzzelAliz.

the class BlockPlaceEventDispatcher method onMultiPlace.

@SubscribeEvent(receiveCanceled = true)
public void onMultiPlace(BlockEvent.EntityMultiPlaceEvent event) {
    Entity entity = event.getEntity();
    if (entity instanceof ServerPlayerEntity) {
        ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity;
        Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity);
        Direction direction = ArclightCaptures.getPlaceEventDirection();
        if (direction != null) {
            Hand hand = ArclightCaptures.getPlaceEventHand(Hand.MAIN_HAND);
            List<BlockState> placedBlocks = new ArrayList<>(event.getReplacedBlockSnapshots().size());
            for (BlockSnapshot snapshot : event.getReplacedBlockSnapshots()) {
                placedBlocks.add(ArclightBlockSnapshot.fromBlockSnapshot(snapshot, true).getState());
            }
            CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().offset(direction.getOpposite()));
            ItemStack bukkitStack;
            if (hand == Hand.MAIN_HAND) {
                bukkitStack = player.getInventory().getItemInMainHand();
            } else {
                bukkitStack = player.getInventory().getItemInOffHand();
            }
            BlockPlaceEvent placeEvent = new BlockMultiPlaceEvent(placedBlocks, againstBlock, bukkitStack, player, !event.isCanceled());
            placeEvent.setCancelled(event.isCanceled());
            Bukkit.getPluginManager().callEvent(placeEvent);
            event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild());
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Player(org.bukkit.entity.Player) BlockMultiPlaceEvent(org.bukkit.event.block.BlockMultiPlaceEvent) BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent) ArrayList(java.util.ArrayList) ArclightBlockSnapshot(io.izzel.arclight.common.mod.util.ArclightBlockSnapshot) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) CraftBlock(org.bukkit.craftbukkit.v.block.CraftBlock) CraftServer(org.bukkit.craftbukkit.v.CraftServer) Direction(net.minecraft.util.Direction) Hand(net.minecraft.util.Hand) BlockState(org.bukkit.block.BlockState) ItemStack(org.bukkit.inventory.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with BlockMultiPlaceEvent

use of org.bukkit.event.block.BlockMultiPlaceEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onBlockPlace.

@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    if (event instanceof BlockMultiPlaceEvent)
        return;
    BlockState previousState = event.getBlockReplacedState();
    // Some blocks, like tall grass and fire, get replaced
    if (previousState.getType() != Material.AIR && previousState.getType() != event.getBlockReplacedState().getType()) {
        Events.fireToCancel(event, new BreakBlockEvent(event, create(event.getPlayer()), previousState.getLocation(), previousState.getType()));
    }
    if (!event.isCancelled()) {
        ItemStack itemStack = new ItemStack(event.getBlockPlaced().getType(), 1);
        Events.fireToCancel(event, new UseItemEvent(event, create(event.getPlayer()), event.getPlayer().getWorld(), itemStack));
    }
    if (!event.isCancelled()) {
        Events.fireToCancel(event, new PlaceBlockEvent(event, create(event.getPlayer()), event.getBlock()));
    }
    if (event.isCancelled()) {
        playDenyEffect(event.getPlayer(), event.getBlockPlaced().getLocation().add(0.5, 0.5, 0.5));
    }
}
Also used : BlockMultiPlaceEvent(org.bukkit.event.block.BlockMultiPlaceEvent) BlockState(org.bukkit.block.BlockState) UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) ItemStack(org.bukkit.inventory.ItemStack) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 3 with BlockMultiPlaceEvent

use of org.bukkit.event.block.BlockMultiPlaceEvent in project Insights by InsightsPlugin.

the class BlockListener method onBlockPlaceMonitor.

/**
 * Handles the BlockPlaceEvent for players, monitoring changes
 * Chunk limitations are not applied here, they are only monitored and displayed to the player.
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlaceMonitor(BlockPlaceEvent event) {
    var block = event.getBlock();
    var location = block.getLocation();
    var material = block.getType();
    var player = event.getPlayer();
    // but since they already exist in the world we do not need to count them.
    if (event instanceof BlockMultiPlaceEvent && material != Material.TRIPWIRE_HOOK) {
        List<BlockState> replacedBlockStates = ((BlockMultiPlaceEvent) event).getReplacedBlockStates();
        for (BlockState state : replacedBlockStates) {
            var loc = state.getLocation();
            handleModification(loc, state.getType(), material, 1);
        }
    } else {
        handleModification(location, event.getBlockReplacedState().getType(), material, 1);
    }
    // No need to add any delta, cache is already updated
    evaluateAddition(player, location, ScanObject.of(material), 0);
}
Also used : BlockMultiPlaceEvent(org.bukkit.event.block.BlockMultiPlaceEvent) BlockState(org.bukkit.block.BlockState) EventHandler(org.bukkit.event.EventHandler)

Example 4 with BlockMultiPlaceEvent

use of org.bukkit.event.block.BlockMultiPlaceEvent 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)

Example 5 with BlockMultiPlaceEvent

use of org.bukkit.event.block.BlockMultiPlaceEvent in project McRPG by Eunoians.

the class WorldListener method onBlockPlace.

/**
 * Monitor BlockPlace events.
 *
 * @param event The event to watch
 */
@EventHandler(priority = EventPriority.MONITOR)
public void onBlockPlace(BlockPlaceEvent event) {
    BlockState blockState = event.getBlock().getState();
    Block block = blockState.getBlock();
    World world = block.getWorld();
    /* WORLD BLACKLIST CHECK */
    if (McRPG.getInstance().getConfig().contains("Configuration.DisabledWorlds") && McRPG.getInstance().getConfig().getStringList("Configuration.DisabledWorlds").contains(world.getName())) {
        return;
    }
    if (isWithinWorldBounds(block)) {
        // NOTE: BlockMultiPlace has its own logic so don't handle anything that would overlap
        if (!(event instanceof BlockMultiPlaceEvent)) {
            McRPG.getPlaceStore().setTrue(blockState);
        }
    }
}
Also used : BlockMultiPlaceEvent(org.bukkit.event.block.BlockMultiPlaceEvent) BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) World(org.bukkit.World) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BlockState (org.bukkit.block.BlockState)5 BlockMultiPlaceEvent (org.bukkit.event.block.BlockMultiPlaceEvent)5 EventHandler (org.bukkit.event.EventHandler)4 ItemStack (org.bukkit.inventory.ItemStack)2 BreakBlockEvent (com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent)1 PlaceBlockEvent (com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent)1 UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)1 AllowPriorityOverride (dev.frankheijden.insights.api.annotations.AllowPriorityOverride)1 ArclightBlockSnapshot (io.izzel.arclight.common.mod.util.ArclightBlockSnapshot)1 ArrayList (java.util.ArrayList)1 Entity (net.minecraft.entity.Entity)1 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)1 Direction (net.minecraft.util.Direction)1 Hand (net.minecraft.util.Hand)1 BlockSnapshot (net.minecraftforge.common.util.BlockSnapshot)1 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)1 World (org.bukkit.World)1 Block (org.bukkit.block.Block)1 CraftServer (org.bukkit.craftbukkit.v.CraftServer)1 CraftBlock (org.bukkit.craftbukkit.v.block.CraftBlock)1