First version of NetworkCodec

This commit is contained in:
AlexProgrammerDE 2024-05-20 18:17:56 +02:00
parent a724a8fdb4
commit 6efa950512
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.Data;
import org.geysermc.mcprotocollib.protocol.codec.NetworkCodec;
@Data
@AllArgsConstructor
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 id;
private String version;

View file

@ -17,15 +17,11 @@ public class ClientboundSelectKnownPacks implements MinecraftPacket {
private final List<KnownPack> 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));
}
}

View file

@ -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);
}
}
}