Search in sources :

Example 1 with UseBlockEvent

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

the class EventAbstractionListener method onPlayerInteract.

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    @Nullable ItemStack item = event.getItem();
    Block clicked = event.getClickedBlock();
    Block placed;
    boolean modifiesWorld;
    Cause cause = create(player);
    switch(event.getAction()) {
        case PHYSICAL:
            if (event.useInteractedBlock() != Result.DENY) {
                if (clicked.getType() == Material.FARMLAND || clicked.getType() == Material.TURTLE_EGG) {
                    BreakBlockEvent breakDelagate = new BreakBlockEvent(event, cause, clicked);
                    breakDelagate.setSilent(true);
                    breakDelagate.getRelevantFlags().add(Flags.TRAMPLE_BLOCKS);
                    boolean denied;
                    if (!(denied = Events.fireToCancel(event, breakDelagate))) {
                        PlaceBlockEvent placeDelegate = new PlaceBlockEvent(event, cause, clicked.getLocation(), clicked.getType() == Material.FARMLAND ? Material.DIRT : clicked.getType());
                        placeDelegate.setSilent(true);
                        placeDelegate.getRelevantFlags().add(Flags.TRAMPLE_BLOCKS);
                        denied = Events.fireToCancel(event, placeDelegate);
                    }
                    if (denied) {
                        playDenyEffect(player, clicked.getLocation());
                    }
                    return;
                }
                DelegateEvent firedEvent = new UseBlockEvent(event, cause, clicked).setAllowed(hasInteractBypass(clicked));
                if (Tag.REDSTONE_ORES.isTagged(clicked.getType())) {
                    firedEvent.setSilent(true);
                }
                if (clicked.getType() == Material.BIG_DRIPLEAF) {
                    firedEvent.getRelevantFlags().add(Flags.USE_DRIPLEAF);
                    firedEvent.setSilent(true);
                }
                interactDebounce.debounce(clicked, event.getPlayer(), event, firedEvent);
                if (event.useInteractedBlock() == Result.DENY) {
                    playDenyEffect(player, clicked.getLocation().add(0, 1, 0));
                }
            }
            break;
        case RIGHT_CLICK_BLOCK:
            if (event.useInteractedBlock() != Result.DENY) {
                placed = clicked.getRelative(event.getBlockFace());
                // Re-used for dispensers
                handleBlockRightClick(event, create(event.getPlayer()), item, clicked, placed);
            }
        case LEFT_CLICK_BLOCK:
            if (event.useInteractedBlock() != Result.DENY) {
                placed = clicked.getRelative(event.getBlockFace());
                // Only fire events for blocks that are modified when right clicked
                final boolean hasItemInteraction = item != null && isItemAppliedToBlock(item, clicked) && event.getAction() == Action.RIGHT_CLICK_BLOCK;
                modifiesWorld = hasItemInteraction || isBlockModifiedOnClick(clicked, event.getAction() == Action.RIGHT_CLICK_BLOCK);
                if (Events.fireAndTestCancel(new UseBlockEvent(event, cause, clicked).setAllowed(!modifiesWorld))) {
                    event.setUseInteractedBlock(Result.DENY);
                }
                // Handle connected blocks (i.e. beds, chests)
                for (Block connected : Blocks.getConnected(clicked)) {
                    if (Events.fireAndTestCancel(new UseBlockEvent(event, create(event.getPlayer()), connected).setAllowed(!modifiesWorld))) {
                        event.setUseInteractedBlock(Result.DENY);
                        break;
                    }
                }
                if (hasItemInteraction) {
                    if (Events.fireAndTestCancel(new PlaceBlockEvent(event, cause, clicked.getLocation(), clicked.getType()))) {
                        event.setUseItemInHand(Result.DENY);
                        event.setUseInteractedBlock(Result.DENY);
                    }
                }
                // Special handling of putting out fires
                if (event.getAction() == Action.LEFT_CLICK_BLOCK && Materials.isFire(placed.getType())) {
                    if (Events.fireAndTestCancel(new BreakBlockEvent(event, create(event.getPlayer()), placed))) {
                        event.setUseInteractedBlock(Result.DENY);
                        break;
                    }
                }
                if (event.isCancelled()) {
                    playDenyEffect(event.getPlayer(), clicked.getLocation().add(0.5, 1, 0.5));
                }
            }
        case LEFT_CLICK_AIR:
        case RIGHT_CLICK_AIR:
            if (event.useItemInHand() != Result.DENY) {
                if (item != null && !item.getType().isBlock() && Events.fireAndTestCancel(new UseItemEvent(event, cause, player.getWorld(), item))) {
                    event.setUseItemInHand(Result.DENY);
                }
            }
            // throw events
            if (item != null && getWorldConfig(player.getWorld()).blockUseAtFeet.test(item)) {
                if (Events.fireAndTestCancel(new UseBlockEvent(event, cause, player.getLocation().getBlock()))) {
                    event.setUseInteractedBlock(Result.DENY);
                }
            }
            break;
    }
}
Also used : Player(org.bukkit.entity.Player) DelegateEvent(com.sk89q.worldguard.bukkit.event.DelegateEvent) UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) Cause(com.sk89q.worldguard.bukkit.cause.Cause) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) Nullable(javax.annotation.Nullable) EventHandler(org.bukkit.event.EventHandler)

