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;
}
Aggregations