Search in sources :

Example 1 with PlayerLeashEntityEvent

use of org.bukkit.event.entity.PlayerLeashEntityEvent in project Glowstone by GlowstoneMC.

the class BlockFence method blockInteract.

@Override
public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
    super.blockInteract(player, block, face, clickedLoc);
    if (!player.getLeashedEntities().isEmpty()) {
        LeashHitch leashHitch = GlowLeashHitch.getLeashHitchAt(block);
        ImmutableList.copyOf(player.getLeashedEntities()).stream().filter(e -> !(EventFactory.getInstance().callEvent(new PlayerLeashEntityEvent(e, leashHitch, player)).isCancelled())).forEach(e -> e.setLeashHolder(leashHitch));
        return true;
    }
    return false;
}
Also used : Vector(org.bukkit.util.Vector) GlowBlock(net.glowstone.block.GlowBlock) ImmutableList(com.google.common.collect.ImmutableList) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) MaterialMatcher(net.glowstone.inventory.MaterialMatcher) EventFactory(net.glowstone.EventFactory) PlayerLeashEntityEvent(org.bukkit.event.entity.PlayerLeashEntityEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) LeashHitch(org.bukkit.entity.LeashHitch) Material(org.bukkit.Material) PlayerLeashEntityEvent(org.bukkit.event.entity.PlayerLeashEntityEvent) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) LeashHitch(org.bukkit.entity.LeashHitch)

Example 2 with PlayerLeashEntityEvent

use of org.bukkit.event.entity.PlayerLeashEntityEvent in project Glowstone by GlowstoneMC.

the class GlowLivingEntity method entityInteract.

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
    super.entityInteract(player, message);
    if (message.getAction() != Action.INTERACT.ordinal()) {
        return false;
    }
    ItemStack handItem = InventoryUtil.itemOrEmpty(player.getInventory().getItem(message.getHandSlot()));
    if (isLeashed() && player.equals(this.getLeashHolder()) && message.getHandSlot() == EquipmentSlot.HAND) {
        if (EventFactory.getInstance().callEvent(new PlayerUnleashEntityEvent(this, player)).isCancelled()) {
            return false;
        }
        setLeashHolder(null);
        if (player.getGameMode() != GameMode.CREATIVE) {
            world.dropItemNaturally(this.location, new ItemStack(Material.LEAD));
        }
        return true;
    } else if (!InventoryUtil.isEmpty(handItem) && handItem.getType() == Material.LEAD) {
        if (!GlowLeashHitch.isAllowedLeashHolder(this.getType()) || this.isLeashed() || EventFactory.getInstance().callEvent(new PlayerLeashEntityEvent(this, player, player)).isCancelled()) {
            return false;
        }
        if (player.getGameMode() != GameMode.CREATIVE) {
            if (handItem.getAmount() > 1) {
                handItem.setAmount(handItem.getAmount() - 1);
            } else {
                handItem = InventoryUtil.createEmptyStack();
            }
            player.getInventory().setItem(message.getHandSlot(), handItem);
        }
        setLeashHolder(player);
        return true;
    }
    return false;
}
Also used : PlayerLeashEntityEvent(org.bukkit.event.entity.PlayerLeashEntityEvent) PlayerUnleashEntityEvent(org.bukkit.event.player.PlayerUnleashEntityEvent) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

PlayerLeashEntityEvent (org.bukkit.event.entity.PlayerLeashEntityEvent)2 ImmutableList (com.google.common.collect.ImmutableList)1 EventFactory (net.glowstone.EventFactory)1 GlowBlock (net.glowstone.block.GlowBlock)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 GlowLeashHitch (net.glowstone.entity.objects.GlowLeashHitch)1 MaterialMatcher (net.glowstone.inventory.MaterialMatcher)1 Material (org.bukkit.Material)1 BlockFace (org.bukkit.block.BlockFace)1 LeashHitch (org.bukkit.entity.LeashHitch)1 PlayerUnleashEntityEvent (org.bukkit.event.player.PlayerUnleashEntityEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 Vector (org.bukkit.util.Vector)1