Search in sources :

Example 1 with PlayerShearEntityEvent

use of org.bukkit.event.player.PlayerShearEntityEvent in project Glowstone by GlowstoneMC.

the class GlowSheep method entityInteract.

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
    super.entityInteract(player, message);
    if (message.getAction() == InteractEntityMessage.Action.INTERACT.ordinal()) {
        // if the sheep is not adult or the player is spectator abort
        if (!isAdult() || player.getGameMode().equals(GameMode.SPECTATOR)) {
            return false;
        }
        final ItemStack handItem = InventoryUtil.itemOrEmpty(player.getInventory().getItem(message.getHandSlot()));
        if (InventoryUtil.isEmpty(handItem)) {
            return false;
        }
        switch(handItem.getType()) {
            case SHEARS:
                // If already sheared return
                if (isSheared()) {
                    return false;
                }
                // noinspection deprecation
                PlayerShearEntityEvent shearEvent = new PlayerShearEntityEvent(player, this);
                shearEvent = EventFactory.getInstance().callEvent(shearEvent);
                if (shearEvent.isCancelled()) {
                    return false;
                }
                if (!player.getGameMode().equals(GameMode.CREATIVE)) {
                    if (handItem.getDurability() < 238) {
                        handItem.setDurability((short) (handItem.getDurability() + 1));
                        player.getInventory().setItem(message.getHandSlot(), handItem);
                    } else {
                        player.getInventory().setItem(message.getHandSlot(), InventoryUtil.createEmptyStack());
                    }
                }
                final GlowWorld world = getWorld();
                world.playSound(getLocation(), Sound.ENTITY_SHEEP_SHEAR, 1, 1);
                final Material mat = WoolUtil.getWoolMaterialByDye(getColor());
                world.dropItemNaturally(getLocation(), new ItemStack(mat, ThreadLocalRandom.current().nextInt(3) + 1));
                setSheared(true);
                return true;
            case INK_SAC:
                {
                    final Dye dye = (Dye) handItem.getData();
                    DyeColor color = dye.getColor();
                    SheepDyeWoolEvent dyeEvent = new SheepDyeWoolEvent(this, color);
                    dyeEvent = EventFactory.getInstance().callEvent(dyeEvent);
                    if (dyeEvent.isCancelled()) {
                        metadata.set(MetadataIndex.SHEEP_DATA, getColorByte(), true);
                        player.updateInventory();
                        return false;
                    }
                    color = dyeEvent.getColor();
                    // If same color, we're done
                    if (color.equals(getColor())) {
                        return false;
                    }
                    if (!player.getGameMode().equals(GameMode.CREATIVE)) {
                        player.getInventory().consumeItemInHand(message.getHandSlot());
                    }
                    setColor(color);
                    return true;
                }
            default:
                return false;
        }
    }
    return false;
}
Also used : Dye(org.bukkit.material.Dye) SheepDyeWoolEvent(org.bukkit.event.entity.SheepDyeWoolEvent) PlayerShearEntityEvent(org.bukkit.event.player.PlayerShearEntityEvent) GlowWorld(net.glowstone.GlowWorld) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) DyeColor(org.bukkit.DyeColor)

Aggregations

GlowWorld (net.glowstone.GlowWorld)1 DyeColor (org.bukkit.DyeColor)1 Material (org.bukkit.Material)1 SheepDyeWoolEvent (org.bukkit.event.entity.SheepDyeWoolEvent)1 PlayerShearEntityEvent (org.bukkit.event.player.PlayerShearEntityEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 Dye (org.bukkit.material.Dye)1