This commit is contained in:
Alex 2024-11-09 21:08:13 -03:00 committed by GitHub
commit 7d0bc4fa63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 10 deletions

View file

@ -0,0 +1,25 @@
package org.geysermc.mcprotocollib.protocol.codec;
import io.netty.buffer.ByteBuf;
public record NetworkCodec<T>(Writer<T> writer, Reader<T> reader) {
public static <T> NetworkCodec<T> of(Writer<T> writer, Reader<T> 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<T> {
void write(T t, ByteBuf out, MinecraftCodecHelper helper);
}
public interface Reader<T> {
T read(ByteBuf in, MinecraftCodecHelper helper);
}
}

View file

@ -2,10 +2,22 @@ package org.geysermc.mcprotocollib.protocol.data.game;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.geysermc.mcprotocollib.protocol.codec.NetworkCodec;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public class KnownPack { public class KnownPack {
public static NetworkCodec<KnownPack> 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 namespace;
private String id; private String id;
private String version; private String version;

View file

@ -17,15 +17,11 @@ public class ClientboundSelectKnownPacks implements MinecraftPacket {
private final List<KnownPack> knownPacks; private final List<KnownPack> knownPacks;
public ClientboundSelectKnownPacks(ByteBuf in, MinecraftCodecHelper helper) { 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 @Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) { public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writeList(out, this.knownPacks, (buf, entry) -> { helper.writeList(out, this.knownPacks, (buf, entry) -> KnownPack.NETWORK_CODEC.write(entry, buf, helper));
helper.writeString(buf, entry.getNamespace());
helper.writeString(buf, entry.getId());
helper.writeString(buf, entry.getVersion());
});
} }
} }

View file

@ -22,7 +22,7 @@ public class ServerboundSelectKnownPacks implements MinecraftPacket {
int entryCount = Math.min(helper.readVarInt(in), 64); int entryCount = Math.min(helper.readVarInt(in), 64);
for (int i = 0; i < entryCount; i++) { 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()); helper.writeVarInt(out, this.knownPacks.size());
for (KnownPack entry : this.knownPacks) { for (KnownPack entry : this.knownPacks) {
helper.writeString(out, entry.getNamespace()); KnownPack.NETWORK_CODEC.write(entry, out, helper);
helper.writeString(out, entry.getId());
helper.writeString(out, entry.getVersion());
} }
} }
} }