From 832687de704f645ddfd5e9cbc7a667c878ef8f14 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 12 Apr 2020 16:50:50 -0400
Subject: [PATCH] Restore preventing saving bad entities patch to full effect

---
 ...revent-Saving-Bad-entities-to-chunks.patch | 31 ++++++++++++----
 ...381-Fix-World-isChunkGenerated-calls.patch | 24 ++++++-------
 ...91-Asynchronous-chunk-IO-and-loading.patch | 36 ++++++++++---------
 3 files changed, 57 insertions(+), 34 deletions(-)

diff --git a/Spigot-Server-Patches/0261-Prevent-Saving-Bad-entities-to-chunks.patch b/Spigot-Server-Patches/0261-Prevent-Saving-Bad-entities-to-chunks.patch
index 6d6d17bd3d..4011da1138 100644
--- a/Spigot-Server-Patches/0261-Prevent-Saving-Bad-entities-to-chunks.patch
+++ b/Spigot-Server-Patches/0261-Prevent-Saving-Bad-entities-to-chunks.patch
@@ -1,4 +1,4 @@
-From 372bb534db9407f2f0a153378e0390e995d6ec8f Mon Sep 17 00:00:00 2001
+From 0e1e8bae2d4aa584614af1937310c93dd0384a88 Mon Sep 17 00:00:00 2001
 From: Aikar <aikar@aikar.co>
 Date: Thu, 26 Jul 2018 00:11:12 -0400
 Subject: [PATCH] Prevent Saving Bad entities to chunks
@@ -18,7 +18,7 @@ an invalid entity.
 This should reduce log occurrences of dupe uuid messages.
 
 diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
