mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
Custom food animation delay + PlayerEatEvent
This commit is contained in:
parent
f5d4cba29d
commit
37add239f8
@ -10,6 +10,7 @@ import net.minestom.server.entity.*;
|
|||||||
import net.minestom.server.entity.damage.DamageType;
|
import net.minestom.server.entity.damage.DamageType;
|
||||||
import net.minestom.server.event.entity.AttackEvent;
|
import net.minestom.server.event.entity.AttackEvent;
|
||||||
import net.minestom.server.event.item.ItemDropEvent;
|
import net.minestom.server.event.item.ItemDropEvent;
|
||||||
|
import net.minestom.server.event.item.ItemUpdateStateEvent;
|
||||||
import net.minestom.server.event.item.PickupItemEvent;
|
import net.minestom.server.event.item.PickupItemEvent;
|
||||||
import net.minestom.server.event.player.*;
|
import net.minestom.server.event.player.*;
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
@ -177,7 +178,7 @@ public class PlayerInit {
|
|||||||
});
|
});
|
||||||
|
|
||||||
player.addEventCallback(PlayerSpawnEvent.class, event -> {
|
player.addEventCallback(PlayerSpawnEvent.class, event -> {
|
||||||
player.setGameMode(GameMode.SURVIVAL);
|
player.setGameMode(GameMode.CREATIVE);
|
||||||
player.teleport(new Position(0, 75, 0));
|
player.teleport(new Position(0, 75, 0));
|
||||||
|
|
||||||
ItemStack item = new ItemStack(Material.STONE, (byte) 43);
|
ItemStack item = new ItemStack(Material.STONE, (byte) 43);
|
||||||
@ -231,6 +232,14 @@ public class PlayerInit {
|
|||||||
player.sendMessage("Using item on block: " + useEvent.getItemStack().getMaterial() + " at " + useEvent.getPosition() + " on face " + useEvent.getBlockFace());
|
player.sendMessage("Using item on block: " + useEvent.getItemStack().getMaterial() + " at " + useEvent.getPosition() + " on face " + useEvent.getBlockFace());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
player.addEventCallback(ItemUpdateStateEvent.class, event -> {
|
||||||
|
System.out.println("ITEM UPDATE STATE");
|
||||||
|
});
|
||||||
|
|
||||||
|
player.addEventCallback(PlayerEatEvent.class, event -> {
|
||||||
|
System.out.println("PLAYER EAT EVENT");
|
||||||
|
});
|
||||||
|
|
||||||
player.addEventCallback(PlayerChunkUnloadEvent.class, event -> {
|
player.addEventCallback(PlayerChunkUnloadEvent.class, event -> {
|
||||||
Instance instance = player.getInstance();
|
Instance instance = player.getInstance();
|
||||||
|
|
||||||
|
@ -55,7 +55,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
// Velocity
|
// Velocity
|
||||||
protected Vector velocity = new Vector(); // Movement in block per second
|
protected Vector velocity = new Vector(); // Movement in block per second
|
||||||
protected float gravityDragPerTick;
|
protected float gravityDragPerTick;
|
||||||
private int gravityTickCounter;
|
|
||||||
|
|
||||||
private Set<Player> viewers = new CopyOnWriteArraySet<>();
|
private Set<Player> viewers = new CopyOnWriteArraySet<>();
|
||||||
private Data data;
|
private Data data;
|
||||||
|
@ -11,17 +11,16 @@ import net.minestom.server.entity.damage.DamageType;
|
|||||||
import net.minestom.server.entity.property.Attribute;
|
import net.minestom.server.entity.property.Attribute;
|
||||||
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
|
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
|
||||||
import net.minestom.server.event.item.ItemDropEvent;
|
import net.minestom.server.event.item.ItemDropEvent;
|
||||||
|
import net.minestom.server.event.item.ItemUpdateStateEvent;
|
||||||
import net.minestom.server.event.item.PickupExperienceEvent;
|
import net.minestom.server.event.item.PickupExperienceEvent;
|
||||||
import net.minestom.server.event.player.PlayerDisconnectEvent;
|
import net.minestom.server.event.player.*;
|
||||||
import net.minestom.server.event.player.PlayerRespawnEvent;
|
|
||||||
import net.minestom.server.event.player.PlayerSpawnEvent;
|
|
||||||
import net.minestom.server.event.player.PlayerTickEvent;
|
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.block.CustomBlock;
|
import net.minestom.server.instance.block.CustomBlock;
|
||||||
import net.minestom.server.inventory.Inventory;
|
import net.minestom.server.inventory.Inventory;
|
||||||
import net.minestom.server.inventory.PlayerInventory;
|
import net.minestom.server.inventory.PlayerInventory;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
|
import net.minestom.server.item.Material;
|
||||||
import net.minestom.server.network.packet.client.ClientPlayPacket;
|
import net.minestom.server.network.packet.client.ClientPlayPacket;
|
||||||
import net.minestom.server.network.packet.server.ServerPacket;
|
import net.minestom.server.network.packet.server.ServerPacket;
|
||||||
import net.minestom.server.network.packet.server.play.*;
|
import net.minestom.server.network.packet.server.play.*;
|
||||||
@ -73,12 +72,16 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
private int food;
|
private int food;
|
||||||
private float foodSaturation;
|
private float foodSaturation;
|
||||||
|
private long startEatingTime;
|
||||||
|
private long eatingTime = 1000L;
|
||||||
|
private boolean isEating;
|
||||||
|
|
||||||
|
// CustomBlock break delay
|
||||||
private CustomBlock targetCustomBlock;
|
private CustomBlock targetCustomBlock;
|
||||||
private BlockPosition targetBlockPosition;
|
private BlockPosition targetBlockPosition;
|
||||||
private long targetBlockTime;
|
private long targetBlockTime;
|
||||||
private byte targetLastStage;
|
private byte targetLastStage;
|
||||||
private int timeBreak;
|
private int blockBreakTime;
|
||||||
|
|
||||||
private Set<BossBar> bossBars = new CopyOnWriteArraySet<>();
|
private Set<BossBar> bossBars = new CopyOnWriteArraySet<>();
|
||||||
private Team team;
|
private Team team;
|
||||||
@ -149,7 +152,7 @@ public class Player extends LivingEntity {
|
|||||||
if (targetCustomBlock != null) {
|
if (targetCustomBlock != null) {
|
||||||
int animationCount = 10;
|
int animationCount = 10;
|
||||||
long since = System.currentTimeMillis() - targetBlockTime;
|
long since = System.currentTimeMillis() - targetBlockTime;
|
||||||
byte stage = (byte) (since / (timeBreak / animationCount));
|
byte stage = (byte) (since / (blockBreakTime / animationCount));
|
||||||
if (stage != targetLastStage) {
|
if (stage != targetLastStage) {
|
||||||
sendBlockBreakAnimation(targetBlockPosition, stage);
|
sendBlockBreakAnimation(targetBlockPosition, stage);
|
||||||
}
|
}
|
||||||
@ -182,6 +185,24 @@ public class Player extends LivingEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Eating animation
|
||||||
|
if (isEating()) {
|
||||||
|
if (System.currentTimeMillis() - startEatingTime >= eatingTime) {
|
||||||
|
refreshEating(false);
|
||||||
|
|
||||||
|
triggerStatus((byte) 9); // Mark item use as finished
|
||||||
|
ItemUpdateStateEvent itemUpdateStateEvent = callItemUpdateStateEvent(true);
|
||||||
|
|
||||||
|
ItemStack foodItem = itemUpdateStateEvent.getItemStack();
|
||||||
|
boolean isFood = foodItem.getMaterial().isFood();
|
||||||
|
|
||||||
|
if (isFood) {
|
||||||
|
PlayerEatEvent playerEatEvent = new PlayerEatEvent(foodItem);
|
||||||
|
callEvent(PlayerEatEvent.class, playerEatEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Tick event
|
// Tick event
|
||||||
callEvent(PlayerTickEvent.class, new PlayerTickEvent());
|
callEvent(PlayerTickEvent.class, new PlayerTickEvent());
|
||||||
|
|
||||||
@ -564,6 +585,19 @@ public class Player extends LivingEntity {
|
|||||||
sendUpdateHealthPacket();
|
sendUpdateHealthPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEating() {
|
||||||
|
return isEating;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to change the eating time animation
|
||||||
|
*
|
||||||
|
* @param eatingTime the eating time in milliseconds
|
||||||
|
*/
|
||||||
|
public void setEatingTime(long eatingTime) {
|
||||||
|
this.eatingTime = eatingTime;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean dropItem(ItemStack item) {
|
public boolean dropItem(ItemStack item) {
|
||||||
ItemDropEvent itemDropEvent = new ItemDropEvent(item);
|
ItemDropEvent itemDropEvent = new ItemDropEvent(item);
|
||||||
callEvent(ItemDropEvent.class, itemDropEvent);
|
callEvent(ItemDropEvent.class, itemDropEvent);
|
||||||
@ -995,17 +1029,50 @@ public class Player extends LivingEntity {
|
|||||||
public void refreshHeldSlot(short slot) {
|
public void refreshHeldSlot(short slot) {
|
||||||
this.heldSlot = slot;
|
this.heldSlot = slot;
|
||||||
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
|
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
|
||||||
|
|
||||||
|
refreshEating(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshOpenInventory(Inventory openInventory) {
|
public void refreshOpenInventory(Inventory openInventory) {
|
||||||
this.openInventory = openInventory;
|
this.openInventory = openInventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void refreshEating(boolean isEating) {
|
||||||
|
this.isEating = isEating;
|
||||||
|
if (isEating) {
|
||||||
|
this.startEatingTime = System.currentTimeMillis();
|
||||||
|
} else {
|
||||||
|
this.startEatingTime = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemUpdateStateEvent callItemUpdateStateEvent(boolean allowFood) {
|
||||||
|
Material mainHandMat = Material.fromId(getItemInMainHand().getMaterialId());
|
||||||
|
Material offHandMat = Material.fromId(getItemInOffHand().getMaterialId());
|
||||||
|
boolean isOffhand = offHandMat.hasState();
|
||||||
|
|
||||||
|
ItemStack updatedItem = isOffhand ? getItemInOffHand() :
|
||||||
|
mainHandMat.hasState() ? getItemInMainHand() : null;
|
||||||
|
if (updatedItem == null) // No item with state, cancel
|
||||||
|
return null;
|
||||||
|
|
||||||
|
boolean isFood = updatedItem.getMaterial().isFood();
|
||||||
|
|
||||||
|
if (isFood && !allowFood)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(updatedItem,
|
||||||
|
isOffhand ? Hand.OFF : Hand.MAIN);
|
||||||
|
callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent);
|
||||||
|
|
||||||
|
return itemUpdateStateEvent;
|
||||||
|
}
|
||||||
|
|
||||||
public void refreshTargetBlock(CustomBlock targetCustomBlock, BlockPosition targetBlockPosition, int breakTime) {
|
public void refreshTargetBlock(CustomBlock targetCustomBlock, BlockPosition targetBlockPosition, int breakTime) {
|
||||||
this.targetCustomBlock = targetCustomBlock;
|
this.targetCustomBlock = targetCustomBlock;
|
||||||
this.targetBlockPosition = targetBlockPosition;
|
this.targetBlockPosition = targetBlockPosition;
|
||||||
this.targetBlockTime = targetBlockPosition == null ? 0 : System.currentTimeMillis();
|
this.targetBlockTime = targetBlockPosition == null ? 0 : System.currentTimeMillis();
|
||||||
this.timeBreak = breakTime;
|
this.blockBreakTime = breakTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetTargetBlock() {
|
public void resetTargetBlock() {
|
||||||
|
@ -1,21 +1,28 @@
|
|||||||
package net.minestom.server.event.item;
|
package net.minestom.server.event.item;
|
||||||
|
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.event.Event;
|
import net.minestom.server.event.Event;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
|
|
||||||
public class ItemUpdateStateEvent extends Event {
|
public class ItemUpdateStateEvent extends Event {
|
||||||
|
|
||||||
private ItemStack itemStack;
|
private ItemStack itemStack;
|
||||||
|
private Player.Hand hand;
|
||||||
private boolean handAnimation;
|
private boolean handAnimation;
|
||||||
|
|
||||||
public ItemUpdateStateEvent(ItemStack itemStack) {
|
public ItemUpdateStateEvent(ItemStack itemStack, Player.Hand hand) {
|
||||||
this.itemStack = itemStack;
|
this.itemStack = itemStack;
|
||||||
|
this.hand = hand;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack getItemStack() {
|
public ItemStack getItemStack() {
|
||||||
return itemStack;
|
return itemStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Player.Hand getHand() {
|
||||||
|
return hand;
|
||||||
|
}
|
||||||
|
|
||||||
public void setHandAnimation(boolean handAnimation) {
|
public void setHandAnimation(boolean handAnimation) {
|
||||||
this.handAnimation = handAnimation;
|
this.handAnimation = handAnimation;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package net.minestom.server.event.player;
|
||||||
|
|
||||||
|
import net.minestom.server.event.Event;
|
||||||
|
import net.minestom.server.item.ItemStack;
|
||||||
|
|
||||||
|
public class PlayerEatEvent extends Event {
|
||||||
|
|
||||||
|
private ItemStack foodItem;
|
||||||
|
|
||||||
|
public PlayerEatEvent(ItemStack foodItem) {
|
||||||
|
this.foodItem = foodItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getFoodItem() {
|
||||||
|
return foodItem;
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,6 @@ import net.minestom.server.instance.block.Block;
|
|||||||
import net.minestom.server.instance.block.CustomBlock;
|
import net.minestom.server.instance.block.CustomBlock;
|
||||||
import net.minestom.server.inventory.PlayerInventory;
|
import net.minestom.server.inventory.PlayerInventory;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
import net.minestom.server.item.Material;
|
|
||||||
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
|
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
|
||||||
import net.minestom.server.network.packet.server.play.AcknowledgePlayerDiggingPacket;
|
import net.minestom.server.network.packet.server.play.AcknowledgePlayerDiggingPacket;
|
||||||
import net.minestom.server.network.packet.server.play.EntityEffectPacket;
|
import net.minestom.server.network.packet.server.play.EntityEffectPacket;
|
||||||
@ -52,7 +51,7 @@ public class PlayerDiggingListener {
|
|||||||
}
|
}
|
||||||
addEffect(player);
|
addEffect(player);
|
||||||
} else {
|
} else {
|
||||||
if(Block.fromId(instance.getBlockId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ())).breaksInstantaneously()) {
|
if (Block.fromId(instance.getBlockId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ())).breaksInstantaneously()) {
|
||||||
if (player.getCustomBlockTarget() != null) {
|
if (player.getCustomBlockTarget() != null) {
|
||||||
player.resetTargetBlock();
|
player.resetTargetBlock();
|
||||||
removeEffect(player);
|
removeEffect(player);
|
||||||
@ -100,20 +99,18 @@ public class PlayerDiggingListener {
|
|||||||
dropItem(player, droppedItemStack2, handItem);
|
dropItem(player, droppedItemStack2, handItem);
|
||||||
break;
|
break;
|
||||||
case UPDATE_ITEM_STATE:
|
case UPDATE_ITEM_STATE:
|
||||||
Material mainHandMat = Material.fromId(mainHand.getMaterialId());
|
player.refreshEating(false);
|
||||||
Material offHandMat = Material.fromId(offHand.getMaterialId());
|
ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false);
|
||||||
boolean isOffhand = offHandMat.hasState();
|
|
||||||
|
|
||||||
ItemStack updatedItem = isOffhand ? offHand :
|
if (itemUpdateStateEvent == null) {
|
||||||
mainHandMat.hasState() ? mainHand : null;
|
player.refreshActiveHand(true, false, false);
|
||||||
if (updatedItem == null) // No item with state, cancel
|
player.sendPacketToViewers(player.getMetadataPacket());
|
||||||
return;
|
} else {
|
||||||
|
boolean isOffHand = itemUpdateStateEvent.getHand() == Player.Hand.OFF;
|
||||||
|
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffHand, false);
|
||||||
|
player.sendPacketToViewers(player.getMetadataPacket());
|
||||||
|
}
|
||||||
|
|
||||||
ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(updatedItem);
|
|
||||||
player.callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent);
|
|
||||||
|
|
||||||
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffhand, false);
|
|
||||||
player.sendPacketToViewers(player.getMetadataPacket());
|
|
||||||
break;
|
break;
|
||||||
case SWAP_ITEM_HAND:
|
case SWAP_ITEM_HAND:
|
||||||
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(offHand.clone(), mainHand.clone());
|
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(offHand.clone(), mainHand.clone());
|
||||||
|
@ -77,6 +77,7 @@ public class UseItemListener {
|
|||||||
armAnimationEvent = new ArmAnimationEvent(ArmAnimationEvent.ArmAnimationType.TRIDENT);
|
armAnimationEvent = new ArmAnimationEvent(ArmAnimationEvent.ArmAnimationType.TRIDENT);
|
||||||
} else if (material.isFood()) {
|
} else if (material.isFood()) {
|
||||||
armAnimationEvent = new ArmAnimationEvent(ArmAnimationEvent.ArmAnimationType.EAT);
|
armAnimationEvent = new ArmAnimationEvent(ArmAnimationEvent.ArmAnimationType.EAT);
|
||||||
|
player.refreshEating(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (armAnimationEvent != null)
|
if (armAnimationEvent != null)
|
||||||
|
@ -15,7 +15,7 @@ public class ClientPacketsHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ClientPacket getPacketInstance(int id) {
|
public ClientPacket getPacketInstance(int id) {
|
||||||
//System.out.println("RECEIVED PACKET 0x" + Integer.toHexString(id));
|
// System.out.println("RECEIVED PACKET 0x" + Integer.toHexString(id));
|
||||||
if (id > SIZE)
|
if (id > SIZE)
|
||||||
throw new IllegalStateException("Packet ID 0x" + Integer.toHexString(id) + " has been tried to be parsed, debug needed");
|
throw new IllegalStateException("Packet ID 0x" + Integer.toHexString(id) + " has been tried to be parsed, debug needed");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user