Skip to content

Commit

Permalink
Implement AnimateEntityPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriM1 authored Feb 26, 2024
1 parent 995bf29 commit 0389439
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
77 changes: 77 additions & 0 deletions src/main/java/cn/nukkit/network/protocol/AnimateEntityPacket.java
Original file line number Diff line number Diff line change
@@ -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: <a href="https://github.com/CloudburstMC/Protocol">...</a>
* Default values from <a href="https://wiki.vg/Bedrock_Protocol">...</a>
*/
@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;
}
}

0 comments on commit 0389439

Please sign in to comment.