ClientboundAddEntityPacket 0x01

ClientboundAddEntityPacket: Minecraft 26.3 packet (0x01 Play clientbound). Wire name 'add_entity', fields: entity, serverEntity, handled

Minecraft 26.3PlayClientboundnet.minecraft.network.protocol.game

Protocol slots (1)

IDdecStateDirectionRegistry
0x011PLAYClientboundGamePacketTypes.CLIENTBOUND_ADD_ENTITY: "add_entity"

Packet ids are positional: the id is the index of the addPacket call in the protocol builder (see ProtocolInfoBuilder.listPackets). Ids change between game versions.

Wire fields (2)

FieldType
entityEntity
serverEntityServerEntity

Stream codec

static final StreamCodec<RegistryFriendlyByteBuf, ClientboundAddEntityPacket> STREAM_CODEC = Packet.codec( ClientboundAddEntityPacket::write, ClientboundAddEntityPacket::new );

Modern packets serialize through StreamCodecs instead of read/write methods.

Handling

Resolved on the ClientGamePacketListener packet-listener interface by void handleAddEntity(ClientboundAddEntityPacket clientboundaddentitypacket). The implementation in the game is ClientPacketListener.

Full Source

ClientboundAddEntityPacket.java176 lines · Minecraft 26.3 · verbatimSource page ↗
1
package net.minecraft.network.protocol.game;
2
3
import java.util.UUID;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.registries.Registries;
6
import net.minecraft.network.RegistryFriendlyByteBuf;
7
import net.minecraft.network.codec.ByteBufCodecs;
8
import net.minecraft.network.codec.StreamCodec;
9
import net.minecraft.network.protocol.Packet;
10
import net.minecraft.network.protocol.PacketType;
11
import net.minecraft.server.level.ServerEntity;
12
import net.minecraft.util.Mth;
13
import net.minecraft.world.entity.Entity;
14
import net.minecraft.world.entity.EntityType;
15
import net.minecraft.world.phys.Vec3;
16
17
public class ClientboundAddEntityPacket implements Packet<ClientGamePacketListener> {
18
	public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundAddEntityPacket> STREAM_CODEC = Packet.codec(
19
		ClientboundAddEntityPacket::write, ClientboundAddEntityPacket::new
20
	);
21
	private final int id;
22
	private final UUID uuid;
23
	private final EntityType<?> type;
24
	private final double x;
25
	private final double y;
26
	private final double z;
27
	private final Vec3 movement;
28
	private final byte xRot;
29
	private final byte yRot;
30
	private final byte yHeadRot;
31
	private final int data;
32
33
	public ClientboundAddEntityPacket(final Entity entity, final ServerEntity serverEntity) {
34
		this(entity, serverEntity, 0);
35
	}
36
37
	public ClientboundAddEntityPacket(final Entity entity, final ServerEntity serverEntity, final int data) {
38
		this(
39
			entity.getId(),
40
			entity.getUUID(),
41
			serverEntity.getPositionBase().x(),
42
			serverEntity.getPositionBase().y(),
43
			serverEntity.getPositionBase().z(),
44
			serverEntity.getLastSentXRot(),
45
			serverEntity.getLastSentYRot(),
46
			entity.getType(),
47
			data,
48
			serverEntity.getLastSentMovement(),
49
			serverEntity.getLastSentYHeadRot()
50
		);
51
	}
52
53
	public ClientboundAddEntityPacket(final Entity entity, final int data, final BlockPos pos) {
54
		this(
55
			entity.getId(),
56
			entity.getUUID(),
57
			pos.getX(),
58
			pos.getY(),
59
			pos.getZ(),
60
			entity.getXRot(),
61
			entity.getYRot(),
62
			entity.getType(),
63
			data,
64
			entity.getDeltaMovement(),
65
			entity.getYHeadRot()
66
		);
67
	}
68
69
	public ClientboundAddEntityPacket(
70
		final int id,
71
		final UUID uuid,
72
		final double x,
73
		final double y,
74
		final double z,
75
		final float xRot,
76
		final float yRot,
77
		final EntityType<?> type,
78
		final int data,
79
		final Vec3 movement,
80
		final double yHeadRot
81
	) {
82
		this.id = id;
83
		this.uuid = uuid;
84
		this.x = x;
85
		this.y = y;
86
		this.z = z;
87
		this.movement = movement;
88
		this.xRot = Mth.packDegrees(xRot);
89
		this.yRot = Mth.packDegrees(yRot);
90
		this.yHeadRot = Mth.packDegrees((float)yHeadRot);
91
		this.type = type;
92
		this.data = data;
93
	}
94
95
	private ClientboundAddEntityPacket(final RegistryFriendlyByteBuf input) {
96
		this.id = input.readVarInt();
97
		this.uuid = input.readUUID();
98
		this.type = ByteBufCodecs.registry(Registries.ENTITY_TYPE).decode(input);
99
		this.x = input.readDouble();
100
		this.y = input.readDouble();
101
		this.z = input.readDouble();
102
		this.movement = Vec3.LP_STREAM_CODEC.decode(input);
103
		this.xRot = input.readByte();
104
		this.yRot = input.readByte();
105
		this.yHeadRot = input.readByte();
106
		this.data = input.readVarInt();
107
	}
108
109
	private void write(final RegistryFriendlyByteBuf output) {
110
		output.writeVarInt(this.id);
111
		output.writeUUID(this.uuid);
112
		ByteBufCodecs.registry(Registries.ENTITY_TYPE).encode(output, this.type);
113
		output.writeDouble(this.x);
114
		output.writeDouble(this.y);
115
		output.writeDouble(this.z);
116
		Vec3.LP_STREAM_CODEC.encode(output, this.movement);
117
		output.writeByte(this.xRot);
118
		output.writeByte(this.yRot);
119
		output.writeByte(this.yHeadRot);
120
		output.writeVarInt(this.data);
121
	}
122
123
	@Override
124
	public PacketType<ClientboundAddEntityPacket> type() {
125
		return GamePacketTypes.CLIENTBOUND_ADD_ENTITY;
126
	}
127
128
	public void handle(final ClientGamePacketListener listener) {
129
		listener.handleAddEntity(this);
130
	}
131
132
	public int getId() {
133
		return this.id;
134
	}
135
136
	public UUID getUUID() {
137
		return this.uuid;
138
	}
139
140
	public EntityType<?> getType() {
141
		return this.type;
142
	}
143
144
	public double getX() {
145
		return this.x;
146
	}
147
148
	public double getY() {
149
		return this.y;
150
	}
151
152
	public double getZ() {
153
		return this.z;
154
	}
155
156
	public Vec3 getMovement() {
157
		return this.movement;
158
	}
159
160
	public float getXRot() {
161
		return Mth.unpackDegrees(this.xRot);
162
	}
163
164
	public float getYRot() {
165
		return Mth.unpackDegrees(this.yRot);
166
	}
167
168
	public float getYHeadRot() {
169
		return Mth.unpackDegrees(this.yHeadRot);
170
	}
171
172
	public int getData() {
173
		return this.data;
174
	}
175
}
176