-index 4af5a230ba..6371f2f5b1 100644
+index 4af5a230b..83bcef7e6 100644
 --- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
 +++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
 @@ -300,6 +300,7 @@ public class ChunkRegionLoader {
@@ -29,18 +29,17 @@ index 4af5a230ba..6371f2f5b1 100644
          if (ichunkaccess.getChunkStatus().getType() == ChunkStatus.Type.LEVELCHUNK) {
              Chunk chunk = (Chunk) ichunkaccess;
  
-@@ -311,13 +312,29 @@ public class ChunkRegionLoader {
+@@ -311,13 +312,28 @@ public class ChunkRegionLoader {
                  while (iterator1.hasNext()) {
                      Entity entity = (Entity) iterator1.next();
                      NBTTagCompound nbttagcompound4 = new NBTTagCompound();
 -
 +                    // Paper start
 +                    if ((int) Math.floor(entity.locX()) >> 4 != chunk.getPos().x || (int) Math.floor(entity.locZ()) >> 4 != chunk.getPos().z) {
-+                        LogManager.getLogger().warn(entity + " is not in this chunk, skipping save. This a bug fix to a vanilla bug. Do not report this to PaperMC please.");
 +                        toUpdate.add(entity);
 +                        continue;
 +                    }
-+                    if (entity.dead) {
++                    if (entity.dead || hasPlayerPassenger(entity)) {
 +                        continue;
 +                    }
 +                    // Paper end
@@ -53,13 +52,33 @@ index 4af5a230ba..6371f2f5b1 100644
 +
 +            // Paper start - move entities to the correct chunk
 +            for (Entity entity : toUpdate) {
-+                worldserver.entityJoinedWorld(entity);
++                worldserver.chunkCheck(entity);
 +            }
 +            // Paper end
 +
          } else {
              ProtoChunk protochunk = (ProtoChunk) ichunkaccess;
  
+@@ -373,6 +389,19 @@ public class ChunkRegionLoader {
+         nbttagcompound1.set("Structures", a(chunkcoordintpair, ichunkaccess.h(), ichunkaccess.v()));
+         return nbttagcompound;
+     }
++    // Paper start - this is saved with the player
++    private static boolean hasPlayerPassenger(Entity entity) {
++        for (Entity passenger : entity.passengers) {
++            if (passenger instanceof EntityPlayer) {
++                return true;
++            }
++            if (hasPlayerPassenger(passenger)) {
++                return true;
++            }
++        }
++        return false;
++    }
++    // Paper end
+ 
+     public static ChunkStatus.Type a(@Nullable NBTTagCompound nbttagcompound) {
+         if (nbttagcompound != null) {
 -- 
 2.25.1
 
diff --git a/Spigot-Server-Patches/0381-Fix-World-isChunkGenerated-calls.patch b/Spigot-Server-Patches/0381-Fix-World-isChunkGenerated-calls.patch
index 8fbff22c8f..926453ad86 100644
--- a/Spigot-Server-Patches/0381-Fix-World-isChunkGenerated-calls.patch
+++ b/Spigot-Server-Patches/0381-Fix-World-isChunkGenerated-calls.patch
@@ -1,4 +1,4 @@
-From e0e23d9f89865a8a275e2565116251f127b2e325 Mon Sep 17 00:00:00 2001
+From 900495214e6cb77bb3b25372a9f98b4226cf9883 Mon Sep 17 00:00:00 2001
 From: Spottedleaf <Spottedleaf@users.noreply.github.com>
 Date: Sat, 15 Jun 2019 08:54:33 -0700
 Subject: [PATCH] Fix World#isChunkGenerated calls
@@ -8,7 +8,7 @@ This patch also adds a chunk status cache on region files (note that
 its only purpose is to cache the status on DISK)
 
 diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
-index 462b224dfe..b582171c51 100644
+index 462b224df..b582171c5 100644
 --- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
 +++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
 @@ -28,7 +28,7 @@ public class ChunkProviderServer extends IChunkProvider {
@@ -43,12 +43,12 @@ index 462b224dfe..b582171c51 100644
  
      @Nullable
 diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
-index 6371f2f5b1..961228e9df 100644
+index 83bcef7e6..47e074a12 100644
 --- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
 +++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
-@@ -391,6 +391,17 @@ public class ChunkRegionLoader {
-         return nbttagcompound;
+@@ -403,6 +403,17 @@ public class ChunkRegionLoader {
      }
+     // Paper end
  
 +    // Paper start
 +    public static ChunkStatus getStatus(NBTTagCompound compound) {
@@ -65,7 +65,7 @@ index 6371f2f5b1..961228e9df 100644
          if (nbttagcompound != null) {
              ChunkStatus chunkstatus = ChunkStatus.a(nbttagcompound.getCompound("Level").getString("Status"));
 diff --git a/src/main/java/net/minecraft/server/ChunkStatus.java b/src/main/java/net/minecraft/server/ChunkStatus.java
-index efdf611e66..134a4f0b7d 100644
+index efdf611e6..134a4f0b7 100644
 --- a/src/main/java/net/minecraft/server/ChunkStatus.java
 +++ b/src/main/java/net/minecraft/server/ChunkStatus.java
 @@ -176,6 +176,7 @@ public class ChunkStatus {
@@ -95,7 +95,7 @@ index efdf611e66..134a4f0b7d 100644
          return (ChunkStatus) IRegistry.CHUNK_STATUS.get(MinecraftKey.a(s));
      }
 diff --git a/src/main/java/net/minecraft/server/IChunkLoader.java b/src/main/java/net/minecraft/server/IChunkLoader.java
-index f0a052eec2..2f95174fcc 100644
+index f0a052eec..2f95174fc 100644
 --- a/src/main/java/net/minecraft/server/IChunkLoader.java
 +++ b/src/main/java/net/minecraft/server/IChunkLoader.java
 @@ -8,7 +8,7 @@ import javax.annotation.Nullable;
@@ -108,7 +108,7 @@ index f0a052eec2..2f95174fcc 100644
      @Nullable
      private PersistentStructureLegacy c;
 diff --git a/src/main/java/net/minecraft/server/PlayerChunk.java b/src/main/java/net/minecraft/server/PlayerChunk.java
-index 66a389a67e..027a6b0fd2 100644
+index 66a389a67..027a6b0fd 100644
 --- a/src/main/java/net/minecraft/server/PlayerChunk.java
 +++ b/src/main/java/net/minecraft/server/PlayerChunk.java
 @@ -114,6 +114,19 @@ public class PlayerChunk {
@@ -132,7 +132,7 @@ index 66a389a67e..027a6b0fd2 100644
  
      public CompletableFuture<Either<IChunkAccess, PlayerChunk.Failure>> getStatusFutureUnchecked(ChunkStatus chunkstatus) {
 diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
-index 18c71609c0..57b1341b5a 100644
+index 18c71609c..57b1341b5 100644
 --- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
 +++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
 @@ -968,12 +968,62 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -201,7 +201,7 @@ index 18c71609c0..57b1341b5a 100644
          // Spigot start
          return isOutsideOfRange(chunkcoordintpair, false);
 diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java
-index 6b543f89d4..d37abf2cf3 100644
+index 6b543f89d..d37abf2cf 100644
 --- a/src/main/java/net/minecraft/server/RegionFile.java
 +++ b/src/main/java/net/minecraft/server/RegionFile.java
 @@ -36,6 +36,30 @@ public class RegionFile implements AutoCloseable {
@@ -250,7 +250,7 @@ index 6b543f89d4..d37abf2cf3 100644
              this.c();
          } finally {
 diff --git a/src/main/java/net/minecraft/server/RegionFileCache.java b/src/main/java/net/minecraft/server/RegionFileCache.java
-index b3d1bb5fd8..e07ae98540 100644
+index b3d1bb5fd..e07ae9854 100644
 --- a/src/main/java/net/minecraft/server/RegionFileCache.java
 +++ b/src/main/java/net/minecraft/server/RegionFileCache.java
 @@ -18,7 +18,14 @@ public final class RegionFileCache implements AutoCloseable {
@@ -280,7 +280,7 @@ index b3d1bb5fd8..e07ae98540 100644
              throwable = throwable1;
              throw throwable1;
 diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
-index 661a89c5d6..a71bb86508 100644
+index 661a89c5d..a71bb8650 100644
 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
 +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
 @@ -18,6 +18,7 @@ import java.util.Objects;
diff --git a/Spigot-Server-Patches/0391-Asynchronous-chunk-IO-and-loading.patch b/Spigot-Server-Patches/0391-Asynchronous-chunk-IO-and-loading.patch
index 309cd900da..8a2acc8eb1 100644
--- a/Spigot-Server-Patches/0391-Asynchronous-chunk-IO-and-loading.patch
+++ b/Spigot-Server-Patches/0391-Asynchronous-chunk-IO-and-loading.patch
@@ -1,4 +1,4 @@
-From d8323c167f2f692f00b4396f04b8ea4af7b2f3ac Mon Sep 17 00:00:00 2001
+From 141395a5db93ca1716ceeff875c0c807636d9c06 Mon Sep 17 00:00:00 2001
 From: Spottedleaf <Spottedleaf@users.noreply.github.com>
 Date: Sat, 13 Jul 2019 09:23:10 -0700
 Subject: [PATCH] Asynchronous chunk IO and loading
@@ -2569,7 +2569,7 @@ index b582171c5..03d7ce829 100644
          } finally {
              playerChunkMap.callbackExecutor.run();
 diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
-index a950ad801..26f1a4b09 100644
+index 8816c90e2..0b61ee3ba 100644
 --- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
 +++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
 @@ -6,6 +6,7 @@ import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
@@ -2785,20 +2785,24 @@ index a950ad801..26f1a4b09 100644
              if (chunksection != Chunk.a || nibblearray != null || nibblearray1 != null) {
                  nbttagcompound2 = new NBTTagCompound();
                  nbttagcompound2.setByte("Y", (byte) (i & 255));
-@@ -315,10 +434,10 @@ public class ChunkRegionLoader {
-                     // Paper start
-                     if ((int) Math.floor(entity.locX()) >> 4 != chunk.getPos().x || (int) Math.floor(entity.locZ()) >> 4 != chunk.getPos().z) {
-                         LogManager.getLogger().warn(entity + " is not in this chunk, skipping save. This a bug fix to a vanilla bug. Do not report this to PaperMC please.");
--                        toUpdate.add(entity);
-+                        if (asyncsavedata == null) toUpdate.add(entity); // todo fix this broken code, entityJoinedWorld wont work in this case!
-                         continue;
-                     }
--                    if (entity.dead) {
-+                    if (asyncsavedata == null && entity.dead) { // todo
-                         continue;
-                     }
-                     // Paper end
-@@ -354,24 +473,32 @@ public class ChunkRegionLoader {
+@@ -329,9 +448,13 @@ public class ChunkRegionLoader {
+             }
+ 
+             // Paper start - move entities to the correct chunk
+-            for (Entity entity : toUpdate) {
+-                worldserver.chunkCheck(entity);
+-            }
++            worldserver.getMinecraftServer().execute(() -> {
++                for (Entity entity : toUpdate) {
++                    if (!entity.dead) {
++                        worldserver.chunkCheck(entity);
++                    }
++                }
++            });
+             // Paper end
+ 
+         } else {
+@@ -353,24 +476,32 @@ public class ChunkRegionLoader {
          }
  
          nbttagcompound1.set("Entities", nbttaglist2);