From 038943911a6eb14fa8751c1e963bf7f55f05c44f Mon Sep 17 00:00:00 2001 From: PetteriM1 <26197131+PetteriM1@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:52:24 +0200 Subject: [PATCH] Implement AnimateEntityPacket --- src/main/java/cn/nukkit/entity/Entity.java | 7 ++ .../network/protocol/AnimateEntityPacket.java | 77 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/cn/nukkit/network/protocol/AnimateEntityPacket.java diff --git a/src/main/java/cn/nukkit/entity/Entity.java b/src/main/java/cn/nukkit/entity/Entity.java index aee160534be..5b8dd04de39 100644 --- a/src/main/java/cn/nukkit/entity/Entity.java +++ b/src/main/java/cn/nukkit/entity/Entity.java @@ -2525,4 +2525,11 @@ public int hashCode() { hash = (int) (29 * hash + this.getId()); return hash; } + + public void playAnimation(String animation) { + AnimateEntityPacket animateEntityPacket = new AnimateEntityPacket(); + animateEntityPacket.animation = animation; + animateEntityPacket.runtimeEntityIds.add(this.id); + Server.broadcastPacket(this.hasSpawned.values(), animateEntityPacket); + } } diff --git a/src/main/java/cn/nukkit/network/protocol/AnimateEntityPacket.java b/src/main/java/cn/nukkit/network/protocol/AnimateEntityPacket.java new file mode 100644 index 00000000000..03e0ecf849d --- /dev/null +++ b/src/main/java/cn/nukkit/network/protocol/AnimateEntityPacket.java @@ -0,0 +1,77 @@ +package cn.nukkit.network.protocol; + +import it.unimi.dsi.fastutil.longs.LongArrayList; +import it.unimi.dsi.fastutil.longs.LongList; +import lombok.ToString; + +/** + * Used to trigger an entity animation on the specified runtime IDs to the client that receives it. + * Source: ... + * Default values from ... + */ +@ToString +public class AnimateEntityPacket extends DataPacket { + + public static final byte NETWORK_ID = ProtocolInfo.ANIMATE_ENTITY_PACKET; + + /** + * Name of the to play on the entities specified in {@link #runtimeEntityIds} + */ + public String animation; + + /** + * The entity state to move to when the animation has finished playing. + */ + public String nextState = "default"; + + /** + * Expression to check if the animation needs to stop. + */ + public String stopExpression = "query.any_animation_finished"; + + /** + * The molang stop expression version + */ + public int stopExpressionVersion; + + /** + * Name of the animation controller to use. + */ + public String controller = "__runtime_controller"; + + /** + * Time taken to blend out of the specified animation. + */ + public float blendOutTime = 0f; + + /** + * Entity runtime IDs to run the animation on when sent to the client. + */ + public final LongList runtimeEntityIds = new LongArrayList(); + + @Override + public void decode() { + } + + @Override + public void encode() { + this.reset(); + + this.putString(this.animation); + this.putString(this.nextState); + this.putString(this.stopExpression); + this.putLInt(this.stopExpressionVersion); + this.putString(this.controller); + this.putLFloat(this.blendOutTime); + + this.putUnsignedVarInt(this.runtimeEntityIds.size()); + for (long runtimeId : this.runtimeEntityIds) { + this.putUnsignedVarLong(runtimeId); + } + } + + @Override + public byte pid() { + return NETWORK_ID; + } +}