Search in sources :

Example 1 with PlaceBlockEvent

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

the class RegionProtectionListener method onPlaceBlock.

@EventHandler(ignoreCancelled = true)
public void onPlaceBlock(final PlaceBlockEvent 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());
    // Don't check liquid flow unless it's enabled
    if (event.getCause().getRootCause() instanceof Block && Materials.isLiquid(type) && !getWorldConfig(event.getWorld()).checkLiquidFlow) {
        return;
    }
    event.filter((Predicate<Location>) target -> {
        boolean canPlace;
        String what;
        if (Materials.isFire(type)) {
            Block block = event.getCause().getFirstBlock();
            boolean fire = block != null && Materials.isFire(type);
            boolean lava = block != null && Materials.isLava(block.getType());
            List<StateFlag> flags = new ArrayList<>();
            flags.add(Flags.BLOCK_PLACE);
            flags.add(Flags.LIGHTER);
            if (fire)
                flags.add(Flags.FIRE_SPREAD);
            if (lava)
                flags.add(Flags.LAVA_FIRE);
            canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, flags.toArray(new StateFlag[flags.size()])));
            what = "place fire";
        } else if (type == Material.FROSTED_ICE) {
            event.setSilent(true);
            canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_PLACE, Flags.FROSTED_ICE_FORM));
            what = "use frostwalker";
        } else {
            canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_PLACE));
            what = "place that block";
        }
        if (!canPlace) {
            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) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ArrayList(java.util.ArrayList) List(java.util.List) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with PlaceBlockEvent

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

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

the class EventAbstractionListener method onBlockIgnite.

@EventHandler(ignoreCancelled = true)
public void onBlockIgnite(BlockIgniteEvent event) {
    Block block = event.getBlock();
    Cause cause;
    // Find the cause
    if (event.getPlayer() != null) {
        cause = create(event.getPlayer());
    } else if (event.getIgnitingEntity() != null) {
        cause = create(event.getIgnitingEntity());
    } else if (event.getIgnitingBlock() != null) {
        cause = create(event.getIgnitingBlock());
    } else {
        cause = Cause.unknown();
    }
    Events.fireToCancel(event, new PlaceBlockEvent(event, cause, block.getLocation(), Material.FIRE));
}
Also used : Cause(com.sk89q.worldguard.bukkit.cause.Cause) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 4 with PlaceBlockEvent

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

the class EventAbstractionListener method onBlockPistonRetract.

@EventHandler(ignoreCancelled = true)
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
    if (event.isSticky()) {
        EventDebounce.Entry entry = pistonRetractDebounce.getIfNotPresent(new BlockPistonRetractKey(event), event);
        if (entry != null) {
            Block piston = event.getBlock();
            Cause cause = create(piston);
            BlockFace direction = event.getDirection();
            ArrayList<Block> blocks = new ArrayList<>(event.getBlocks());
            int originalSize = blocks.size();
            Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, cause, event.getBlock().getWorld(), blocks, Material.AIR));
            if (originalSize != blocks.size()) {
                event.setCancelled(true);
                return;
            }
            for (Block b : blocks) {
                Location loc = b.getRelative(direction).getLocation();
                Events.fireToCancel(event, new PlaceBlockEvent(event, cause, loc, b.getType()));
            }
            entry.setCancelled(event.isCancelled());
            if (event.isCancelled()) {
                playDenyEffect(piston.getLocation().add(0.5, 1, 0.5));
            }
        }
    }
}
Also used : BlockPistonRetractKey(com.sk89q.worldguard.bukkit.listener.debounce.BlockPistonRetractKey) 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) 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) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 5 with PlaceBlockEvent

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

the class EventAbstractionListener method onBlockFertilize.

@EventHandler(ignoreCancelled = true)
public void onBlockFertilize(BlockFertilizeEvent event) {
    if (event.getBlocks().isEmpty())
        return;
    Cause cause = create(event.getPlayer(), event.getBlock());
    Events.fireToCancel(event, new PlaceBlockEvent(event, cause, event.getBlock().getWorld(), event.getBlocks()));
}
Also used : Cause(com.sk89q.worldguard.bukkit.cause.Cause) 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