Example 2 with UseBlockEvent

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

the class EventAbstractionListener method onTakeLecternBook.

@EventHandler(ignoreCancelled = true)
public void onTakeLecternBook(PlayerTakeLecternBookEvent event) {
    final UseBlockEvent useEvent = new UseBlockEvent(event, create(event.getPlayer()), event.getLectern().getBlock());
    Events.fireToCancel(event, useEvent);
}
Also used : UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 3 with UseBlockEvent

use of com.sk89q.worldguard.bukkit.event.block.UseBlockEvent 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 4 with UseBlockEvent

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

the class RegionProtectionListener method onUseBlock.

@EventHandler(ignoreCancelled = true)
public void onUseBlock(final UseBlockEvent event) {
    // Don't care about events that have been pre-allowed
    if (event.getResult() == Result.ALLOW)
        return;
    // Region support disabled
    if (!isRegionSupportEnabled(event.getWorld()))
        return;
    // Whitelisted cause
    if (isWhitelisted(event.getCause(), event.getWorld(), false))
        return;
    final Material type = event.getEffectiveMaterial();
    final RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    final RegionAssociable associable = createRegionAssociable(event.getCause());
    event.filter((Predicate<Location>) target -> {
        boolean canUse;
        String what;
        if (Materials.isConsideredBuildingIfUsed(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event));
            what = "use that";
        } else if (Materials.isInventoryBlock(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.CHEST_ACCESS));
            what = "open that";
        } else if (handleAsInventoryUsage(event.getOriginalEvent())) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.CHEST_ACCESS));
            what = "take that";
        } else if (Materials.isAnvil(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.USE_ANVIL));
            what = "use that";
        } else if (Materials.isBed(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.SLEEP));
            what = "sleep";
        } else if (type == Material.RESPAWN_ANCHOR) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.RESPAWN_ANCHORS));
            what = "use anchors";
        } else if (type == Material.TNT) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.TNT));
            what = "use explosives";
        } else if (Materials.isUseFlagApplicable(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.USE));
            what = "use that";
        } else {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT));
            what = "use that";
        }
        if (!canUse) {
            tellErrorMessage(event, event.getCause(), target, what);
            return false;
        }
        return true;
    });
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Arrays(java.util.Arrays) Item(org.bukkit.entity.Item) Cause(com.sk89q.worldguard.bukkit.cause.Cause) Materials(com.sk89q.worldguard.bukkit.util.Materials) Event(org.bukkit.event.Event) InteropUtils(com.sk89q.worldguard.bukkit.util.InteropUtils) Player(org.bukkit.entity.Player) WGMetadata(com.sk89q.worldguard.bukkit.internal.WGMetadata) CommandUtils(com.sk89q.worldguard.commands.CommandUtils) ArrayList(java.util.ArrayList) EventHandler(org.bukkit.event.EventHandler) Block(org.bukkit.block.Block) Location(org.bukkit.Location) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) ItemFrame(org.bukkit.entity.ItemFrame) World(org.bukkit.World) WorldGuard(com.sk89q.worldguard.WorldGuard) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) Material(org.bukkit.Material) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) Entities(com.sk89q.worldguard.bukkit.util.Entities) RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Entity(org.bukkit.entity.Entity) State(com.sk89q.worldguard.protection.flags.StateFlag.State) DestroyEntityEvent(com.sk89q.worldguard.bukkit.event.entity.DestroyEntityEvent) WorldGuardPlugin(com.sk89q.worldguard.bukkit.WorldGuardPlugin) Flags(com.sk89q.worldguard.protection.flags.Flags) LocalPlayer(com.sk89q.worldguard.LocalPlayer) Events(com.sk89q.worldguard.bukkit.util.Events) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) EntityType(org.bukkit.entity.EntityType) Result(org.bukkit.event.Event.Result) PlayerTakeLecternBookEvent(org.bukkit.event.player.PlayerTakeLecternBookEvent) DelegateEvent(com.sk89q.worldguard.bukkit.event.DelegateEvent) DamageEntityEvent(com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent) List(java.util.List) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) ExperienceOrb(org.bukkit.entity.ExperienceOrb) Predicate(com.google.common.base.Predicate) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) BukkitAdapter(com.sk89q.worldedit.bukkit.BukkitAdapter) DisallowedPVPEvent(com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent) VehicleExitEvent(org.bukkit.event.vehicle.VehicleExitEvent) ChatColor(org.bukkit.ChatColor) SpawnEntityEvent(com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent) Tameable(org.bukkit.entity.Tameable) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) Material(org.bukkit.Material) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 5 with UseBlockEvent

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

