mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 22:27:32 +00:00
Generate Packet Chunks for Large Packets
This commit is contained in:
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;
|
||||
|
||||
@@ -17,6 +17,7 @@ import noppes.npcs.CustomNpcs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class PacketHandler {
|
||||
public static PacketHandler Instance;
|
||||
@@ -116,48 +117,39 @@ public final class PacketHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToPlayer(AbstractPacket packet, EntityPlayerMP player) {
|
||||
public void sendPacket(AbstractPacket packet, Consumer<FMLProxyPacket> sendFunction) {
|
||||
try {
|
||||
FMLProxyPacket proxyPacket = packet.generatePacket();
|
||||
getEventChannel(packet).sendTo(proxyPacket, player);
|
||||
if (packet instanceof LargeAbstractPacket) {
|
||||
List<FMLProxyPacket> chunks = ((LargeAbstractPacket) packet).generatePacketChunks();
|
||||
for (FMLProxyPacket chunk : chunks) {
|
||||
sendFunction.accept(chunk);
|
||||
}
|
||||
} else {
|
||||
FMLProxyPacket proxyPacket = packet.generatePacket();
|
||||
sendFunction.accept(proxyPacket);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToPlayer(AbstractPacket packet, EntityPlayerMP player) {
|
||||
sendPacket(packet, proxyPacket -> getEventChannel(packet).sendTo(proxyPacket, player));
|
||||
}
|
||||
|
||||
public void sendToServer(AbstractPacket packet) {
|
||||
try {
|
||||
FMLProxyPacket proxyPacket = packet.generatePacket();
|
||||
getEventChannel(packet).sendToServer(proxyPacket);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sendPacket(packet, proxyPacket -> getEventChannel(packet).sendToServer(proxyPacket));
|
||||
}
|
||||
|
||||
public void sendToAll(AbstractPacket packet) {
|
||||
try {
|
||||
FMLProxyPacket proxyPacket = packet.generatePacket();
|
||||
getEventChannel(packet).sendToAll(proxyPacket);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sendPacket(packet, proxyPacket -> getEventChannel(packet).sendToAll(proxyPacket));
|
||||
}
|
||||
|
||||
public void sendToAllAround(AbstractPacket packet, NetworkRegistry.TargetPoint point) {
|
||||
try {
|
||||
FMLProxyPacket proxyPacket = packet.generatePacket();
|
||||
getEventChannel(packet).sendToAllAround(proxyPacket, point);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sendPacket(packet, proxyPacket -> getEventChannel(packet).sendToAllAround(proxyPacket, point));
|
||||
}
|
||||
|
||||
public void sendToDimension(AbstractPacket packet, int dimensionId) {
|
||||
try {
|
||||
FMLProxyPacket proxyPacket = packet.generatePacket();
|
||||
getEventChannel(packet).sendToDimension(proxyPacket, dimensionId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sendPacket(packet, proxyPacket -> getEventChannel(packet).sendToDimension(proxyPacket, dimensionId));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user