chore(config-network): align packet serialization with recommendations in blog post
ci/woodpecker/push/docs Pipeline was successful Details
ci/woodpecker/push/jfmod Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2024-04-25 09:53:04 +02:00
parent 1109f9e610
commit 00929dd17a
Signed by: Johannes
GPG Key ID: E76429612C2929F4
4 changed files with 33 additions and 41 deletions

View File

@ -4,19 +4,11 @@ import io.gitlab.jfronny.libjf.config.impl.network.RequestRouter;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
public record ConfigurationCompletePacket() implements CustomPayload {
public static final CustomPayload.Id<ConfigurationCompletePacket> ID = CustomPayload.id(RequestRouter.MOD_ID + ":handshake_complete");
public static final PacketCodec<ByteBuf, ConfigurationCompletePacket> CODEC = new PacketCodec<>() {
@Override
public ConfigurationCompletePacket decode(ByteBuf buf) {
return new ConfigurationCompletePacket();
}
@Override
public void encode(ByteBuf buf, ConfigurationCompletePacket value) {
}
};
public static final CustomPayload.Id<ConfigurationCompletePacket> ID = new CustomPayload.Id<>(new Identifier(RequestRouter.MOD_ID, "handshake_complete"));
public static final PacketCodec<ByteBuf, ConfigurationCompletePacket> CODEC = PacketCodec.unit(new ConfigurationCompletePacket());
@Override
public Id<? extends CustomPayload> getId() {

View File

@ -5,9 +5,10 @@ import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
public record ConfigurationPacket(int version) implements CustomPayload {
public static final CustomPayload.Id<ConfigurationPacket> ID = CustomPayload.id(RequestRouter.MOD_ID + ":handshake");
public static final CustomPayload.Id<ConfigurationPacket> ID = new CustomPayload.Id<>(new Identifier(RequestRouter.MOD_ID, "handshake"));
public static final PacketCodec<ByteBuf, ConfigurationPacket> CODEC = PacketCodecs.INTEGER.xmap(ConfigurationPacket::new, ConfigurationPacket::version);
@Override

View File

@ -4,27 +4,30 @@ import io.gitlab.jfronny.libjf.config.impl.network.RequestRouter;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.Function;
public record RequestPacket(long request, @Nullable Long parent, String name, PacketByteBuf aux) implements CustomPayload {
public static final Id<RequestPacket> ID = CustomPayload.id(RequestRouter.MOD_ID + ":request");
public static final PacketCodec<PacketByteBuf, RequestPacket> CODEC = PacketCodec.of(
(value, buf) -> {
buf.writeLong(value.request);
buf.writeBoolean(value.parent != null);
if (value.parent != null) buf.writeLong(value.parent);
buf.writeString(value.name);
buf.writeBytes(value.aux);
},
buf -> {
long request = buf.readLong();
Long parent = buf.readBoolean() ? buf.readLong() : null;
String name = buf.readString();
PacketByteBuf aux = PacketByteBufs.copy(buf);
return new RequestPacket(request, parent, name, aux);
}
);
public static final Id<RequestPacket> ID = new CustomPayload.Id<>(new Identifier(RequestRouter.MOD_ID, "request"));
public static final PacketCodec<PacketByteBuf, RequestPacket> CODEC = PacketCodec.tuple(
PacketCodecs.VAR_LONG, RequestPacket::request,
PacketCodecs.optional(PacketCodecs.VAR_LONG), RequestPacket::optionalParent,
PacketCodecs.STRING, RequestPacket::name,
PacketCodec.of(PacketByteBuf::writeBytes, PacketByteBufs::copy), RequestPacket::aux,
RequestPacket::new);
private RequestPacket(long request, Optional<Long> parent, String name, PacketByteBuf aux) {
this(request, parent.orElse(null), name, aux);
}
private Optional<Long> optionalParent() {
return Optional.ofNullable(parent);
}
@Override
public Id<? extends CustomPayload> getId() {

View File

@ -4,21 +4,17 @@ import io.gitlab.jfronny.libjf.config.impl.network.RequestRouter;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
public record ResponsePacket(long request, int status, PacketByteBuf aux) implements CustomPayload {
public static final CustomPayload.Id<ResponsePacket> ID = CustomPayload.id(RequestRouter.MOD_ID + ":response");
public static final PacketCodec<PacketByteBuf, ResponsePacket> CODEC = PacketCodec.of(
(value, buf) -> {
buf.writeLong(value.request());
buf.writeInt(value.status());
buf.writeBytes(value.aux());
},
buf -> {
long request = buf.readLong();
int status = buf.readInt();
return new ResponsePacket(request, status, PacketByteBufs.copy(buf));
}
public static final CustomPayload.Id<ResponsePacket> ID = new CustomPayload.Id<>(new Identifier(RequestRouter.MOD_ID, "response"));
public static final PacketCodec<PacketByteBuf, ResponsePacket> CODEC = PacketCodec.tuple(
PacketCodecs.VAR_LONG, ResponsePacket::request,
PacketCodecs.INTEGER, ResponsePacket::status,
PacketCodec.of(PacketByteBuf::writeBytes, PacketByteBufs::copy), ResponsePacket::aux,
ResponsePacket::new
);
@Override