Added PlayerPreEatEvent for more customisation, separate the default eating time and the individual ones. Also a InventoryOpenEvent#setInventory to change the inventory to open

This commit is contained in:
Felix Cravic 2020-05-12 18:40:04 +02:00
parent a1b427bf83
commit be0311a874
5 changed files with 82 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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