Search in sources :

Example 6 with PlaceBlockEvent

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

the class EventAbstractionListener method onBlockFromTo.

// TODO: Handle EntityPortalEnterEvent
// -------------------------------------------------------------------------
// Block self-interaction
// -------------------------------------------------------------------------
@EventHandler(ignoreCancelled = true)
public void onBlockFromTo(BlockFromToEvent event) {
    WorldConfiguration config = getWorldConfig(event.getBlock().getWorld());
    // frequency events at the moment
    if (!config.useRegions || (!config.highFreqFlags && !config.checkLiquidFlow)) {
        return;
    }
    Block from = event.getBlock();
    Block to = event.getToBlock();
    Material fromType = from.getType();
    Material toType = to.getType();
    // Liquids pass this event when flowing to solid blocks
    if (Materials.isLiquid(fromType) && toType.isSolid()) {
        return;
    }
    // flow and the from/to data may not be correct.
    if ((Materials.isWater(fromType) && Materials.isWater(toType)) || (Materials.isLava(fromType) && Materials.isLava(toType))) {
        return;
    }
    Cause cause = create(from);
    // Disable since it's probably not needed
    /*if (from.getType() != Material.AIR) {
            Events.fireToCancel(event, new BreakBlockEvent(event, cause, to));
        }*/
    Events.fireToCancel(event, new PlaceBlockEvent(event, cause, to.getLocation(), from.getType()));
}
Also used : WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) BukkitWorldConfiguration(com.sk89q.worldguard.bukkit.BukkitWorldConfiguration) Cause(com.sk89q.worldguard.bukkit.cause.Cause) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) Material(org.bukkit.Material) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 7 with PlaceBlockEvent

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

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

the class EventAbstractionListener method onStructureGrowEvent.

@EventHandler(ignoreCancelled = true)
public void onStructureGrowEvent(StructureGrowEvent event) {
    int originalCount = event.getBlocks().size();
    Player player = event.getPlayer();
    Events.fireBulkEventToCancel(event, new PlaceBlockEvent(event, create(player == null ? event.getLocation().getBlock() : player), event.getLocation().getWorld(), event.getBlocks()));
    if (!event.isCancelled() && event.getBlocks().size() != originalCount) {
        event.getLocation().getBlock().setType(Material.AIR);
    }
}
Also used : Player(org.bukkit.entity.Player) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 9 with PlaceBlockEvent

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

the class EventAbstractionListener method onBlockDispense.

@EventHandler(ignoreCancelled = true)
public void onBlockDispense(BlockDispenseEvent event) {
    Block dispenserBlock = event.getBlock();
    // Simulate right click event as players have it
    if (dispenserBlock.getType() == Material.DISPENSER) {
        Cause cause = create(event.getBlock());
        ItemStack item = event.getItem();
        if (Events.fireToCancel(event, new UseItemEvent(event, cause, dispenserBlock.getWorld(), item))) {
            return;
        }
        BlockData blockData = dispenserBlock.getBlockData();
        // if this ClassCastExceptions it's a bukkit bug
        Dispenser dispenser = (Dispenser) blockData;
        Block placed = dispenserBlock.getRelative(dispenser.getFacing());
        Block clicked = placed.getRelative(dispenser.getFacing());
        handleBlockRightClick(event, cause, item, clicked, placed);
        // handle special dispenser behavior
        if (Materials.isShulkerBox(item.getType())) {
            if (Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), item.getType()))) {
                playDenyEffect(placed.getLocation());
            }
        } else if (isItemAppliedToBlock(item, placed)) {
            if (Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), placed.getType()))) {
                playDenyEffect(placed.getLocation());
            }
        }
    }
}
Also used : Dispenser(org.bukkit.block.data.type.Dispenser) UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) Cause(com.sk89q.worldguard.bukkit.cause.Cause) FallingBlock(org.bukkit.entity.FallingBlock) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) BlockData(org.bukkit.block.data.BlockData) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 10 with PlaceBlockEvent

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

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