Generate Packet Chunks for Large Packets

This commit is contained in:
Kam committed 2025-01-29 13:28:14 -05:00
1 parent 5d41f5b939
commit 36c8b70eb4
2 files changed
+49 -27

No files matched your search

@@ -1,12 +1,15 @@
package kamkeel.npcs.network;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayer;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.ArrayList;
public abstract class LargeAbstractPacket extends AbstractPacket {
private static final int CHUNK_SIZE = 10000;
@@ -55,6 +58,33 @@ public abstract class LargeAbstractPacket extends AbstractPacket {
}
}
public final List<FMLProxyPacket> generatePacketChunks() {
List<FMLProxyPacket> chunks = new ArrayList<>();
ByteBuf data = Unpooled.buffer();
try {
writeData(data);
int totalSize = data.readableBytes();
UUID packetId = UUID.randomUUID();
int totalChunks = (int) Math.ceil((double) totalSize / CHUNK_SIZE);
for (int i = 0; i < totalSize; i += CHUNK_SIZE) {
int chunkSize = Math.min(CHUNK_SIZE, totalSize - i);
ByteBuf chunk = data.readBytes(chunkSize);
ByteBuf out = Unpooled.buffer();
out.writeLong(packetId.getMostSignificantBits());
out.writeLong(packetId.getLeastSignificantBits());
out.writeInt(totalSize);
out.writeInt(i);
out.writeInt(totalChunks);
out.writeBytes(chunk);
chunks.add(new FMLProxyPacket(out, getChannel().getChannelName()));
}
} catch (IOException e) {
e.printStackTrace();
}
return chunks;
}
public abstract void writeData(ByteBuf out) throws IOException;
public abstract void handleData(ByteBuf data, EntityPlayer player) throws IOException;