From 55aa29c70036dd67721adc88713d62009023e2d1 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Wed, 19 Jun 2019 15:48:48 -0700
Subject: [PATCH] Fix MC-154214 (#2195)

This should resolve unloading issues.
---
 ...403-Fix-World-isChunkGenerated-calls.patch |  2 +-
 ...ate-location-if-we-failed-to-read-it.patch |  2 +-
 .../0405-Fix-MC-154214.patch                  | 50 +++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 Spigot-Server-Patches/0405-Fix-MC-154214.patch

diff --git a/Spigot-Server-Patches/0403-Fix-World-isChunkGenerated-calls.patch b/Spigot-Server-Patches/0403-Fix-World-isChunkGenerated-calls.patch
index 47c273623..d4f88edca 100644
--- a/Spigot-Server-Patches/0403-Fix-World-isChunkGenerated-calls.patch
+++ b/Spigot-Server-Patches/0403-Fix-World-isChunkGenerated-calls.patch
@@ -1,4 +1,4 @@
-From 66c4bd93d02eff2e8d86242544bf26e251af3adc Mon Sep 17 00:00:00 2001
+From 27e5668efea88ccb9b7f77a1d7222b6b9f2ac65a 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
diff --git a/Spigot-Server-Patches/0404-Show-blockstate-location-if-we-failed-to-read-it.patch b/Spigot-Server-Patches/0404-Show-blockstate-location-if-we-failed-to-read-it.patch
index 70153792b..99d651444 100644
--- a/Spigot-Server-Patches/0404-Show-blockstate-location-if-we-failed-to-read-it.patch
+++ b/Spigot-Server-Patches/0404-Show-blockstate-location-if-we-failed-to-read-it.patch
@@ -1,4 +1,4 @@
-From 04c4f0e5dc5048071fb5fc42dac2a2302091c670 Mon Sep 17 00:00:00 2001
+From 69cdbe91b7cd74fedd4c1cb6eb5d405dca1deaca Mon Sep 17 00:00:00 2001
 From: Spottedleaf <Spottedleaf@users.noreply.github.com>
 Date: Sat, 15 Jun 2019 10:28:25 -0700
 Subject: [PATCH] Show blockstate location if we failed to read it
diff --git a/Spigot-Server-Patches/0405-Fix-MC-154214.patch b/Spigot-Server-Patches/0405-Fix-MC-154214.patch
new file mode 100644
index 000000000..c531a9588
--- /dev/null
+++ b/Spigot-Server-Patches/0405-Fix-MC-154214.patch
@@ -0,0 +1,50 @@
+From 12a8af6c9ac2c8c30a0723cc70b6435ae7f03d01 Mon Sep 17 00:00:00 2001
+From: Spottedleaf <Spottedleaf@users.noreply.github.com>
+Date: Sun, 16 Jun 2019 23:30:25 -0700
+Subject: [PATCH] Fix MC-154214
+
+Avoid adding player tickets when they're out of range of the closest player
+
+diff --git a/src/main/java/net/minecraft/server/ChunkMapDistance.java b/src/main/java/net/minecraft/server/ChunkMapDistance.java
+index 705ca68798..799c134e4b 100644
+--- a/src/main/java/net/minecraft/server/ChunkMapDistance.java
++++ b/src/main/java/net/minecraft/server/ChunkMapDistance.java
+@@ -367,12 +367,18 @@ public abstract class ChunkMapDistance {
+         }
+ 
+         private void a(long i, int j, boolean flag, boolean flag1) {
++            final long chunkPosition = i; // Paper - conflict on variable change
+             if (flag != flag1) {
+                 Ticket<?> ticket = new Ticket<>(TicketType.PLAYER, ChunkMapDistance.b, new ChunkCoordIntPair(i), ChunkMapDistance.this.currentTick);
+ 
+                 if (flag1) {
+                     ChunkMapDistance.this.m.a(ChunkTaskQueueSorter.a(() -> { // Paper - decompile fix
+                         ChunkMapDistance.this.p.execute(() -> {
++                            // Paper start - Fix MC-154214
++                            if (!this.inRange(this.getNearestDistanceFromPlayerMap().get(chunkPosition))) {
++                                return;
++                            }
++                            // Paper end
+                             ChunkMapDistance.this.a(i, ticket);
+                             ChunkMapDistance.this.o.add(i);
+                         });
+@@ -416,6 +422,7 @@ public abstract class ChunkMapDistance {
+ 
+         }
+ 
++        private final boolean inRange(int distance) { return this.c(distance); } // Paper - OBFHELPER
+         private boolean c(int i) {
+             return i <= this.e - 2;
+         }
+@@ -463,7 +470,7 @@ public abstract class ChunkMapDistance {
+ 
+     class b extends ChunkMap {
+ 
+-        protected final Long2ByteMap a = new Long2ByteOpenHashMap();
++        protected final Long2ByteMap a = new Long2ByteOpenHashMap(); protected final Long2ByteMap getNearestDistanceFromPlayerMap() { return this.a; } // Paper - OBFHELPER
+         protected final int b;
+ 
+         protected b(int i) {
+-- 
+2.21.0
+