Handle Large Packets disconnecting client

If a players inventory is too big to send in a single packet,
split the inventory set into multiple packets instead.
This commit is contained in:
Aikar
2018-11-27 21:18:06 -05:00
parent ba71d372a5
commit 236dce4925
5 changed files with 126 additions and 15 deletions

View File

@@ -0,0 +1,22 @@
--- a/net/minecraft/network/protocol/Packet.java
+++ b/net/minecraft/network/protocol/Packet.java
@@ -11,6 +11,19 @@
void handle(T listener);
+ // Paper start
+ default boolean hasLargePacketFallback() {
+ return false;
+ }
+
+ /**
+ * override {@link #hasLargePacketFallback()} to return true when overriding in subclasses
+ */
+ default boolean packetTooLarge(net.minecraft.network.Connection manager) {
+ return false;
+ }
+ // Paper end
+
default boolean isSkippable() {
return false;
}

View File

@@ -0,0 +1,24 @@
--- a/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
+++ b/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
@@ -36,6 +36,21 @@
this.carriedItem = ItemStack.OPTIONAL_STREAM_CODEC.decode(buf);
}
+ // Paper start - Handle large packets disconnecting client
+ @Override
+ public boolean hasLargePacketFallback() {
+ return true;
+ }
+
+ @Override
+ public boolean packetTooLarge(net.minecraft.network.Connection manager) {
+ for (int i = 0 ; i < this.items.size() ; i++) {
+ manager.send(new ClientboundContainerSetSlotPacket(this.containerId, this.stateId, i, this.items.get(i)));
+ }
+ return true;
+ }
+ // Paper end - Handle large packets disconnecting client
+
private void write(RegistryFriendlyByteBuf buf) {
buf.writeContainerId(this.containerId);
buf.writeVarInt(this.stateId);

View File

@@ -1,5 +1,14 @@
--- a/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
+++ b/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
@@ -52,7 +52,7 @@
throw new RuntimeException("Can't read heightmap in packet for [" + x + ", " + z + "]");
} else {
int i = buf.readVarInt();
- if (i > 2097152) {
+ if (i > 2097152) { // Paper - diff on change - if this changes, update PacketEncoder
throw new RuntimeException("Chunk Packet trying to allocate too much memory on read.");
} else {
this.buffer = new byte[i];
@@ -154,6 +154,7 @@
CompoundTag compoundTag = blockEntity.getUpdateTag(blockEntity.getLevel().registryAccess());
BlockPos blockPos = blockEntity.getBlockPos();