Added hand inside eat events

This commit is contained in:
TheMode 2021-04-13 22:59:40 +02:00
parent a55ae4d048
commit 6ba336ee73
5 changed files with 41 additions and 32 deletions

View File

@ -131,7 +131,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
private long startEatingTime;
private long defaultEatingTime = 1000L;
private long eatingTime;
private boolean isEating;
private Hand eatingHand;
// Game state (https://wiki.vg/Protocol#Change_Game_State)
private boolean enableRespawnScreen;
@ -398,10 +398,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Eating animation
if (isEating()) {
if (time - startEatingTime >= eatingTime) {
refreshEating(false);
triggerStatus((byte) 9); // Mark item use as finished
ItemUpdateStateEvent itemUpdateStateEvent = callItemUpdateStateEvent(true);
ItemUpdateStateEvent itemUpdateStateEvent = callItemUpdateStateEvent(true, eatingHand);
Check.notNull(itemUpdateStateEvent, "#callItemUpdateStateEvent returned null.");
@ -413,9 +411,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
final boolean isFood = foodItem.getMaterial().isFood();
if (isFood) {
PlayerEatEvent playerEatEvent = new PlayerEatEvent(this, foodItem);
PlayerEatEvent playerEatEvent = new PlayerEatEvent(this, foodItem, eatingHand);
callEvent(PlayerEatEvent.class, playerEatEvent);
}
refreshEating(null);
}
}
@ -1192,7 +1192,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* @return true if the player is eating, false otherwise
*/
public boolean isEating() {
return isEating;
return eatingHand != null;
}
/**
@ -2319,12 +2319,12 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
this.heldSlot = slot;
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
refreshEating(false);
refreshEating(null);
}
public void refreshEating(boolean isEating, long eatingTime) {
this.isEating = isEating;
if (isEating) {
public void refreshEating(@Nullable Hand eatingHand, long eatingTime) {
this.eatingHand = eatingHand;
if (eatingHand != null) {
this.startEatingTime = System.currentTimeMillis();
this.eatingTime = eatingTime;
} else {
@ -2332,8 +2332,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
}
public void refreshEating(boolean isEating) {
refreshEating(isEating, defaultEatingTime);
public void refreshEating(@Nullable Hand eatingHand) {
refreshEating(eatingHand, defaultEatingTime);
}
/**
@ -2344,23 +2344,16 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* @return the called {@link ItemUpdateStateEvent},
* null if there is no item to update the state
*/
@Nullable
public ItemUpdateStateEvent callItemUpdateStateEvent(boolean allowFood) {
final Material mainHandMat = getItemInMainHand().getMaterial();
final Material offHandMat = getItemInOffHand().getMaterial();
final boolean isOffhand = offHandMat.hasState();
final ItemStack updatedItem = isOffhand ? getItemInOffHand() :
mainHandMat.hasState() ? getItemInMainHand() : null;
if (updatedItem == null) // No item with state, cancel
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;
final Hand hand = isOffhand ? Hand.OFF : Hand.MAIN;
ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(this, hand, updatedItem);
callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent);

View File

@ -11,10 +11,12 @@ import org.jetbrains.annotations.NotNull;
public class PlayerEatEvent extends PlayerEvent {
private final ItemStack foodItem;
private final Player.Hand hand;
public PlayerEatEvent(@NotNull Player player, @NotNull ItemStack foodItem) {
public PlayerEatEvent(@NotNull Player player, @NotNull ItemStack foodItem, @NotNull Player.Hand hand) {
super(player);
this.foodItem = foodItem;
this.hand = hand;
}
/**
@ -22,8 +24,11 @@ public class PlayerEatEvent extends PlayerEvent {
*
* @return the food item
*/
@NotNull
public ItemStack getFoodItem() {
public @NotNull ItemStack getFoodItem() {
return foodItem;
}
public @NotNull Player.Hand getHand() {
return hand;
}
}

View File

@ -14,13 +14,15 @@ import org.jetbrains.annotations.NotNull;
public class PlayerPreEatEvent extends PlayerEvent implements CancellableEvent {
private final ItemStack foodItem;
private final Player.Hand hand;
private long eatingTime;
private boolean cancelled;
public PlayerPreEatEvent(@NotNull Player player, @NotNull ItemStack foodItem, long eatingTime) {
public PlayerPreEatEvent(@NotNull Player player, @NotNull ItemStack foodItem, @NotNull Player.Hand hand, long eatingTime) {
super(player);
this.foodItem = foodItem;
this.hand = hand;
this.eatingTime = eatingTime;
}
@ -29,11 +31,14 @@ public class PlayerPreEatEvent extends PlayerEvent implements CancellableEvent {
*
* @return the food item
*/
@NotNull
public ItemStack getFoodItem() {
public @NotNull ItemStack getFoodItem() {
return foodItem;
}
public @NotNull Player.Hand getHand() {
return hand;
}
/**
* Gets the food eating time.
* <p>

View File

@ -135,9 +135,15 @@ public class PlayerDiggingListener {
}
} else if (status == ClientPlayerDiggingPacket.Status.UPDATE_ITEM_STATE) {
Player.Hand hand = null;
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(false);
ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false);
player.refreshEating(null);
ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false, hand);
if (itemUpdateStateEvent == null) {
player.refreshActiveHand(true, false, false);

View File

@ -62,8 +62,8 @@ public class UseItemListener {
itemAnimationType = PlayerItemAnimationEvent.ItemAnimationType.EAT;
// 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()));
PlayerPreEatEvent playerPreEatEvent = new PlayerPreEatEvent(player, itemStack, hand, player.getDefaultEatingTime());
player.callCancellableEvent(PlayerPreEatEvent.class, playerPreEatEvent, () -> player.refreshEating(hand, playerPreEatEvent.getEatingTime()));
}
if (itemAnimationType != null) {