From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Mon, 29 Mar 2021 09:07:25 +0200
Subject: [PATCH] Make sure to remove correct TE during TE tick

This looks like it can cause premature TE removal.

diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index b89cefc8890774dbc64fd6bddeb038d2ee36d485..4523bc1f49e7be248a47eeb599fa7b6550dbb08d 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -895,7 +895,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
                 //this.tileEntityList.remove(tileentity); // Paper - remove unused list
                 // Paper - prevent double chunk lookups
                 LevelChunk chunk; if ((chunk = this.getChunkIfLoaded(tileentity.getBlockPos())) != null) { // inlined contents of this.isLoaded(BlockPosition). Reuse the returned chunk instead of looking it up again
-                    chunk.removeBlockEntity(tileentity.getBlockPos());
+                    chunk.removeTileEntity(tileentity.getBlockPos(), tileentity); // Paper - remove correct TE
                 }
                 // Paper end
             }
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
index 9b76dc15417eef420804e5184a6d684e1137a746..a15c08be3e1bd0e7934175db6ae0684bbb05e249 100644
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
@@ -818,10 +818,18 @@ public class LevelChunk implements ChunkAccess {
 
     @Override
     public void removeBlockEntity(BlockPos pos) {
+        // Paper start - remove correct TE
+        removeTileEntity(pos, null);
+    }
+    public void removeTileEntity(BlockPos blockposition, BlockEntity match) {
+        // Paper end
         if (this.loaded || this.world.isClientSide()) {
-            BlockEntity tileentity = (BlockEntity) this.blockEntities.remove(pos);
+            // Paper start
+            BlockEntity tileentity = (BlockEntity) this.blockEntities.get(blockposition);
 
-            if (tileentity != null) {
+            if (tileentity != null && (match == null || match == tileentity)) {
+                this.blockEntities.remove(blockposition);
+                // Paper end
                 tileentity.setRemoved();
             }
         }