Search in sources :

Example 6 with UseItemEvent

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

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

the class EventAbstractionListener method onPlayerBucketEmpty.

@EventHandler(ignoreCancelled = true)
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
    Player player = event.getPlayer();
    Block blockClicked = event.getBlockClicked();
    Block blockAffected;
    if (blockClicked.getBlockData() instanceof Waterlogged) {
        blockAffected = blockClicked;
    } else {
        blockAffected = blockClicked.getRelative(event.getBlockFace());
    }
    boolean allowed = false;
    // Milk buckets can't be emptied as of writing
    if (event.getBucket() == Material.MILK_BUCKET) {
        allowed = true;
    }
    ItemStack item = new ItemStack(event.getBucket(), 1);
    Material blockMaterial = Materials.getBucketBlockMaterial(event.getBucket());
    Events.fireToCancel(event, new PlaceBlockEvent(event, create(player), blockAffected.getLocation(), blockMaterial).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) Waterlogged(org.bukkit.block.data.Waterlogged) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 8 with UseItemEvent

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

the class EventAbstractionListener method onPlayerInteractEntity.

// -------------------------------------------------------------------------
// Entity external interaction
// -------------------------------------------------------------------------
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    Player player = event.getPlayer();
    World world = player.getWorld();
    ItemStack item = event.getHand() == EquipmentSlot.OFF_HAND ? player.getInventory().getItemInOffHand() : player.getInventory().getItemInMainHand();
    Entity entity = event.getRightClicked();
    if (Events.fireToCancel(event, new UseItemEvent(event, create(player), world, item))) {
        return;
    }
    final UseEntityEvent useEntityEvent = new UseEntityEvent(event, create(player), entity);
    Material matchingItem = Materials.getRelatedMaterial(entity.getType());
    if (matchingItem != null && hasInteractBypass(world, matchingItem)) {
        useEntityEvent.setAllowed(true);
    }
    if (!Events.fireToCancel(event, useEntityEvent)) {
        // so this is a hack but CreeperIgniteEvent doesn't actually tell us who, so we need to do it here
        if (item.getType() == Material.FLINT_AND_STEEL && entity.getType() == EntityType.CREEPER) {
            Cause.trackParentCause(entity, player);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) UseItemEvent(com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) Material(org.bukkit.Material) World(org.bukkit.World) ItemStack(org.bukkit.inventory.ItemStack) 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