From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 6 May 2020 05:00:57 -0400
Subject: [PATCH] Handle Oversized Tile Entities in chunks

Splits out Extra Packets if too many TE's are encountered to prevent
creating too large of a packet to sed.

Co authored by Spottedleaf

diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
@@ -0,0 +0,0 @@ public class ClientboundLevelChunkPacket implements Packet<ClientGamePacketListe
     private final int[] biomes;
     private final byte[] buffer;
     private final List<CompoundTag> blockEntitiesTags;
+    // Paper start
+    private final java.util.List<Packet> extraPackets = new java.util.ArrayList<>();
+    private static final int TE_LIMIT = Integer.getInteger("Paper.excessiveTELimit", 750);
+
+    @Override
+    public java.util.List<Packet> getExtraPackets() {
+        return extraPackets;
+    }
+    // Paper end
 
     public ClientboundLevelChunkPacket(LevelChunk chunk) {
         ChunkPos chunkPos = chunk.getPos();
@@ -0,0 +0,0 @@ public class ClientboundLevelChunkPacket implements Packet<ClientGamePacketListe
         this.buffer = new byte[this.calculateChunkSize(chunk)];
         this.availableSections = this.extractChunkData(new FriendlyByteBuf(this.getWriteBuffer()), chunk);
         this.blockEntitiesTags = Lists.newArrayList();
+        int totalTileEntities = 0; // Paper
 
         for(Entry<BlockPos, BlockEntity> entry2 : chunk.getBlockEntities().entrySet()) {
             BlockEntity blockEntity = entry2.getValue();
+            // Paper start - improve oversized chunk data packet handling
+            if (++totalTileEntities > TE_LIMIT) {
+                ClientboundBlockEntityDataPacket updatePacket = blockEntity.getUpdatePacket();
+                if (updatePacket != null) {
+                    this.extraPackets.add(updatePacket);
+                    continue;
+                }
+            }
+            // Paper end
             CompoundTag compoundTag = blockEntity.getUpdateTag();
             if (blockEntity instanceof net.minecraft.world.level.block.entity.SkullBlockEntity) { net.minecraft.world.level.block.entity.SkullBlockEntity.sanitizeTileEntityUUID(compoundTag); } // Paper
             this.blockEntitiesTags.add(compoundTag);
