Search in sources :

Example 11 with PlaceBlockEvent

use of com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method handleBlockRightClick.

/**
 * Handle the right click of a block while an item is held.
 *
 * @param event the original event
 * @param cause the list of cause
 * @param item the item
 * @param placed the placed block
 * @param <T> the event type
 */
private static <T extends Event & Cancellable> void handleBlockRightClick(T event, Cause cause, @Nullable ItemStack item, Block clicked, Block placed) {
    if (item != null && item.getType() == Material.TNT) {
        // Workaround for a bug that allowed TNT to trigger instantly if placed
        // next to redstone, without plugins getting the clicked place event
        // (not sure if this actually still happens) -- note Jun 2019 - happens with dispensers still, tho not players
        Events.fireToCancel(event, new UseBlockEvent(event, cause, clicked.getLocation(), Material.TNT));
        // Workaround for http://leaky.bukkit.org/issues/1034
        Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), Material.TNT));
        return;
    }
    // Handle created Minecarts
    if (item != null && Materials.isMinecart(item.getType())) {
        EntityType entityType = Materials.getRelatedEntity(item.getType());
        if (entityType == null) {
            entityType = EntityType.MINECART;
        }
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, clicked.getLocation().add(0.5, 0, 0.5), entityType));
        return;
    }
    // Handle created boats
    if (item != null && Materials.isBoat(item.getType())) {
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.BOAT));
        return;
    }
    // Handle created armor stands
    if (item != null && item.getType() == Material.ARMOR_STAND) {
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.ARMOR_STAND));
        return;
    }
    if (item != null && item.getType() == Material.END_CRYSTAL) {
        /*&& placed.getType() == Material.BEDROCK) {*/
        // in vanilla you can only place them on bedrock but who knows what plugins will add
        // may be overprotective as a result, but better than being underprotective
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.ENDER_CRYSTAL));
        return;
    }
    // Handle created spawn eggs
    if (item != null && Materials.isSpawnEgg(item.getType())) {
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), Materials.getEntitySpawnEgg(item.getType())));
        return;
    }
    // handle water/lava placement
    if (item != null && (item.getType() == Material.WATER_BUCKET || item.getType() == Material.LAVA_BUCKET)) {
        Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), item.getType() == Material.WATER_BUCKET ? Material.WATER : Material.LAVA));
        return;
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) SpawnEntityEvent(com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)

Example 12 with PlaceBlockEvent

use of com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onBlockPistonExtend.

@EventHandler(ignoreCancelled = true)
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
    EventDebounce.Entry entry = pistonExtendDebounce.getIfNotPresent(new BlockPistonExtendKey(event), event);
    if (entry != null) {
        Cause cause = create(event.getBlock());
        List<Block> blocks = new ArrayList<>(event.getBlocks());
        int originalLength = blocks.size();
        Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, cause, event.getBlock().getWorld(), blocks, Material.AIR));
        if (originalLength != blocks.size()) {
            event.setCancelled(true);
            return;
        }
        BlockFace dir = event.getDirection();
        for (int i = 0; i < blocks.size(); i++) {
            Block existing = blocks.get(i);
            if (existing.getPistonMoveReaction() == PistonMoveReaction.MOVE || existing.getPistonMoveReaction() == PistonMoveReaction.PUSH_ONLY || existing.getType() == Material.PISTON || existing.getType() == Material.STICKY_PISTON) {
                blocks.set(i, existing.getRelative(dir));
            }
        }
        Events.fireBulkEventToCancel(event, new PlaceBlockEvent(event, cause, event.getBlock().getWorld(), blocks, Material.STONE));
        if (blocks.size() != originalLength) {
            event.setCancelled(true);
        }
        entry.setCancelled(event.isCancelled());
        if (event.isCancelled()) {
            playDenyEffect(event.getBlock().getLocation().add(0.5, 1, 0.5));
        }
    }
}
Also used : InventoryMoveItemEventDebounce(com.sk89q.worldguard.bukkit.listener.debounce.legacy.InventoryMoveItemEventDebounce) EventDebounce(com.sk89q.worldguard.bukkit.listener.debounce.EventDebounce) EntityEntityEventDebounce(com.sk89q.worldguard.bukkit.listener.debounce.legacy.EntityEntityEventDebounce) BlockEntityEventDebounce(com.sk89q.worldguard.bukkit.listener.debounce.legacy.BlockEntityEventDebounce) BlockFace(org.bukkit.block.BlockFace) Cause(com.sk89q.worldguard.bukkit.cause.Cause) Entry(com.sk89q.worldguard.bukkit.listener.debounce.legacy.AbstractEventDebounce.Entry) BlockPistonExtendKey(com.sk89q.worldguard.bukkit.listener.debounce.BlockPistonExtendKey) ArrayList(java.util.ArrayList) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 13 with PlaceBlockEvent