the class EventAbstractionListener method handleInventoryHolderUse.

private static <T extends Event & Cancellable> void handleInventoryHolderUse(T originalEvent, Cause cause, InventoryHolder holder) {
    if (originalEvent.isCancelled()) {
        return;
    }
    if (holder instanceof Entity) {
        Entity entity = (Entity) holder;
        Material mat = Materials.getRelatedMaterial((entity).getType());
        UseEntityEvent useEntityEvent = new UseEntityEvent(originalEvent, cause, entity);
        if (mat != null && hasInteractBypass((entity).getWorld(), mat)) {
            useEntityEvent.setAllowed(true);
        }
        Events.fireToCancel(originalEvent, useEntityEvent);
    } else {
        if (holder instanceof BlockState) {
            final BlockState block = (BlockState) holder;
            final UseBlockEvent useBlockEvent = new UseBlockEvent(originalEvent, cause, block.getBlock());
            if (hasInteractBypass(block.getWorld(), block.getType())) {
                useBlockEvent.setAllowed(true);
            }
            Events.fireToCancel(originalEvent, useBlockEvent);
        } else if (holder instanceof DoubleChest) {
            InventoryHolder left = ((DoubleChest) holder).getLeftSide();
            InventoryHolder right = ((DoubleChest) holder).getRightSide();
            if (left instanceof Chest) {
                Events.fireToCancel(originalEvent, new UseBlockEvent(originalEvent, cause, ((Chest) left).getBlock()));
            }
            if (right instanceof Chest) {
                Events.fireToCancel(originalEvent, new UseBlockEvent(originalEvent, cause, ((Chest) right).getBlock()));
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) DoubleChest(org.bukkit.block.DoubleChest) Chest(org.bukkit.block.Chest) BlockState(org.bukkit.block.BlockState) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) Material(org.bukkit.Material) DoubleChest(org.bukkit.block.DoubleChest) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Aggregations

UseBlockEvent (com.sk89q.worldguard.bukkit.event.block.UseBlockEvent)5 PlaceBlockEvent (com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent)3 Cause (com.sk89q.worldguard.bukkit.cause.Cause)2 DelegateEvent (com.sk89q.worldguard.bukkit.event.DelegateEvent)2 BreakBlockEvent (com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent)2 SpawnEntityEvent (com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)2 UseEntityEvent (com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent)2 Material (org.bukkit.Material)2 Block (org.bukkit.block.Block)2 Entity (org.bukkit.entity.Entity)2 EntityType (org.bukkit.entity.EntityType)2 EventHandler (org.bukkit.event.EventHandler)2 Predicate (com.google.common.base.Predicate)1 BukkitAdapter (com.sk89q.worldedit.bukkit.BukkitAdapter)1 LocalPlayer (com.sk89q.worldguard.LocalPlayer)1 WorldGuard (com.sk89q.worldguard.WorldGuard)1 WorldGuardPlugin (com.sk89q.worldguard.bukkit.WorldGuardPlugin)1 DamageEntityEvent (com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent)1 DestroyEntityEvent (com.sk89q.worldguard.bukkit.event.entity.DestroyEntityEvent)1 UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)1