Fixed eating particles when eating was cancelled by client

This commit is contained in:
Arne Dalhuisen 2021-05-23 16:46:29 +02:00
parent a749f07a3f
commit 8b79945992
2 changed files with 17 additions and 9 deletions

View File

@ -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);

View File

@ -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);