Search in sources :

Example 1 with UseItemEvent

use of com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent 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 UseItemEvent

use of com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onPlayerBucketFill.

@EventHandler(ignoreCancelled = true)
public void onPlayerBucketFill(PlayerBucketFillEvent event) {
    Player player = event.getPlayer();
    Block blockAffected = event.getBlockClicked().getRelative(event.getBlockFace());
    boolean allowed = false;
    // Milk buckets can't be emptied as of writing
    if (event.getItemStack().getType() == Material.MILK_BUCKET) {
        allowed = true;
    }
    ItemStack item = new ItemStack(event.getBucket(), 1);
    Events.fireToCancel(event, new BreakBlockEvent(event, create(player), blockAffected).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) ItemStack(org.bukkit.inventory.ItemStack) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 3 with UseItemEvent

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

use of com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onLingeringSplash.

@EventHandler(ignoreCancelled = true)
public void onLingeringSplash(LingeringPotionSplashEvent event) {
    AreaEffectCloud aec = event.getAreaEffectCloud();
    ThrownPotion potion = event.getEntity();
    World world = potion.getWorld();
    Cause cause = create(event.getEntity());
    // Fire item interaction event
    Events.fireToCancel(event, new UseItemEvent(event, cause, world, potion.getItem()));
    // Fire entity spawn event
    if (!event.isCancelled()) {
        // radius unfortunately doesn't go through with this, so only a single location is tested
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, aec.getLocation().add(0.5, 0, 0.5), EntityType.AREA_EFFECT_CLOUD));
    }
}
Also used : UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) Cause(com.sk89q.worldguard.bukkit.cause.Cause) ThrownPotion(org.bukkit.entity.ThrownPotion) AreaEffectCloud(org.bukkit.entity.AreaEffectCloud) World(org.bukkit.World) SpawnEntityEvent(com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent) EventHandler(org.bukkit.event.EventHandler)

Example 5 with UseItemEvent

use of com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onPotionSplash.

@EventHandler(ignoreCancelled = true)
public void onPotionSplash(PotionSplashEvent event) {
    Entity entity = event.getEntity();
    ThrownPotion potion = event.getPotion();
    World world = entity.getWorld();
    Cause cause = create(potion);
    // Fire item interaction event
    Events.fireToCancel(event, new UseItemEvent(event, cause, world, potion.getItem()));
    // Fire entity interaction event
    if (!event.isCancelled()) {
        int blocked = 0;
        int affectedSize = event.getAffectedEntities().size();
        boolean hasDamageEffect = Materials.hasDamageEffect(potion.getEffects());
        for (LivingEntity affected : event.getAffectedEntities()) {
            DelegateEvent delegate = hasDamageEffect ? new DamageEntityEvent(event, cause, affected) : new UseEntityEvent(event, cause, affected);
            // Consider the potion splash flag
            delegate.getRelevantFlags().add(Flags.POTION_SPLASH);
            if (Events.fireAndTestCancel(delegate)) {
                event.setIntensity(affected, 0);
                blocked++;
            }
        }
        if (affectedSize > 0 && blocked == affectedSize) {
            // server does weird things with this if the event is modified, so use cached number
            event.setCancelled(true);
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) DamageEntityEvent(com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) DelegateEvent(com.sk89q.worldguard.bukkit.event.DelegateEvent) Cause(com.sk89q.worldguard.bukkit.cause.Cause) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) ThrownPotion(org.bukkit.entity.ThrownPotion) World(org.bukkit.World) EventHandler(org.bukkit.event.EventHandler)

Aggregations

UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)8 EventHandler (org.bukkit.event.EventHandler)8 ItemStack (org.bukkit.inventory.ItemStack)6 Cause (com.sk89q.worldguard.bukkit.cause.Cause)4 PlaceBlockEvent (com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent)4 Block (org.bukkit.block.Block)4 FallingBlock (org.bukkit.entity.FallingBlock)4 Player (org.bukkit.entity.Player)4 BreakBlockEvent (com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent)3 World (org.bukkit.World)3 DelegateEvent (com.sk89q.worldguard.bukkit.event.DelegateEvent)2 UseEntityEvent (com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent)2 Material (org.bukkit.Material)2 Entity (org.bukkit.entity.Entity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 ThrownPotion (org.bukkit.entity.ThrownPotion)2 UseBlockEvent (com.sk89q.worldguard.bukkit.event.block.UseBlockEvent)1 DamageEntityEvent (com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent)1 SpawnEntityEvent (com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)1 Nullable (javax.annotation.Nullable)1