From 6194f6cc4985a4a6840bb2cc39375caf9835ae68 Mon Sep 17 00:00:00 2001
From: Geoff Crossland <gcrossland+bukkit@gmail.com>
Date: Tue, 31 Jan 2017 23:52:21 +0000
Subject: [PATCH] Drop RegionFile.chunkExists() in favour of Mojang's own
 version

---
 nms-patches/ChunkProviderServer.patch |  4 +-
 nms-patches/ChunkRegionLoader.patch   | 41 ++++++++------------
 nms-patches/RegionFile.patch          | 56 ++++++---------------------
 nms-patches/RegionFileCache.patch     |  7 +++-
 4 files changed, 36 insertions(+), 72 deletions(-)

diff --git a/nms-patches/ChunkProviderServer.patch b/nms-patches/ChunkProviderServer.patch
index bcb4586a4f..954d90732b 100644
--- a/nms-patches/ChunkProviderServer.patch
+++ b/nms-patches/ChunkProviderServer.patch
@@ -22,7 +22,7 @@
 +            if (this.chunkLoader instanceof ChunkRegionLoader) {
 +                loader = (ChunkRegionLoader) this.chunkLoader;
 +            }
-+            if (loader != null && loader.chunkExists(world, i, j)) {
++            if (loader != null && loader.a(i, j)) {
 +                chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j);
 +            }
 +        }
@@ -72,7 +72,7 @@
 +
 +        }
 +        // We can only use the queue for already generated chunks
-+        if (chunk == null && loader != null && loader.chunkExists(world, i, j)) {
++        if (chunk == null && loader != null && loader.a(i, j)) {
 +            if (runnable != null) {
 +                ChunkIOExecutor.queueChunkLoad(world, loader, this, i, j, runnable);
 +                return null;
diff --git a/nms-patches/ChunkRegionLoader.patch b/nms-patches/ChunkRegionLoader.patch
index fb39a6e007..f83005d821 100644
--- a/nms-patches/ChunkRegionLoader.patch
+++ b/nms-patches/ChunkRegionLoader.patch
@@ -1,23 +1,9 @@
 --- a/net/minecraft/server/ChunkRegionLoader.java
 +++ b/net/minecraft/server/ChunkRegionLoader.java
-@@ -29,19 +29,49 @@
+@@ -29,25 +29,41 @@
          this.e = dataconvertermanager;
      }
  
-+    // CraftBukkit start
-+    public boolean chunkExists(World world, int i, int j) {
-+        ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i, j);
-+
-+        if (this.c.contains(chunkcoordintpair)) {
-+            if (this.b.containsKey(chunkcoordintpair)) {
-+                return true;
-+            }
-+        }
-+
-+        return RegionFileCache.a(this.d, i, j).chunkExists(i & 31, j & 31);
-+    }
-+    // CraftBukkit end
-+
 +    // CraftBukkit start - Add async variant, provide compatibility
      @Nullable
      public Chunk a(World world, int i, int j) throws IOException {
@@ -53,7 +39,14 @@
          }
  
          return this.a(world, i, j, nbttagcompound);
-@@ -55,7 +85,7 @@
+     }
+ 
+-    public boolean a(int i, int j) {
++    public boolean a(int i, int j) { // PAIL chunkExists (also in IChunkLoader)
+         ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i, j);
+         NBTTagCompound nbttagcompound = (NBTTagCompound) this.b.get(chunkcoordintpair);
+ 
+@@ -55,7 +71,7 @@
      }
  
      @Nullable
@@ -62,7 +55,7 @@
          if (!nbttagcompound.hasKeyOfType("Level", 10)) {
              ChunkRegionLoader.a.error("Chunk file at {},{} is missing level data, skipping", new Object[] { Integer.valueOf(i), Integer.valueOf(j)});
              return null;
-@@ -72,10 +102,28 @@
+@@ -72,10 +88,28 @@
                      ChunkRegionLoader.a.error("Chunk file at {},{} is in the wrong location; relocating. (Expected {}, {}, got {}, {})", new Object[] { Integer.valueOf(i), Integer.valueOf(j), Integer.valueOf(i), Integer.valueOf(j), Integer.valueOf(chunk.locX), Integer.valueOf(chunk.locZ)});
                      nbttagcompound1.setInt("xPos", i);
                      nbttagcompound1.setInt("zPos", j);
@@ -92,7 +85,7 @@
              }
          }
      }
