Skip to content

Commit

Permalink
Payload Splitter Interface / IClientPayloadData
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jun 12, 2024
1 parent 2f2f0bb commit 22c8236
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/main/java/fi/dy/masa/malilib/network/PayloadSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<Pair<PacketListener, Identifier>, ReadingSession> READING_SESSIONS = new HashMap<>();
private static final Map<Long, ReadingSession> READING_SESSIONS = new HashMap<>();

public static <T extends CustomPayload> boolean send(IPluginClientPlayHandler<T> handler, PacketByteBuf packet, ClientPlayNetworkHandler networkHandler)
{
Expand Down Expand Up @@ -62,23 +60,23 @@ private static <T extends CustomPayload> boolean send(IPluginClientPlayHandler<T
}

public static <T extends CustomPayload> PacketByteBuf receive(IPluginClientPlayHandler<T> 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<PacketListener, Identifier> 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());
Expand All @@ -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 <T extends CustomPayload> void sendPacketTypeAndCompound(IPluginClientPlayHandler<T> handler, int packetType, NbtCompound data, ClientPlayNetworkHandler networkHandler)
{
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
Expand All @@ -98,21 +96,34 @@ public static <T extends CustomPayload> 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<PacketListener, Identifier> key;
private final long key;
private int expectedSize = -1;
private PacketByteBuf received;

private ReadingSession(Pair<PacketListener, Identifier> key)
private ReadingSession(long key)
{
this.key = key;
}

@Nullable
private PacketByteBuf receive(PacketByteBuf data, int maxLength)
{
data.readerIndex(0);
//data = PacketUtils.slice(data);

if (this.expectedSize < 0)
{
this.expectedSize = data.readVarInt();
Expand Down

0 comments on commit 22c8236

Please sign in to comment.