diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java index 80bb1fb1fe..1135e36189 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java @@ -39,6 +39,7 @@ import net.fabricmc.fabric.impl.networking.GenericFutureListenerHolder; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; import net.fabricmc.fabric.mixin.networking.client.accessor.ClientLoginNetworkHandlerAccessor; public final class ClientLoginNetworkAddon extends AbstractNetworkAddon { @@ -57,7 +58,7 @@ public ClientLoginNetworkAddon(ClientLoginNetworkHandler handler, MinecraftClien public boolean handlePacket(LoginQueryRequestS2CPacket packet) { PacketByteBufLoginQueryRequestPayload payload = (PacketByteBufLoginQueryRequestPayload) packet.payload(); - return handlePacket(packet.queryId(), packet.payload().id(), payload.data()); + return handlePacket(packet.queryId(), packet.payload().id(), PayloadHelper.reset(payload.data())); } private boolean handlePacket(int queryId, Identifier channelName, PacketByteBuf buf) { diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java index 55735a4451..bb2bf06f29 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java @@ -36,6 +36,7 @@ import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; import net.fabricmc.fabric.impl.networking.NetworkingImpl; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; public final class ClientPlayNetworkAddon extends AbstractChanneledNetworkAddon { private final ClientPlayNetworkHandler handler; @@ -85,7 +86,7 @@ public void onServerReady() { */ public boolean handle(PacketByteBufPayload payload) { try { - return this.handle(payload.id(), payload.data()); + return this.handle(payload.id(), PayloadHelper.reset(payload.data())); } finally { payload.data().release(); } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/LoginQueryRequestS2CPacketFactory.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/LoginQueryRequestS2CPacketFactory.java index 31a89bc384..9ab1f29b34 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/LoginQueryRequestS2CPacketFactory.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/LoginQueryRequestS2CPacketFactory.java @@ -21,8 +21,8 @@ import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket; import net.minecraft.util.Identifier; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; public class LoginQueryRequestS2CPacketFactory { public static LoginQueryRequestS2CPacket create(PacketByteBuf buf) { @@ -30,8 +30,6 @@ public static LoginQueryRequestS2CPacket create(PacketByteBuf buf) { } private static LoginQueryRequestPayload readPayload(Identifier id, PacketByteBuf buf) { - PacketByteBuf copy = PacketByteBufs.copy(buf); - buf.skipBytes(buf.readableBytes()); - return new PacketByteBufLoginQueryRequestPayload(id, copy); + return new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf)); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java index 95fa6c3ea7..2e50bc9e3c 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java @@ -23,6 +23,6 @@ public record PacketByteBufLoginQueryRequestPayload(Identifier id, PacketByteBuf data) implements LoginQueryRequestPayload { @Override public void write(PacketByteBuf buf) { - buf.writeBytes(this.data()); + PayloadHelper.write(buf, data()); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java index c3fe21dbae..a7875efca7 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java @@ -22,6 +22,6 @@ public record PacketByteBufLoginQueryResponse(PacketByteBuf data) implements LoginQueryResponse { @Override public void write(PacketByteBuf buf) { - buf.writeBytes(this.data()); + PayloadHelper.write(buf, data()); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufPayload.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufPayload.java index 5a9890698b..78ac1cc39f 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufPayload.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufPayload.java @@ -23,6 +23,6 @@ public record PacketByteBufPayload(Identifier id, PacketByteBuf data) implements CustomPayload { @Override public void write(PacketByteBuf buf) { - buf.writeBytes(this.data()); + PayloadHelper.write(buf, data()); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java new file mode 100644 index 0000000000..f3c8b188e3 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java @@ -0,0 +1,23 @@ +package net.fabricmc.fabric.impl.networking.payload; + +import net.minecraft.network.PacketByteBuf; + +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; + +public class PayloadHelper { + public static void write(PacketByteBuf byteBuf, PacketByteBuf data) { + byteBuf.writeBytes(data.copy()); + } + + public static PacketByteBuf read(PacketByteBuf byteBuf) { + PacketByteBuf newBuf = PacketByteBufs.create(); + newBuf.writeBytes(byteBuf.copy()); + return newBuf; + } + + public static PacketByteBuf reset(PacketByteBuf byteBuf) { +// byteBuf.resetReaderIndex(); +//// byteBuf.release(); + return byteBuf; + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java index bfd3cba4c7..e3346577e2 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java @@ -48,6 +48,7 @@ import net.fabricmc.fabric.impl.networking.GenericFutureListenerHolder; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; import net.fabricmc.fabric.mixin.networking.accessor.ServerLoginNetworkHandlerAccessor; public final class ServerLoginNetworkAddon extends AbstractNetworkAddon implements PacketSender { @@ -130,7 +131,7 @@ private void sendCompressionPacket() { */ public boolean handle(LoginQueryResponseC2SPacket packet) { PacketByteBufLoginQueryResponse response = (PacketByteBufLoginQueryResponse) packet.response(); - return handle(packet.queryId(), response.data()); + return handle(packet.queryId(), response == null ? null : PayloadHelper.reset(response.data())); } private boolean handle(int queryId, @Nullable PacketByteBuf originalBuf) { diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java index ceb6383bca..64e75e8399 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java @@ -33,6 +33,7 @@ import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; import net.fabricmc.fabric.impl.networking.NetworkingImpl; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonNetworkHandlerAccessor; public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon { @@ -75,7 +76,7 @@ public void onClientReady() { * @return true if the packet has been handled */ public boolean handle(PacketByteBufPayload payload) { - return this.handle(payload.id(), payload.data()); + return this.handle(payload.id(), PayloadHelper.reset(payload.data())); } @Override diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java index 75d63ac809..17bd8b1155 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java @@ -26,8 +26,8 @@ import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; import net.minecraft.util.Identifier; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; @Mixin(CustomPayloadC2SPacket.class) public class CustomPayloadC2SPacketMixin { @@ -37,8 +37,6 @@ public class CustomPayloadC2SPacketMixin { cancellable = true ) private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable cir) { - PacketByteBuf copy = PacketByteBufs.copy(buf); - buf.skipBytes(buf.readableBytes()); - cir.setReturnValue(new PacketByteBufPayload(id, copy)); + cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf))); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java index bafffb95b3..ed4599ed1c 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java @@ -26,8 +26,8 @@ import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; import net.minecraft.util.Identifier; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; @Mixin(CustomPayloadS2CPacket.class) public class CustomPayloadS2CPacketMixin { @@ -37,8 +37,6 @@ public class CustomPayloadS2CPacketMixin { cancellable = true ) private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable cir) { - PacketByteBuf copy = PacketByteBufs.copy(buf); - buf.skipBytes(buf.readableBytes()); - cir.setReturnValue(new PacketByteBufPayload(id, copy)); + cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf))); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java index 2c7896ab6d..6455a8eda1 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java @@ -25,15 +25,13 @@ import net.minecraft.network.packet.c2s.login.LoginQueryResponse; import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; +import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; @Mixin(LoginQueryResponseC2SPacket.class) public class LoginQueryResponseC2SPacketMixin { @Inject(method = "readResponse", at = @At("HEAD"), cancellable = true) private static void readResponse(int queryId, PacketByteBuf buf, CallbackInfoReturnable cir) { - PacketByteBuf copy = PacketByteBufs.copy(buf); - buf.skipBytes(buf.readableBytes()); - cir.setReturnValue(new PacketByteBufLoginQueryResponse(copy)); + cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf))); } }