use of com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onPlayerBucketEmpty.

@EventHandler(ignoreCancelled = true)
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
    Player player = event.getPlayer();
    Block blockClicked = event.getBlockClicked();
    Block blockAffected;
    if (blockClicked.getBlockData() instanceof Waterlogged) {
        blockAffected = blockClicked;
    } else {
        blockAffected = blockClicked.getRelative(event.getBlockFace());
    }
    boolean allowed = false;
    // Milk buckets can't be emptied as of writing
    if (event.getBucket() == Material.MILK_BUCKET) {
        allowed = true;
    }
    ItemStack item = new ItemStack(event.getBucket(), 1);
    Material blockMaterial = Materials.getBucketBlockMaterial(event.getBucket());
    Events.fireToCancel(event, new PlaceBlockEvent(event, create(player), blockAffected.getLocation(), blockMaterial).setAllowed(allowed));
    Events.fireToCancel(event, new UseItemEvent(event, create(player), player.getWorld(), item).setAllowed(allowed));
    if (event.isCancelled()) {
        playDenyEffect(event.getPlayer(), blockAffected.getLocation().add(0.5, 0.5, 0.5));
    }
}
Also used : Player(org.bukkit.entity.Player) UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) Waterlogged(org.bukkit.block.data.Waterlogged) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 14 with PlaceBlockEvent

use of com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onBlockMultiPlace.

@EventHandler(ignoreCancelled = true)
public void onBlockMultiPlace(BlockMultiPlaceEvent event) {
    List<Block> placed = event.getReplacedBlockStates().stream().map(BlockState::getBlock).collect(Collectors.toList());
    int origAmt = placed.size();
    PlaceBlockEvent delegateEvent = new PlaceBlockEvent(event, create(event.getPlayer()), event.getBlock().getWorld(), placed, event.getBlockPlaced().getType());
    Events.fireToCancel(event, delegateEvent);
    if (origAmt != placed.size()) {
        event.setCancelled(true);
    }
}
Also used : FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Aggregations

PlaceBlockEvent (com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent)14 EventHandler (org.bukkit.event.EventHandler)13 Block (org.bukkit.block.Block)10 Cause (com.sk89q.worldguard.bukkit.cause.Cause)9 FallingBlock (org.bukkit.entity.FallingBlock)9 BreakBlockEvent (com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent)6 Player (org.bukkit.entity.Player)5 UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)4 Material (org.bukkit.Material)4 ItemStack (org.bukkit.inventory.ItemStack)4 UseBlockEvent (com.sk89q.worldguard.bukkit.event.block.UseBlockEvent)3 SpawnEntityEvent (com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)3 ArrayList (java.util.ArrayList)3 DelegateEvent (com.sk89q.worldguard.bukkit.event.DelegateEvent)2 EventDebounce (com.sk89q.worldguard.bukkit.listener.debounce.EventDebounce)2 Entry (com.sk89q.worldguard.bukkit.listener.debounce.legacy.AbstractEventDebounce.Entry)2 BlockEntityEventDebounce (com.sk89q.worldguard.bukkit.listener.debounce.legacy.BlockEntityEventDebounce)2 EntityEntityEventDebounce (com.sk89q.worldguard.bukkit.listener.debounce.legacy.EntityEntityEventDebounce)2 InventoryMoveItemEventDebounce (com.sk89q.worldguard.bukkit.listener.debounce.legacy.InventoryMoveItemEventDebounce)2 WorldConfiguration (com.sk89q.worldguard.config.WorldConfiguration)2