Search in sources :

Example 1 with UseEntityEvent

use of com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method onEntityUnleash.

@EventHandler(ignoreCancelled = true)
public void onEntityUnleash(EntityUnleashEvent event) {
    if (event instanceof PlayerUnleashEntityEvent) {
        PlayerUnleashEntityEvent playerEvent = (PlayerUnleashEntityEvent) event;
        Events.fireToCancel(playerEvent, new UseEntityEvent(playerEvent, create(playerEvent.getPlayer()), event.getEntity()));
    }
}
Also used : PlayerUnleashEntityEvent(org.bukkit.event.player.PlayerUnleashEntityEvent) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) EventHandler(org.bukkit.event.EventHandler)

Example 2 with UseEntityEvent

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

Example 3 with UseEntityEvent

use of com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method handleInventoryHolderUse.

private static <T extends Event & Cancellable> void handleInventoryHolderUse(T originalEvent, Cause cause, InventoryHolder holder) {
    if (originalEvent.isCancelled()) {
        return;
    }
    if (holder instanceof Entity) {
        Entity entity = (Entity) holder;
        Material mat = Materials.getRelatedMaterial((entity).getType());
        UseEntityEvent useEntityEvent = new UseEntityEvent(originalEvent, cause, entity);
        if (mat != null && hasInteractBypass((entity).getWorld(), mat)) {
            useEntityEvent.setAllowed(true);
        }
        Events.fireToCancel(originalEvent, useEntityEvent);
    } else {
        if (holder instanceof BlockState) {
            final BlockState block = (BlockState) holder;
            final UseBlockEvent useBlockEvent = new UseBlockEvent(originalEvent, cause, block.getBlock());
            if (hasInteractBypass(block.getWorld(), block.getType())) {
                useBlockEvent.setAllowed(true);
            }
            Events.fireToCancel(originalEvent, useBlockEvent);
        } else if (holder instanceof DoubleChest) {
            InventoryHolder left = ((DoubleChest) holder).getLeftSide();
            InventoryHolder right = ((DoubleChest) holder).getRightSide();
            if (left instanceof Chest) {
                Events.fireToCancel(originalEvent, new UseBlockEvent(originalEvent, cause, ((Chest) left).getBlock()));
            }
            if (right instanceof Chest) {
                Events.fireToCancel(originalEvent, new UseBlockEvent(originalEvent, cause, ((Chest) right).getBlock()));
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) DoubleChest(org.bukkit.block.DoubleChest) Chest(org.bukkit.block.Chest) BlockState(org.bukkit.block.BlockState) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) Material(org.bukkit.Material) DoubleChest(org.bukkit.block.DoubleChest) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 4 with UseEntityEvent

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

UseEntityEvent (com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent)4 Entity (org.bukkit.entity.Entity)3 LivingEntity (org.bukkit.entity.LivingEntity)3 EventHandler (org.bukkit.event.EventHandler)3 UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)2 Material (org.bukkit.Material)2 World (org.bukkit.World)2 Cause (com.sk89q.worldguard.bukkit.cause.Cause)1 DelegateEvent (com.sk89q.worldguard.bukkit.event.DelegateEvent)1 UseBlockEvent (com.sk89q.worldguard.bukkit.event.block.UseBlockEvent)1 DamageEntityEvent (com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent)1 BlockState (org.bukkit.block.BlockState)1 Chest (org.bukkit.block.Chest)1 DoubleChest (org.bukkit.block.DoubleChest)1 Player (org.bukkit.entity.Player)1 ThrownPotion (org.bukkit.entity.ThrownPotion)1 PlayerUnleashEntityEvent (org.bukkit.event.player.PlayerUnleashEntityEvent)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 ItemStack (org.bukkit.inventory.ItemStack)1