diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NetworkCodec.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NetworkCodec.java new file mode 100644 index 00000000..72838ec6 --- /dev/null +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NetworkCodec.java @@ -0,0 +1,25 @@ +package org.geysermc.mcprotocollib.protocol.codec; + +import io.netty.buffer.ByteBuf; + +public record NetworkCodec(Writer writer, Reader reader) { + public static NetworkCodec of(Writer writer, Reader reader) { + return new NetworkCodec<>(writer, reader); + } + + public void write(T t, ByteBuf byteBuf, MinecraftCodecHelper helper) { + writer.write(t, byteBuf, helper); + } + + public T read(ByteBuf byteBuf, MinecraftCodecHelper helper) { + return reader.read(byteBuf, helper); + } + + public interface Writer { + void write(T t, ByteBuf out, MinecraftCodecHelper helper); + } + + public interface Reader { + T read(ByteBuf in, MinecraftCodecHelper helper); + } +} diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/KnownPack.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/KnownPack.java index f8079e45..48ea8998 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/KnownPack.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/KnownPack.java @@ -2,10 +2,22 @@ package org.geysermc.mcprotocollib.protocol.data.game; import lombok.AllArgsConstructor; import lombok.Data; +import org.geysermc.mcprotocollib.protocol.codec.NetworkCodec; @Data @AllArgsConstructor public class KnownPack { + public static NetworkCodec NETWORK_CODEC = NetworkCodec.of((knownPack, out, helper) -> { + helper.writeString(out, knownPack.getNamespace()); + helper.writeString(out, knownPack.getId()); + helper.writeString(out, knownPack.getVersion()); + }, (in, helper) -> { + String namespace = helper.readString(in); + String id = helper.readString(in); + String version = helper.readString(in); + return new KnownPack(namespace, id, version); + }); + private String namespace; private String id; private String version; diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundSelectKnownPacks.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundSelectKnownPacks.java index e5dfa96f..ced57ff5 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundSelectKnownPacks.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundSelectKnownPacks.java @@ -17,15 +17,11 @@ public class ClientboundSelectKnownPacks implements MinecraftPacket { private final List knownPacks; public ClientboundSelectKnownPacks(ByteBuf in, MinecraftCodecHelper helper) { - this.knownPacks = helper.readList(in, buf -> new KnownPack(helper.readString(buf), helper.readString(buf), helper.readString(buf))); + this.knownPacks = helper.readList(in, buf -> KnownPack.NETWORK_CODEC.read(buf, helper)); } @Override public void serialize(ByteBuf out, MinecraftCodecHelper helper) { - helper.writeList(out, this.knownPacks, (buf, entry) -> { - helper.writeString(buf, entry.getNamespace()); - helper.writeString(buf, entry.getId()); - helper.writeString(buf, entry.getVersion()); - }); + helper.writeList(out, this.knownPacks, (buf, entry) -> KnownPack.NETWORK_CODEC.write(entry, buf, helper)); } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundSelectKnownPacks.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundSelectKnownPacks.java index d8cfd6d7..66fbcacc 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundSelectKnownPacks.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundSelectKnownPacks.java @@ -22,7 +22,7 @@ public class ServerboundSelectKnownPacks implements MinecraftPacket { int entryCount = Math.min(helper.readVarInt(in), 64); for (int i = 0; i < entryCount; i++) { - this.knownPacks.add(new KnownPack(helper.readString(in), helper.readString(in), helper.readString(in))); + this.knownPacks.add(KnownPack.NETWORK_CODEC.read(in, helper)); } } @@ -34,9 +34,7 @@ public class ServerboundSelectKnownPacks implements MinecraftPacket { helper.writeVarInt(out, this.knownPacks.size()); for (KnownPack entry : this.knownPacks) { - helper.writeString(out, entry.getNamespace()); - helper.writeString(out, entry.getId()); - helper.writeString(out, entry.getVersion()); + KnownPack.NETWORK_CODEC.write(entry, out, helper); } } }