From 8b7994599207105e5a3ef7e978f2389898e6d0f0 Mon Sep 17 00:00:00 2001 From: Arne Dalhuisen <59421074+Bloepiloepi@users.noreply.github.com> Date: Sun, 23 May 2021 16:46:29 +0200 Subject: [PATCH 1/4] Fixed eating particles when eating was cancelled by client --- .../net/minestom/server/entity/Player.java | 18 +++++++++++------- .../server/listener/PlayerDiggingListener.java | 8 ++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 49bd4ec3f..923f010fc 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -401,7 +401,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, if (isEating()) { if (time - startEatingTime >= eatingTime) { triggerStatus((byte) 9); // Mark item use as finished - ItemUpdateStateEvent itemUpdateStateEvent = callItemUpdateStateEvent(true, eatingHand); + ItemUpdateStateEvent itemUpdateStateEvent = callItemUpdateStateEvent(eatingHand); Check.notNull(itemUpdateStateEvent, "#callItemUpdateStateEvent returned null."); @@ -1136,6 +1136,15 @@ public class Player extends LivingEntity implements CommandSender, Localizable, return eatingHand != null; } + /** + * Gets the hand which the player is eating from. + * + * @return the eating hand, null if none + */ + public Hand getEatingHand() { + return eatingHand; + } + /** * Gets the player default eating time. * @@ -2287,19 +2296,14 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * Used to call {@link ItemUpdateStateEvent} with the proper item * It does check which hand to get the item to update. * - * @param allowFood true if food should be updated, false otherwise * @return the called {@link ItemUpdateStateEvent}, * null if there is no item to update the state */ - public @Nullable ItemUpdateStateEvent callItemUpdateStateEvent(boolean allowFood, @Nullable Hand hand) { + public @Nullable ItemUpdateStateEvent callItemUpdateStateEvent(@Nullable Hand hand) { if (hand == null) return null; final ItemStack updatedItem = getItemInHand(hand); - final boolean isFood = updatedItem.getMaterial().isFood(); - - if (isFood && !allowFood) - return null; ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(this, hand, updatedItem); callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent); diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index 351c78bac..2940944cf 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -136,14 +136,18 @@ public class PlayerDiggingListener { } else if (status == ClientPlayerDiggingPacket.Status.UPDATE_ITEM_STATE) { Player.Hand hand = null; - if (player.getItemInHand(Player.Hand.OFF).getMaterial().hasState()) { + if (player.isEating()) { + hand = player.getEatingHand(); + } else if (player.getItemInHand(Player.Hand.OFF).getMaterial().hasState()) { hand = Player.Hand.OFF; } else if (player.getItemInHand(Player.Hand.MAIN).getMaterial().hasState()) { hand = Player.Hand.MAIN; } player.refreshEating(null); - ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false, hand); + player.triggerStatus((byte) 9); + + ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(hand); if (itemUpdateStateEvent == null) { player.refreshActiveHand(true, false, false); From ca96ed342065028830cba373daf55d2d725a6d77 Mon Sep 17 00:00:00 2001 From: Arne Dalhuisen <59421074+Bloepiloepi@users.noreply.github.com> Date: Sun, 23 May 2021 16:56:48 +0200 Subject: [PATCH 2/4] Also cancel food animation when PlayerPreEatEvent is cancelled --- .../net/minestom/server/listener/UseItemListener.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/listener/UseItemListener.java b/src/main/java/net/minestom/server/listener/UseItemListener.java index df80bed7b..a72bba3ef 100644 --- a/src/main/java/net/minestom/server/listener/UseItemListener.java +++ b/src/main/java/net/minestom/server/listener/UseItemListener.java @@ -50,6 +50,8 @@ public class UseItemListener { PlayerItemAnimationEvent.ItemAnimationType itemAnimationType = null; boolean riptideSpinAttack = false; + boolean cancelAnimation = false; + if (material == Material.BOW) { itemAnimationType = PlayerItemAnimationEvent.ItemAnimationType.BOW; } else if (material == Material.CROSSBOW) { @@ -64,9 +66,13 @@ public class UseItemListener { // Eating code, contains the eating time customisation PlayerPreEatEvent playerPreEatEvent = new PlayerPreEatEvent(player, itemStack, hand, player.getDefaultEatingTime()); player.callCancellableEvent(PlayerPreEatEvent.class, playerPreEatEvent, () -> player.refreshEating(hand, playerPreEatEvent.getEatingTime())); + + if (playerPreEatEvent.isCancelled()) { + cancelAnimation = true; + } } - if (itemAnimationType != null) { + if (!cancelAnimation && itemAnimationType != null) { PlayerItemAnimationEvent playerItemAnimationEvent = new PlayerItemAnimationEvent(player, itemAnimationType); player.callCancellableEvent(PlayerItemAnimationEvent.class, playerItemAnimationEvent, () -> { player.refreshActiveHand(true, hand == Player.Hand.OFF, riptideSpinAttack); From e3c41a254d76db7e4bf26e70fe21cbc222c41f68 Mon Sep 17 00:00:00 2001 From: Arne Dalhuisen <59421074+Bloepiloepi@users.noreply.github.com> Date: Sun, 23 May 2021 20:19:46 +0200 Subject: [PATCH 3/4] Added callItemUpdateStateEvent(allowFood, hand) overload, getEatingHand is now @Nullable --- .../net/minestom/server/entity/Player.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 923f010fc..8dc3290ec 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1141,7 +1141,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * * @return the eating hand, null if none */ - public Hand getEatingHand() { + public @Nullable Hand getEatingHand() { return eatingHand; } @@ -2296,14 +2296,19 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * Used to call {@link ItemUpdateStateEvent} with the proper item * It does check which hand to get the item to update. * + * @param allowFood true if food should be updated, false otherwise * @return the called {@link ItemUpdateStateEvent}, * null if there is no item to update the state */ - public @Nullable ItemUpdateStateEvent callItemUpdateStateEvent(@Nullable Hand hand) { + public @Nullable ItemUpdateStateEvent callItemUpdateStateEvent(boolean allowFood, @Nullable Hand hand) { if (hand == null) return null; final ItemStack updatedItem = getItemInHand(hand); + final boolean isFood = updatedItem.getMaterial().isFood(); + + if (isFood && !allowFood) + return null; ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(this, hand, updatedItem); callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent); @@ -2311,6 +2316,17 @@ public class Player extends LivingEntity implements CommandSender, Localizable, return itemUpdateStateEvent; } + /** + * Used to call {@link ItemUpdateStateEvent} with the proper item + * It does check which hand to get the item to update. Allows food. + * + * @return the called {@link ItemUpdateStateEvent}, + * null if there is no item to update the state + */ + public @Nullable ItemUpdateStateEvent callItemUpdateStateEvent(@Nullable Hand hand) { + return callItemUpdateStateEvent(true, hand); + } + /** * Makes the player digging a custom block, see {@link #resetTargetBlock()} to rewind. * From 1da372eb3bac479ccacfab4284254069d6fbc9fb Mon Sep 17 00:00:00 2001 From: Arne Dalhuisen <59421074+Bloepiloepi@users.noreply.github.com> Date: Sun, 23 May 2021 20:36:51 +0200 Subject: [PATCH 4/4] Deprecation notice for callItemUpdateStateEvent(allowFood, hand) --- src/main/java/net/minestom/server/entity/Player.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 8dc3290ec..846194261 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -2299,7 +2299,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * @param allowFood true if food should be updated, false otherwise * @return the called {@link ItemUpdateStateEvent}, * null if there is no item to update the state + * + * @deprecated Use {@link #callItemUpdateStateEvent(Hand)} instead */ + @Deprecated public @Nullable ItemUpdateStateEvent callItemUpdateStateEvent(boolean allowFood, @Nullable Hand hand) { if (hand == null) return null;