Search in sources :

Example 6 with BreakBlockEvent

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

the class EventAbstractionListener method onBlockExplode.

@EventHandler(ignoreCancelled = true)
public void onBlockExplode(BlockExplodeEvent event) {
    final BreakBlockEvent eventToFire = new BreakBlockEvent(event, create(event.getBlock()), event.getBlock().getLocation().getWorld(), event.blockList(), Material.AIR);
    eventToFire.getRelevantFlags().add(Flags.OTHER_EXPLOSION);
    Events.fireBulkEventToCancel(event, eventToFire);
}
Also used : BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 7 with BreakBlockEvent

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

the class EventAbstractionListener method onEntityChangeBlock.

@EventHandler(ignoreCancelled = true)
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
    Block block = event.getBlock();
    Entity entity = event.getEntity();
    Material toType = event.getTo();
    Material fromType = block.getType();
    Cause cause = create(entity);
    // Fire two events: one as BREAK and one as PLACE
    if (toType != Material.AIR && fromType != Material.AIR) {
        BreakBlockEvent breakDelagate = new BreakBlockEvent(event, cause, block);
        setDelegateEventMaterialOptions(breakDelagate, fromType, toType);
        boolean denied;
        if (!(denied = Events.fireToCancel(event, breakDelagate))) {
            PlaceBlockEvent placeDelegate = new PlaceBlockEvent(event, cause, block.getLocation(), toType);
            setDelegateEventMaterialOptions(placeDelegate, fromType, toType);
            denied = Events.fireToCancel(event, placeDelegate);
        }
        if (denied && entity instanceof Player) {
            playDenyEffect((Player) entity, block.getLocation());
        }
        handleFallingBlock(event, denied);
    } else if (toType == Material.AIR) {
        // Track the source so later we can create a proper chain of causes
        if (entity instanceof FallingBlock) {
            Cause.trackParentCause(entity, block);
            // Switch around the event
            Events.fireToCancel(event, new SpawnEntityEvent(event, create(block), entity));
        } else {
            entityBreakBlockDebounce.debounce(block, event.getEntity(), event, new BreakBlockEvent(event, cause, block));
        }
    } else {
        // toType != Material.AIR && fromType == Material.AIR
        boolean denied = Events.fireToCancel(event, new PlaceBlockEvent(event, cause, block.getLocation(), toType));
        handleFallingBlock(event, denied);
    }
}
Also used : FallingBlock(org.bukkit.entity.FallingBlock) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Cause(com.sk89q.worldguard.bukkit.cause.Cause) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) Material(org.bukkit.Material) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) SpawnEntityEvent(com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent) EventHandler(org.bukkit.event.EventHandler)

Example 8 with BreakBlockEvent

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

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

the class EventAbstractionListener method onEntityExplode.

@EventHandler(ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
    Entity entity = event.getEntity();
    Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, create(entity), event.getLocation().getWorld(), event.blockList(), Material.AIR));
    if (entity instanceof Creeper) {
        Cause.untrackParentCause(entity);
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Creeper(org.bukkit.entity.Creeper) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 10 with BreakBlockEvent

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

the class RegionProtectionListener method onBreakBlock.

@EventHandler(ignoreCancelled = true)
public void onBreakBlock(final BreakBlockEvent 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 RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    if (!event.isCancelled()) {
        final RegionAssociable associable = createRegionAssociable(event.getCause());
        event.filter((Predicate<Location>) target -> {
            boolean canBreak;
            String what;
            if (event.getCause().find(EntityType.PRIMED_TNT, EntityType.MINECART_TNT) != null) {
                canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK, Flags.TNT));
                what = "use dynamite";
            } else {
                canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK));
                what = "break that block";
            }
            if (!canBreak) {
                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) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BreakBlockEvent (com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent)10 EventHandler (org.bukkit.event.EventHandler)10 Block (org.bukkit.block.Block)7 PlaceBlockEvent (com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent)6 FallingBlock (org.bukkit.entity.FallingBlock)6 Cause (com.sk89q.worldguard.bukkit.cause.Cause)5 Player (org.bukkit.entity.Player)4 UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)3 ArrayList (java.util.ArrayList)3 Entity (org.bukkit.entity.Entity)3 ItemStack (org.bukkit.inventory.ItemStack)3 DelegateEvent (com.sk89q.worldguard.bukkit.event.DelegateEvent)2 UseBlockEvent (com.sk89q.worldguard.bukkit.event.block.UseBlockEvent)2 SpawnEntityEvent (com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)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 Material (org.bukkit.Material)2