Skip to content

Commit 6dd20b1

Browse files
Staticify all serialization helpers (#878)
* Staticify all serialization helpers First of all, why am I changing the whole API so that MinecraftCodecHelper is now static instead of an instance? The reason is that these methods are better used statically. Right now the biggest issue with MCPL I see is its reliance on "serialization instances" that are being passed around everywhere. Why were those even made in the first place? Because there was a belief a few years ago that MCPL could somehow ever support multiple versions of Minecraft. The idea is good however it is unrealistic. Packets change heavily between versions and rarely do serialization types. Serialization instances were made with the idea somehow you could implement multiple codechelpers for each version and "downgrade" packets and bind them together using UnsupportedOperationException. In my opinion this is not a route MCPL should be taking. Instead my plan is to add a module that allows ViaVersion to be used with MCPL to allow the desired multi-version support. Without having to pass serialization instances around, the API becomes a lot simpler. Especially with the removal of the parent class of ItemCodecHelper. (Which previously extended MinecraftCodecHelper for some reason) Now it is clear what you're calling and there are way more lambda/inlining opportunities around the codebase. This makes the API more similar to the approach Velocity and ViaVersion are also taking with their static serialization methods. Classes renamed: - MinecraftCodecHelper -> MinecraftTypes - ItemCodecHelper -> ItemTypes Classes removed: - PacketCodecHelper interface (not needed anymore) - BasePacketCodec (merged into MinecraftTypes) Why was BasePacketCodec not renamed to BaseTypes? Because this library is slowly going to more and more specialize on only minecraft, not needing to retain a special implementation for developers who use it just for PacketLib. * Update comments
1 parent 7501bec commit 6dd20b1

File tree

268 files changed

+2906
-3101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+2906
-3101
lines changed

example/src/main/java/org/geysermc/mcprotocollib/network/example/PingPacket.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.geysermc.mcprotocollib.network.example;
22

33
import io.netty.buffer.ByteBuf;
4-
import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper;
54
import org.geysermc.mcprotocollib.network.packet.Packet;
5+
import org.geysermc.mcprotocollib.protocol.codec.MinecraftTypes;
66

77
public class PingPacket implements Packet {
88
private final String id;
99

10-
public PingPacket(ByteBuf buf, PacketCodecHelper codecHelper) {
11-
this.id = codecHelper.readString(buf);
10+
public PingPacket(ByteBuf buf) {
11+
this.id = MinecraftTypes.readString(buf);
1212
}
1313

1414
public PingPacket(String id) {

example/src/main/java/org/geysermc/mcprotocollib/network/example/TestProtocol.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import io.netty.buffer.ByteBuf;
44
import org.geysermc.mcprotocollib.network.Server;
55
import org.geysermc.mcprotocollib.network.Session;
6-
import org.geysermc.mcprotocollib.network.codec.BasePacketCodecHelper;
7-
import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper;
86
import org.geysermc.mcprotocollib.network.codec.PacketDefinition;
97
import org.geysermc.mcprotocollib.network.codec.PacketSerializer;
108
import org.geysermc.mcprotocollib.network.crypt.AESEncryption;
@@ -13,6 +11,7 @@
1311
import org.geysermc.mcprotocollib.network.packet.PacketHeader;
1412
import org.geysermc.mcprotocollib.network.packet.PacketProtocol;
1513
import org.geysermc.mcprotocollib.network.packet.PacketRegistry;
14+
import org.geysermc.mcprotocollib.protocol.codec.MinecraftTypes;
1615
import org.slf4j.Logger;
1716
import org.slf4j.LoggerFactory;
1817

@@ -33,20 +32,16 @@ public TestProtocol(SecretKey key) {
3332
this.setSecretKey(key);
3433
}
3534

36-
public PacketCodecHelper createHelper() {
37-
return new BasePacketCodecHelper();
38-
}
39-
4035
public void setSecretKey(SecretKey key) {
4136
registry.register(0, PingPacket.class, new PacketSerializer<>() {
4237
@Override
43-
public void serialize(ByteBuf buf, PacketCodecHelper helper, PingPacket packet) {
44-
helper.writeString(buf, packet.getPingId());
38+
public void serialize(ByteBuf buf, PingPacket packet) {
39+
MinecraftTypes.writeString(buf, packet.getPingId());
4540
}
4641

4742
@Override
48-
public PingPacket deserialize(ByteBuf buf, PacketCodecHelper helper, PacketDefinition<PingPacket, PacketCodecHelper> definition) {
49-
return new PingPacket(buf, helper);
43+
public PingPacket deserialize(ByteBuf buf, PacketDefinition<PingPacket> definition) {
44+
return new PingPacket(buf);
5045
}
5146
});
5247

protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import net.kyori.adventure.text.Component;
55
import org.checkerframework.checker.nullness.qual.NonNull;
66
import org.checkerframework.checker.nullness.qual.Nullable;
7-
import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper;
87
import org.geysermc.mcprotocollib.network.compression.CompressionConfig;
98
import org.geysermc.mcprotocollib.network.crypt.EncryptionConfig;
109
import org.geysermc.mcprotocollib.network.event.session.SessionEvent;
@@ -45,13 +44,6 @@ public interface Session {
4544
*/
4645
PacketProtocol getPacketProtocol();
4746

48-
/**
49-
* Gets the session's {@link PacketCodecHelper}.
50-
*
51-
* @return The session's packet codec helper.
52-
*/
53-
PacketCodecHelper getCodecHelper();
54-
5547
/**
5648
* Gets this session's set flags. If this session belongs to a server, the server's
5749
* flags will be included in the results.

protocol/src/main/java/org/geysermc/mcprotocollib/network/codec/BasePacketCodecHelper.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

protocol/src/main/java/org/geysermc/mcprotocollib/network/codec/PacketCodecHelper.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

protocol/src/main/java/org/geysermc/mcprotocollib/network/codec/PacketDefinition.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
*
1111
* @param <T> the packet type
1212
*/
13-
public class PacketDefinition<T extends Packet, H extends PacketCodecHelper> {
13+
public class PacketDefinition<T extends Packet> {
1414
private final int id;
1515
private final Class<T> packetClass;
16-
private final PacketSerializer<T, H> serializer;
16+
private final PacketSerializer<T> serializer;
1717

18-
public PacketDefinition(final int id, final Class<T> packetClass, final PacketSerializer<T, H> serializer) {
18+
public PacketDefinition(final int id, final Class<T> packetClass, final PacketSerializer<T> serializer) {
1919
this.id = id;
2020
this.packetClass = packetClass;
2121
this.serializer = serializer;
@@ -44,11 +44,11 @@ public Class<T> getPacketClass() {
4444
*
4545
* @return the packet serializer of the packet
4646
*/
47-
public PacketSerializer<T, H> getSerializer() {
47+
public PacketSerializer<T> getSerializer() {
4848
return this.serializer;
4949
}
5050

51-
public T newInstance(ByteBuf buf, H helper) {
52-
return this.serializer.deserialize(buf, helper, this);
51+
public T newInstance(ByteBuf buf) {
52+
return this.serializer.deserialize(buf, this);
5353
}
5454
}

protocol/src/main/java/org/geysermc/mcprotocollib/network/codec/PacketSerializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import io.netty.buffer.ByteBuf;
44
import org.geysermc.mcprotocollib.network.packet.Packet;
55

6-
public interface PacketSerializer<T extends Packet, H extends PacketCodecHelper> {
6+
public interface PacketSerializer<T extends Packet> {
77

8-
void serialize(ByteBuf buf, H helper, T packet);
8+
void serialize(ByteBuf buf, T packet);
99

10-
T deserialize(ByteBuf buf, H helper, PacketDefinition<T, H> definition);
10+
T deserialize(ByteBuf buf, PacketDefinition<T> definition);
1111
}

protocol/src/main/java/org/geysermc/mcprotocollib/network/netty/MinecraftChannelInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ protected void addHandlers(S session, Channel ch) {
3737
pipeline.addLast("write-timeout", new WriteTimeoutHandler(session.getFlag(BuiltinFlags.WRITE_TIMEOUT, 0)));
3838

3939
pipeline.addLast("encryption", new PacketEncryptorCodec());
40-
pipeline.addLast("sizer", new PacketSizerCodec(protocol.getPacketHeader(), session.getCodecHelper()));
41-
pipeline.addLast("compression", new PacketCompressionCodec(session.getCodecHelper()));
40+
pipeline.addLast("sizer", new PacketSizerCodec(protocol.getPacketHeader()));
41+
pipeline.addLast("compression", new PacketCompressionCodec());
4242

4343
pipeline.addLast("flow-control", new AutoReadFlowControlHandler());
4444
pipeline.addLast("codec", new PacketCodec(session, client));

protocol/src/main/java/org/geysermc/mcprotocollib/network/netty/PacketCodec.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.netty.handler.codec.EncoderException;
77
import io.netty.handler.codec.MessageToMessageCodec;
88
import org.geysermc.mcprotocollib.network.Session;
9-
import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper;
109
import org.geysermc.mcprotocollib.network.codec.PacketDefinition;
1110
import org.geysermc.mcprotocollib.network.event.session.PacketErrorEvent;
1211
import org.geysermc.mcprotocollib.network.packet.Packet;
@@ -40,14 +39,13 @@ public void encode(ChannelHandlerContext ctx, Packet packet, List<Object> out) {
4039

4140
PacketProtocol packetProtocol = this.session.getPacketProtocol();
4241
PacketRegistry packetRegistry = packetProtocol.getOutboundPacketRegistry();
43-
PacketCodecHelper codecHelper = this.session.getCodecHelper();
4442
try {
4543
int packetId = this.client ? packetRegistry.getServerboundId(packet) : packetRegistry.getClientboundId(packet);
4644
PacketDefinition definition = this.client ? packetRegistry.getServerboundDefinition(packetId) : packetRegistry.getClientboundDefinition(packetId);
4745

4846
ByteBuf buf = ctx.alloc().buffer();
49-
packetProtocol.getPacketHeader().writePacketId(buf, codecHelper, packetId);
50-
definition.getSerializer().serialize(buf, codecHelper, packet);
47+
packetProtocol.getPacketHeader().writePacketId(buf, packetId);
48+
definition.getSerializer().serialize(buf, packet);
5149

5250
out.add(buf);
5351

@@ -76,18 +74,17 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
7674

7775
PacketProtocol packetProtocol = this.session.getPacketProtocol();
7876
PacketRegistry packetRegistry = packetProtocol.getInboundPacketRegistry();
79-
PacketCodecHelper codecHelper = this.session.getCodecHelper();
8077
Packet packet = null;
8178
try {
82-
int id = packetProtocol.getPacketHeader().readPacketId(buf, codecHelper);
79+
int id = packetProtocol.getPacketHeader().readPacketId(buf);
8380
if (id == -1) {
8481
buf.readerIndex(initial);
8582
return;
8683
}
8784

8885
log.trace(marker, "Decoding packet with id: {}", id);
8986

90-
packet = this.client ? packetRegistry.createClientboundPacket(id, buf, codecHelper) : packetRegistry.createServerboundPacket(id, buf, codecHelper);
87+
packet = this.client ? packetRegistry.createClientboundPacket(id, buf) : packetRegistry.createServerboundPacket(id, buf);
9188

9289
if (buf.readableBytes() > 0) {
9390
throw new IllegalStateException("Packet \"" + packet.getClass().getSimpleName() + "\" not fully read.");

protocol/src/main/java/org/geysermc/mcprotocollib/network/netty/PacketCompressionCodec.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import io.netty.handler.codec.MessageToMessageCodec;
77
import lombok.RequiredArgsConstructor;
88
import org.geysermc.mcprotocollib.network.NetworkConstants;
9-
import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper;
109
import org.geysermc.mcprotocollib.network.compression.CompressionConfig;
10+
import org.geysermc.mcprotocollib.protocol.codec.MinecraftTypes;
1111

1212
import java.util.List;
1313

1414
@RequiredArgsConstructor
1515
public class PacketCompressionCodec extends MessageToMessageCodec<ByteBuf, ByteBuf> {
1616
private static final int MAX_UNCOMPRESSED_SIZE = 8 * 1024 * 1024; // 8MiB
17-
private final PacketCodecHelper helper;
1817

1918
@Override
2019
public void handlerRemoved(ChannelHandlerContext ctx) {
@@ -42,10 +41,10 @@ public void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) {
4241
ByteBuf outBuf = ctx.alloc().directBuffer(uncompressed);
4342
if (uncompressed < config.threshold()) {
4443
// Under the threshold, there is nothing to do.
45-
this.helper.writeVarInt(outBuf, 0);
44+
MinecraftTypes.writeVarInt(outBuf, 0);
4645
outBuf.writeBytes(msg);
4746
} else {
48-
this.helper.writeVarInt(outBuf, uncompressed);
47+
MinecraftTypes.writeVarInt(outBuf, uncompressed);
4948
config.compression().deflate(msg, outBuf);
5049
}
5150

@@ -60,7 +59,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
6059
return;
6160
}
6261

63-
int claimedUncompressedSize = this.helper.readVarInt(in);
62+
int claimedUncompressedSize = MinecraftTypes.readVarInt(in);
6463
if (claimedUncompressedSize == 0) {
6564
out.add(in.retain());
6665
return;

0 commit comments

Comments
 (0)