Search in sources :

Example 1 with PlayerUnleashEntityEvent

use of org.bukkit.event.player.PlayerUnleashEntityEvent 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)1 PlayerUnleashEntityEvent (org.bukkit.event.player.PlayerUnleashEntityEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1