Compare commits

...

3 commits

Author SHA1 Message Date
Alex
7d0bc4fa63
Merge 6efa950512 into 8150091888 2024-11-09 21:08:13 -03:00
Eclipse
8150091888
Fix equippable component writing (#868)
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
Deploy / build (push) Has been cancelled
2024-11-07 19:03:03 +08:00
AlexProgrammerDE
6efa950512 First version of NetworkCodec 2024-05-21 08:19:01 +02:00
5 changed files with 42 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

@ -158,6 +158,7 @@ public class ItemCodecHelper extends MinecraftCodecHelper {
}
this.writeNullable(buf, equippable.model(), this::writeResourceLocation);
this.writeNullable(buf, equippable.cameraOverlay(), this::writeResourceLocation);
this.writeNullable(buf, equippable.allowedEntities(), this::writeHolderSet);
buf.writeBoolean(equippable.dispensable());
buf.writeBoolean(equippable.swappable());

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