mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2024-11-14 11:24:57 -05:00
Merge 6efa950512
into 8150091888
This commit is contained in:
commit
7d0bc4fa63
4 changed files with 41 additions and 10 deletions
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
|
@ -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());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue