From 61236ef43ba0902146448eaab955f2d7e54ba775 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Tue, 15 Jun 2021 21:53:50 -0700
Subject: [PATCH] add more patches back

---
 build-data/paper.at                           |  3 ++
 .../Fix-MC-117075-TE-Unload-Lag-Spike.patch   | 42 +++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 patches/server/Fix-MC-117075-TE-Unload-Lag-Spike.patch

diff --git a/build-data/paper.at b/build-data/paper.at
index 76c0fda459..850c04ac8a 100644
--- a/build-data/paper.at
+++ b/build-data/paper.at
@@ -177,3 +177,6 @@ public net.minecraft.util.thread.BlockableEventLoop runAllTasks()V
 # Chunk debug command
 public net.minecraft.server.level.ChunkMap entitiesInLevel
 public net.minecraft.server.level.ServerLevel players
+
+# Improve block entity unload performance
+public net.minecraft.Util$IdentityStrategy
diff --git a/patches/server/Fix-MC-117075-TE-Unload-Lag-Spike.patch b/patches/server/Fix-MC-117075-TE-Unload-Lag-Spike.patch
new file mode 100644
index 0000000000..d3eb17f0d8
--- /dev/null
+++ b/patches/server/Fix-MC-117075-TE-Unload-Lag-Spike.patch
@@ -0,0 +1,42 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: mezz <tehgeek@gmail.com>
+Date: Wed, 9 Aug 2017 17:51:22 -0500
+Subject: [PATCH] Fix MC-117075: TE Unload Lag Spike
+
+
+diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/world/level/Level.java
++++ b/src/main/java/net/minecraft/world/level/Level.java
+@@ -0,0 +0,0 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
+         // Spigot start
+         // Iterator iterator = this.blockEntityTickers.iterator();
+         int tilesThisCycle = 0;
++        it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet<TickingBlockEntity> toRemove = new it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet(net.minecraft.Util.IdentityStrategy.INSTANCE); // Paper - use removeAll
++        toRemove.add(null);
+         for (tileTickPosition = 0; tileTickPosition < this.blockEntityTickers.size(); tileTickPosition++) { // Paper - Disable tick limiters
+             this.tileTickPosition = (this.tileTickPosition < this.blockEntityTickers.size()) ? this.tileTickPosition : 0;
+             TickingBlockEntity tickingblockentity = (TickingBlockEntity) this.blockEntityTickers.get(tileTickPosition);
+@@ -0,0 +0,0 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
+             if (tickingblockentity == null) {
+                 this.getCraftServer().getLogger().severe("Spigot has detected a null entity and has removed it, preventing a crash");
+                 tilesThisCycle--;
+-                this.blockEntityTickers.remove(this.tileTickPosition--);
+                 continue;
+             }
+             // Spigot end
+@@ -0,0 +0,0 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
+             if (tickingblockentity.isRemoved()) {
+                 // Spigot start
+                 tilesThisCycle--;
+-                this.blockEntityTickers.remove(this.tileTickPosition--);
++                toRemove.add(tickingblockentity); // Paper - use removeAll
+                 // Spigot end
+             } else {
+                 tickingblockentity.tick();
+             }
+         }
++        this.blockEntityTickers.removeAll(toRemove);
+ 
+         timings.tileEntityTick.stopTiming(); // Spigot
+         this.tickingBlockEntities = false;