diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index e3794c095..69406fd7d 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -236,6 +236,12 @@ public class PlayerInit { System.out.println("ITEM UPDATE STATE"); }); + player.addEventCallback(PlayerPreEatEvent.class, event -> { + ItemStack itemStack = event.getFoodItem(); + Material material = itemStack.getMaterial(); + event.setEatingTime(material == Material.PORKCHOP ? 100 : 1000); + }); + player.addEventCallback(PlayerEatEvent.class, event -> { System.out.println("PLAYER EAT EVENT"); }); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 3ce42eca6..2082c88eb 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -74,7 +74,8 @@ public class Player extends LivingEntity { private int food; private float foodSaturation; private long startEatingTime; - private long eatingTime = 1000L; + private long defaultEatingTime = 1000L; + private long eatingTime; private boolean isEating; // CustomBlock break delay @@ -594,13 +595,17 @@ public class Player extends LivingEntity { return isEating; } + public long getDefaultEatingTime() { + return defaultEatingTime; + } + /** - * Used to change the eating time animation + * Used to change the default eating time animation * - * @param eatingTime the eating time in milliseconds + * @param defaultEatingTime the default eating time in milliseconds */ - public void setEatingTime(long eatingTime) { - this.eatingTime = eatingTime; + public void setDefaultEatingTime(long defaultEatingTime) { + this.defaultEatingTime = defaultEatingTime; } public boolean dropItem(ItemStack item) { @@ -892,13 +897,15 @@ public class Player extends LivingEntity { closeInventory(); } + Inventory newInventory = inventoryOpenEvent.getInventory(); + OpenWindowPacket openWindowPacket = new OpenWindowPacket(); - openWindowPacket.windowId = inventory.getWindowId(); - openWindowPacket.windowType = inventory.getInventoryType().getWindowType(); - openWindowPacket.title = inventory.getTitle(); + openWindowPacket.windowId = newInventory.getWindowId(); + openWindowPacket.windowType = newInventory.getInventoryType().getWindowType(); + openWindowPacket.title = newInventory.getTitle(); playerConnection.sendPacket(openWindowPacket); - inventory.addViewer(this); - refreshOpenInventory(inventory); + newInventory.addViewer(this); + refreshOpenInventory(newInventory); }); @@ -1056,15 +1063,20 @@ public class Player extends LivingEntity { this.openInventory = openInventory; } - public void refreshEating(boolean isEating) { + public void refreshEating(boolean isEating, long eatingTime) { this.isEating = isEating; if (isEating) { this.startEatingTime = System.currentTimeMillis(); + this.eatingTime = eatingTime; } else { this.startEatingTime = 0; } } + public void refreshEating(boolean isEating) { + refreshEating(isEating, defaultEatingTime); + } + public ItemUpdateStateEvent callItemUpdateStateEvent(boolean allowFood) { Material mainHandMat = Material.fromId(getItemInMainHand().getMaterialId()); Material offHandMat = Material.fromId(getItemInOffHand().getMaterialId()); diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java index 2587b099c..e58a64b80 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java @@ -21,4 +21,11 @@ public class InventoryOpenEvent extends CancellableEvent { public Inventory getInventory() { return inventory; } + + public void setInventory(Inventory inventory) { + if (inventory == null) + throw new NullPointerException("Inventory cannot be null!"); + + this.inventory = inventory; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java new file mode 100644 index 000000000..8b2dc31c3 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java @@ -0,0 +1,39 @@ +package net.minestom.server.event.player; + +import net.minestom.server.entity.Player; +import net.minestom.server.event.CancellableEvent; +import net.minestom.server.item.ItemStack; + +/** + * Called before the PlayerEatEvent and can be used to change the eating time + * or to cancel its processing, cancelling the event means that the player will + * continue the animation indefinitely + */ +public class PlayerPreEatEvent extends CancellableEvent { + + private Player player; + private ItemStack foodItem; + private long eatingTime; + + public PlayerPreEatEvent(Player player, ItemStack foodItem, long eatingTime) { + this.player = player; + this.foodItem = foodItem; + this.eatingTime = eatingTime; + } + + public Player getPlayer() { + return player; + } + + public ItemStack getFoodItem() { + return foodItem; + } + + public long getEatingTime() { + return eatingTime; + } + + public void setEatingTime(long eatingTime) { + this.eatingTime = eatingTime; + } +} diff --git a/src/main/java/net/minestom/server/listener/UseItemListener.java b/src/main/java/net/minestom/server/listener/UseItemListener.java index e2c7b1e32..6e685b475 100644 --- a/src/main/java/net/minestom/server/listener/UseItemListener.java +++ b/src/main/java/net/minestom/server/listener/UseItemListener.java @@ -3,6 +3,7 @@ package net.minestom.server.listener; import net.minestom.server.entity.Player; import net.minestom.server.event.animation.ArmAnimationEvent; import net.minestom.server.event.item.ArmorEquipEvent; +import net.minestom.server.event.player.PlayerPreEatEvent; import net.minestom.server.event.player.PlayerUseItemEvent; import net.minestom.server.inventory.PlayerInventory; import net.minestom.server.item.ItemStack; @@ -77,7 +78,12 @@ public class UseItemListener { armAnimationEvent = new ArmAnimationEvent(ArmAnimationEvent.ArmAnimationType.TRIDENT); } else if (material.isFood()) { armAnimationEvent = new ArmAnimationEvent(ArmAnimationEvent.ArmAnimationType.EAT); - player.refreshEating(true); + + // Eating code, contains the eating time customisation + PlayerPreEatEvent playerPreEatEvent = new PlayerPreEatEvent(player, itemStack, player.getDefaultEatingTime()); + player.callCancellableEvent(PlayerPreEatEvent.class, playerPreEatEvent, () -> { + player.refreshEating(true, playerPreEatEvent.getEatingTime()); + }); } if (armAnimationEvent != null)