From 22c8236df4b78218806bfcef95c463df32d513d1 Mon Sep 17 00:00:00 2001 From: Sakura Ryoko Date: Tue, 11 Jun 2024 22:01:59 -0400 Subject: [PATCH] Payload Splitter Interface / IClientPayloadData --- .../masa/malilib/network/PayloadSplitter.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/fi/dy/masa/malilib/network/PayloadSplitter.java b/src/main/java/fi/dy/masa/malilib/network/PayloadSplitter.java index 265fc27988..1c95035866 100644 --- a/src/main/java/fi/dy/masa/malilib/network/PayloadSplitter.java +++ b/src/main/java/fi/dy/masa/malilib/network/PayloadSplitter.java @@ -5,18 +5,16 @@ import java.util.Map; import io.netty.buffer.Unpooled; import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.listener.ClientPlayPacketListener; -import net.minecraft.network.listener.PacketListener; import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; -import net.minecraft.util.Pair; /** * Network packet splitter code from QuickCarpet by skyrising * @author skyrising * + * Updated by Sakura to work with newer versions by changing the Reading Session keys, + * and using the HANDLER interface to send packets via the Payload system */ public class PayloadSplitter { @@ -27,7 +25,7 @@ public class PayloadSplitter public static final int DEFAULT_MAX_RECEIVE_SIZE_C2S = 1048576; public static final int DEFAULT_MAX_RECEIVE_SIZE_S2C = 67108864; - private static final Map, ReadingSession> READING_SESSIONS = new HashMap<>(); + private static final Map READING_SESSIONS = new HashMap<>(); public static boolean send(IPluginClientPlayHandler handler, PacketByteBuf packet, ClientPlayNetworkHandler networkHandler) { @@ -62,23 +60,23 @@ private static boolean send(IPluginClientPlayHandler PacketByteBuf receive(IPluginClientPlayHandler handler, - PacketByteBuf buf, - ClientPlayNetworkHandler networkHandler) + long key, + PacketByteBuf buf) { - return receive(handler.getPayloadChannel(), buf, DEFAULT_MAX_RECEIVE_SIZE_S2C, networkHandler); + return receive(handler.getPayloadChannel(), key, buf, DEFAULT_MAX_RECEIVE_SIZE_S2C); } @Nullable private static PacketByteBuf receive(Identifier channel, + long key, PacketByteBuf buf, - int maxLength, - ClientPlayPacketListener networkHandler) + int maxLength) { - Pair key = new Pair<>(networkHandler, channel); - - return READING_SESSIONS.computeIfAbsent(key, ReadingSession::new).receive(readPayload(buf), maxLength); + return READING_SESSIONS.computeIfAbsent(key, ReadingSession::new).receive(buf, maxLength); } + // Not needed + /* public static PacketByteBuf readPayload(PacketByteBuf byteBuf) { PacketByteBuf newBuf = new PacketByteBuf(Unpooled.buffer()); @@ -87,9 +85,9 @@ public static PacketByteBuf readPayload(PacketByteBuf byteBuf) return newBuf; } - /** + ** * Sends a packet type ID as a VarInt, and then the given Compound tag. - */ + * public static void sendPacketTypeAndCompound(IPluginClientPlayHandler handler, int packetType, NbtCompound data, ClientPlayNetworkHandler networkHandler) { PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); @@ -98,14 +96,24 @@ public static void sendPacketTypeAndCompound(IPluginCl send(handler, buf, networkHandler); } + */ + /** + * I had to fix the `Pair.of` key mappings, because they were removed from MC; + * So I made it into a pre-shared random session 'key' between client and server. + * Generated using 'long key = Random.create(Util.getMeasuringTimeMs()).nextLong();' + * - + * It can be shared to the receiving end via a separate packet; or it can just be + * generated randomly on the receiving end per an expected Reading Session. + * It needs to be stored and changed for every unique session. + */ private static class ReadingSession { - private final Pair key; + private final long key; private int expectedSize = -1; private PacketByteBuf received; - private ReadingSession(Pair key) + private ReadingSession(long key) { this.key = key; } @@ -113,6 +121,9 @@ private ReadingSession(Pair key) @Nullable private PacketByteBuf receive(PacketByteBuf data, int maxLength) { + data.readerIndex(0); + //data = PacketUtils.slice(data); + if (this.expectedSize < 0) { this.expectedSize = data.readVarInt();