Skip to content

Commit 534f23c

Browse files
committed
feat: clientbound spawn entity living packet
1 parent 5123aa6 commit 534f23c

File tree

6 files changed

+126
-5
lines changed

6 files changed

+126
-5
lines changed

vanilla-protocol/src/main/java/by/milansky/protocol/vanilla/registry/VanillaStateRegistry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ private VanillaStateRegistry() {
4040
}));
4141

4242
registerClientboundState(ProtocolState.PLAY, BaseSuppliedPacketRegistry.create(registry -> {
43+
registry.register(ClientboundSpawnEntityLiving.class,
44+
UnmodifiableVersionMapping.createMapping(VanillaProtocolVersion.MINECRAFT_1_8, 15,
45+
VanillaProtocolVersion.MINECRAFT_1_9, 3, VanillaProtocolVersion.MINECRAFT_1_16, 2,
46+
VanillaProtocolVersion.MINECRAFT_1_19));
47+
4348
registry.register(ClientboundUpsertPlayerInfo.class,
4449
UnmodifiableVersionMapping.createMapping(VanillaProtocolVersion.MINECRAFT_1_8, 0x38,
4550
VanillaProtocolVersion.MINECRAFT_1_9, 0x2D, VanillaProtocolVersion.MINECRAFT_1_12_1, 0x2E,

vanilla-protocol/src/main/java/by/milansky/protocol/vanilla/standard/ClientboundCompression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void encode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersio
3131
}
3232

3333
@Override
34-
public void decode(@NotNull ByteBuf byteBuf, @NotNull ProtocolVersion version) {
34+
public void decode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
3535
threshold = byteBuf.readVarInt();
3636
}
3737
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package by.milansky.protocol.vanilla.standard;
2+
3+
import by.milansky.protocol.api.packet.Packet;
4+
import by.milansky.protocol.api.version.ProtocolVersion;
5+
import by.milansky.protocol.vanilla.utility.ProtocolUtility;
6+
import by.milansky.protocol.vanilla.version.VanillaProtocolVersion;
7+
import io.netty.buffer.ByteBuf;
8+
import lombok.AccessLevel;
9+
import lombok.AllArgsConstructor;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
import lombok.experimental.Accessors;
13+
import lombok.experimental.ExtensionMethod;
14+
import lombok.experimental.FieldDefaults;
15+
import org.jetbrains.annotations.NotNull;
16+
17+
import java.util.UUID;
18+
19+
/**
20+
* @author milansky
21+
*/
22+
@Data
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
@Accessors(fluent = true, chain = true)
26+
@ExtensionMethod({ProtocolUtility.class})
27+
@FieldDefaults(level = AccessLevel.PRIVATE)
28+
public final class ClientboundSpawnEntityLiving implements Packet {
29+
private static final double POSITION_FACTOR = 32.0;
30+
private static final float ROTATION_FACTOR = 256.0F / 360.0F;
31+
private static final double VELOCITY_FACTOR = 8000.0;
32+
33+
int entityID;
34+
UUID entityUUID;
35+
int entityTypeID;
36+
double positionX, positionY, positionZ;
37+
float yaw, pitch, headPitch;
38+
double velocityX, velocityY, velocityZ;
39+
40+
@Override
41+
public void encode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
42+
byteBuf.writeVarInt(entityID);
43+
if (version.lower(VanillaProtocolVersion.MINECRAFT_1_9)) {
44+
byteBuf.writeByte(entityTypeID & 255);
45+
byteBuf.writeInt((int) Math.floor(positionX * POSITION_FACTOR));
46+
byteBuf.writeInt((int) Math.floor(positionY * POSITION_FACTOR));
47+
byteBuf.writeInt((int) Math.floor(positionZ * POSITION_FACTOR));
48+
} else {
49+
byteBuf.writeUuid(entityUUID);
50+
51+
if (version.greaterEqual(VanillaProtocolVersion.MINECRAFT_1_11)) {
52+
byteBuf.writeVarInt(entityTypeID);
53+
} else {
54+
byteBuf.writeByte(entityTypeID & 255);
55+
}
56+
57+
byteBuf.writeDouble(positionX);
58+
byteBuf.writeDouble(positionY);
59+
byteBuf.writeDouble(positionZ);
60+
}
61+
62+
byteBuf.writeByte((int) (yaw * ROTATION_FACTOR));
63+
byteBuf.writeByte((int) (pitch * ROTATION_FACTOR));
64+
byteBuf.writeByte((int) (headPitch * ROTATION_FACTOR));
65+
66+
byteBuf.writeShort((int) (velocityX * VELOCITY_FACTOR));
67+
byteBuf.writeShort((int) (velocityY * VELOCITY_FACTOR));
68+
byteBuf.writeShort((int) (velocityZ * VELOCITY_FACTOR));
69+
70+
if (version.lower(VanillaProtocolVersion.MINECRAFT_1_15))
71+
byteBuf.writeEmptyEntityMetadata(version);
72+
}
73+
74+
@Override
75+
public void decode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
76+
this.entityID = byteBuf.readVarInt();
77+
if (version.lower(VanillaProtocolVersion.MINECRAFT_1_9)) {
78+
this.entityUUID = new UUID(0L, 0L);
79+
this.entityTypeID = byteBuf.readByte() & 255;
80+
81+
this.positionX = byteBuf.readInt() / POSITION_FACTOR;
82+
this.positionY = byteBuf.readInt() / POSITION_FACTOR;
83+
this.positionZ = byteBuf.readInt() / POSITION_FACTOR;
84+
} else {
85+
this.entityUUID = byteBuf.readUuid();
86+
if (version.greaterEqual(VanillaProtocolVersion.MINECRAFT_1_11)) {
87+
this.entityTypeID = byteBuf.readVarInt();
88+
} else {
89+
this.entityTypeID = byteBuf.readUnsignedByte();
90+
}
91+
92+
this.positionX = byteBuf.readDouble();
93+
this.positionY = byteBuf.readDouble();
94+
this.positionZ = byteBuf.readDouble();
95+
}
96+
this.yaw = byteBuf.readByte() / ROTATION_FACTOR;
97+
this.pitch = byteBuf.readByte() / ROTATION_FACTOR;
98+
this.headPitch = byteBuf.readByte() / ROTATION_FACTOR;
99+
100+
this.velocityX = byteBuf.readShort() / VELOCITY_FACTOR;
101+
this.velocityY = byteBuf.readShort() / VELOCITY_FACTOR;
102+
this.velocityZ = byteBuf.readShort() / VELOCITY_FACTOR;
103+
104+
if (version.lower(VanillaProtocolVersion.MINECRAFT_1_15)) {
105+
byteBuf.skipBytes(byteBuf.readableBytes());
106+
}
107+
}
108+
}

vanilla-protocol/src/main/java/by/milansky/protocol/vanilla/standard/ClientboundTeam.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class ClientboundTeam implements Packet {
3636
String[] players;
3737

3838
@Override
39-
public void encode(@NotNull ByteBuf byteBuf, @NotNull ProtocolVersion version) {
39+
public void encode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
4040
byteBuf.writeString(name);
4141
byteBuf.writeByte(mode.ordinal());
4242

@@ -71,7 +71,7 @@ public void encode(@NotNull ByteBuf byteBuf, @NotNull ProtocolVersion version) {
7171
}
7272

7373
@Override
74-
public void decode(@NotNull ByteBuf byteBuf, @NotNull ProtocolVersion version) {
74+
public void decode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
7575
name = byteBuf.readString();
7676
mode = TeamMode.VALUES[byteBuf.readByte()];
7777

vanilla-protocol/src/main/java/by/milansky/protocol/vanilla/standard/ClientboundUpdateHealth.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public final class ClientboundUpdateHealth implements Packet {
2727
int food;
2828

2929
@Override
30-
public void encode(@NotNull ByteBuf byteBuf, @NotNull ProtocolVersion version) {
30+
public void encode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
3131
byteBuf.writeFloat(health);
3232
byteBuf.writeVarInt(food);
3333
byteBuf.writeFloat(foodSaturation);
3434
}
3535

3636
@Override
37-
public void decode(@NotNull ByteBuf byteBuf, @NotNull ProtocolVersion version) {
37+
public void decode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) {
3838
health = byteBuf.readFloat();
3939
food = byteBuf.readVarInt();
4040
foodSaturation = byteBuf.readFloat();

vanilla-protocol/src/main/java/by/milansky/protocol/vanilla/utility/ProtocolUtility.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ public static Property[] readProperties(ByteBuf buf) {
441441
return properties;
442442
}
443443

444+
public static void writeEmptyEntityMetadata(final @NotNull ByteBuf buf, final @NotNull ProtocolVersion version) {
445+
if (version.greaterEqual(VanillaProtocolVersion.MINECRAFT_1_9)) {
446+
buf.writeByte(255);
447+
} else {
448+
buf.writeByte(127);
449+
}
450+
}
451+
444452
public static BinaryTag serializeBinaryTag(final JsonElement json) {
445453
if (json instanceof JsonPrimitive jsonPrimitive) {
446454
if (jsonPrimitive.isNumber()) {

0 commit comments

Comments
 (0)