-@@ -106,20 +154,27 @@
+@@ -106,20 +140,27 @@
      }
  
      public boolean c() {
@@ -123,7 +116,7 @@
  
                  if (nbttagcompound != null) {
                      try {
-@@ -139,10 +194,14 @@
+@@ -139,10 +180,14 @@
      }
  
      private void b(ChunkCoordIntPair chunkcoordintpair, NBTTagCompound nbttagcompound) throws IOException {
@@ -139,7 +132,7 @@
      }
  
      public void b(World world, Chunk chunk) throws IOException {}
-@@ -157,6 +216,7 @@
+@@ -157,6 +202,7 @@
                  if (this.c()) {
                      continue;
                  }
@@ -147,7 +140,7 @@
              }
          } finally {
              this.f = false;
-@@ -334,6 +394,13 @@
+@@ -334,6 +380,13 @@
              chunk.a(nbttagcompound.getByteArray("Biomes"));
          }
  
@@ -161,7 +154,7 @@
          NBTTagList nbttaglist1 = nbttagcompound.getList("Entities", 10);
  
          for (int l = 0; l < nbttaglist1.size(); ++l) {
-@@ -371,7 +438,7 @@
+@@ -371,7 +424,7 @@
              }
          }
  
@@ -170,7 +163,7 @@
      }
  
      @Nullable
-@@ -399,14 +466,20 @@
+@@ -399,14 +452,20 @@
      }
  
      @Nullable
@@ -192,7 +185,7 @@
                  return null;
              } else {
                  if (nbttagcompound.hasKeyOfType("Passengers", 9)) {
-@@ -435,8 +508,14 @@
+@@ -435,8 +494,14 @@
          }
      }
  
diff --git a/nms-patches/RegionFile.patch b/nms-patches/RegionFile.patch
index de58590070..a1abb02577 100644
--- a/nms-patches/RegionFile.patch
+++ b/nms-patches/RegionFile.patch
@@ -1,48 +1,16 @@
 --- a/net/minecraft/server/RegionFile.java
 +++ b/net/minecraft/server/RegionFile.java
-@@ -86,6 +86,45 @@
- 
+@@ -224,11 +224,11 @@
+         return i < 0 || i >= 32 || j < 0 || j >= 32;
+     }
+ 
+-    private int e(int i, int j) {
++    private synchronized int e(int i, int j) { // CraftBukkit
+         return this.d[i + j * 32];
+     }
+ 
+-    public boolean c(int i, int j) {
++    public boolean c(int i, int j) { // PAIL chunkExists
+         return this.e(i, j) != 0;
      }
  
-+    // CraftBukkit start - This is a copy (sort of) of the method below it, make sure they stay in sync
-+    public synchronized boolean chunkExists(int i, int j) {
-+        if (this.d(i, j)) {
-+            return false;
-+        } else {
-+            try {
-+                int k = this.e(i, j);
-+
-+                if (k == 0) {
-+                    return false;
-+                } else {
-+                    int l = k >> 8;
-+                    int i1 = k & 255;
-+
-+                    if (l + i1 > this.f.size()) {
-+                        return false;
-+                    }
-+
-+                    this.c.seek((long) (l * 4096));
-+                    int j1 = this.c.readInt();
-+
-+                    if (j1 > 4096 * i1 || j1 <= 0) {
-+                        return false;
-+                    }
-+
-+                    byte b0 = this.c.readByte();
-+                    if (b0 == 1 || b0 == 2) {
-+                        return true;
-+                    }
-+                }
-+            } catch (IOException ioexception) {
-+                return false;
-+            }
-+        }
-+
-+        return false;
-+    }
-+    // CraftBukkit end
-+
-     @Nullable
-     public synchronized DataInputStream a(int i, int j) {
-         if (this.d(i, j)) {
diff --git a/nms-patches/RegionFileCache.patch b/nms-patches/RegionFileCache.patch
index e97b881c74..fe90d577fc 100644
--- a/nms-patches/RegionFileCache.patch
+++ b/nms-patches/RegionFileCache.patch
@@ -1,6 +1,6 @@
 --- a/net/minecraft/server/RegionFileCache.java
 +++ b/net/minecraft/server/RegionFileCache.java
-@@ -74,17 +74,27 @@
+@@ -74,19 +74,29 @@
          RegionFileCache.a.clear();
      }
  
@@ -30,5 +30,8 @@
      }
 +    // CraftBukkit end
  
-     public static boolean f(File file, int i, int j) {
+-    public static boolean f(File file, int i, int j) {
++    public static synchronized boolean f(File file, int i, int j) { // PAIL chunkExists; CraftBukkit
          RegionFile regionfile = b(file, i, j);
+ 
+         return regionfile != null ? regionfile.c(i & 31, j & 31) : false;