From 93dc25ececbc05f07a390ab28f0ce3180a9a625e Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 5 Jul 2018 18:39:43 -0400
Subject: [PATCH] Fix weird bugs with entities - Fixes #1195

---
 ...-get-a-BlockState-without-a-snapshot.patch |  2 +-
 ...unk-Unloads-based-on-Player-Movement.patch |  4 ++--
 ...oreboards-for-non-players-by-default.patch |  2 +-
 ...llow-entities-to-ride-themselves-572.patch |  2 +-
 .../Don-t-teleport-dead-entities.patch        |  2 +-
 ...ck-and-tnt-entities-at-the-specified.patch |  2 +-
 Spigot-Server-Patches/Entity-Origin-API.patch |  2 +-
 Spigot-Server-Patches/Optimize-Hoppers.patch  |  2 +-
 ...nilla-per-world-scoreboard-coloring-.patch |  2 +-
 .../Optional-TNT-doesn-t-move-in-water.patch  |  2 +-
 ...event-tile-entity-and-entity-crashes.patch |  2 +-
 ...emove-entities-on-dimension-teleport.patch |  2 +-
 ...to-current-Chunk-for-Entity-and-Bloc.patch | 21 ++++++++++++++-----
 .../Vehicle-Event-Cancellation-Changes.patch  |  2 +-
 14 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/Spigot-Server-Patches/API-to-get-a-BlockState-without-a-snapshot.patch b/Spigot-Server-Patches/API-to-get-a-BlockState-without-a-snapshot.patch
index 25b0be65fc..e616833ef3 100644
--- a/Spigot-Server-Patches/API-to-get-a-BlockState-without-a-snapshot.patch
+++ b/Spigot-Server-Patches/API-to-get-a-BlockState-without-a-snapshot.patch
@@ -13,7 +13,7 @@ also Avoid NPE during CraftBlockEntityState load if could not get TE
 If Tile Entity was null, correct Sign to return empty lines instead of null
 
 diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
-index d3bc13726..cc7f2069d 100644
+index 358c99227..5b5f102e2 100644
 --- a/src/main/java/net/minecraft/server/TileEntity.java
 +++ b/src/main/java/net/minecraft/server/TileEntity.java
 @@ -0,0 +0,0 @@ public abstract class TileEntity implements KeyedObject {
diff --git a/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch b/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch
index 2ac8800138..49feecbe81 100644
--- a/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch
+++ b/Spigot-Server-Patches/Delay-Chunk-Unloads-based-on-Player-Movement.patch
@@ -40,11 +40,11 @@ index ce43e7bb7..e35e72e8b 100644
 +    }
  }
 diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
-index 857df2937..cc9c8b2e0 100644
+index 3904a1b8f..bcf68af4f 100644
 --- a/src/main/java/net/minecraft/server/Chunk.java
 +++ b/src/main/java/net/minecraft/server/Chunk.java
 @@ -0,0 +0,0 @@ public class Chunk {
-     private boolean j;
+     private boolean j; public boolean isLoaded() { return j; } // Paper - OBFHELPER
      public final World world;
      public final int[] heightMap;
 +    public Long scheduledForUnload; // Paper - delay chunk unloads
diff --git a/Spigot-Server-Patches/Disable-Scoreboards-for-non-players-by-default.patch b/Spigot-Server-Patches/Disable-Scoreboards-for-non-players-by-default.patch
index c9a89d476c..78b2a74f95 100644
--- a/Spigot-Server-Patches/Disable-Scoreboards-for-non-players-by-default.patch
+++ b/Spigot-Server-Patches/Disable-Scoreboards-for-non-players-by-default.patch
@@ -37,7 +37,7 @@ index ec9a87239..b08274d93 100644
  
                          if (scoreboard.addPlayerToTeam(s2, s)) {
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 584501787..b4ad611fc 100644
+index 21118c031..921b442cd 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Don-t-allow-entities-to-ride-themselves-572.patch b/Spigot-Server-Patches/Don-t-allow-entities-to-ride-themselves-572.patch
index afde6c4bb1..c5086ab7f1 100644
--- a/Spigot-Server-Patches/Don-t-allow-entities-to-ride-themselves-572.patch
+++ b/Spigot-Server-Patches/Don-t-allow-entities-to-ride-themselves-572.patch
@@ -5,7 +5,7 @@ Subject: [PATCH] Don't allow entities to ride themselves - #572
 
 
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index b13830e87..20324deeb 100644
+index 0ed9a8413..646a2d673 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Don-t-teleport-dead-entities.patch b/Spigot-Server-Patches/Don-t-teleport-dead-entities.patch
index 4c69b2125e..145ba636ae 100644
--- a/Spigot-Server-Patches/Don-t-teleport-dead-entities.patch
+++ b/Spigot-Server-Patches/Don-t-teleport-dead-entities.patch
@@ -7,7 +7,7 @@ Had some issue with this in past, and this is the vanilla logic.
 Potentially an old CB change that's no longer needed.
 
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 4a08db5ba..d4ab5fba9 100644
+index e8775e66c..7e992aeaa 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Drop-falling-block-and-tnt-entities-at-the-specified.patch b/Spigot-Server-Patches/Drop-falling-block-and-tnt-entities-at-the-specified.patch
index 4ce5c602c6..004f12acad 100644
--- a/Spigot-Server-Patches/Drop-falling-block-and-tnt-entities-at-the-specified.patch
+++ b/Spigot-Server-Patches/Drop-falling-block-and-tnt-entities-at-the-specified.patch
@@ -24,7 +24,7 @@ index 0094d1a87..4da846719 100644
 +    }
  }
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 8dcff9bd4..041af8070 100644
+index 137da4255..655340c0b 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Entity-Origin-API.patch b/Spigot-Server-Patches/Entity-Origin-API.patch
index 985b823a96..7b518e9d6a 100644
--- a/Spigot-Server-Patches/Entity-Origin-API.patch
+++ b/Spigot-Server-Patches/Entity-Origin-API.patch
@@ -5,7 +5,7 @@ Subject: [PATCH] Entity Origin API
 
 
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 041af8070..43b802855 100644
+index 655340c0b..a96513e27 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Optimize-Hoppers.patch b/Spigot-Server-Patches/Optimize-Hoppers.patch
index c84a852317..2c4f1253f0 100644
--- a/Spigot-Server-Patches/Optimize-Hoppers.patch
+++ b/Spigot-Server-Patches/Optimize-Hoppers.patch
@@ -40,7 +40,7 @@ index fcf6bac08..3092913f5 100644
                  this.methodProfiler.a(() -> {
                      return worldserver.getWorldData().getName();
 diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
-index cc7f2069d..78ce2f8bb 100644
+index 5b5f102e2..3f6b34489 100644
 --- a/src/main/java/net/minecraft/server/TileEntity.java
 +++ b/src/main/java/net/minecraft/server/TileEntity.java
 @@ -0,0 +0,0 @@ public abstract class TileEntity implements KeyedObject {
diff --git a/Spigot-Server-Patches/Option-to-use-vanilla-per-world-scoreboard-coloring-.patch b/Spigot-Server-Patches/Option-to-use-vanilla-per-world-scoreboard-coloring-.patch
index 7bb1446aa9..05a3bfda28 100644
--- a/Spigot-Server-Patches/Option-to-use-vanilla-per-world-scoreboard-coloring-.patch
+++ b/Spigot-Server-Patches/Option-to-use-vanilla-per-world-scoreboard-coloring-.patch
@@ -19,7 +19,7 @@ index abc1aabdd..6ea608ba9 100644
 +    }
  }
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index d4ab5fba9..7314b4dc5 100644
+index 7e992aeaa..08d872318 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Optional-TNT-doesn-t-move-in-water.patch b/Spigot-Server-Patches/Optional-TNT-doesn-t-move-in-water.patch
index ae76abcb44..93d567ae22 100644
--- a/Spigot-Server-Patches/Optional-TNT-doesn-t-move-in-water.patch
+++ b/Spigot-Server-Patches/Optional-TNT-doesn-t-move-in-water.patch
@@ -32,7 +32,7 @@ index 067cb233e..06acdaaf0 100644
 +    }
  }
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 945f06c93..cb9ef622c 100644
+index 0412e183a..9ce691061 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Prevent-tile-entity-and-entity-crashes.patch b/Spigot-Server-Patches/Prevent-tile-entity-and-entity-crashes.patch
index 3bd0dc56fe..50b30e3402 100644
--- a/Spigot-Server-Patches/Prevent-tile-entity-and-entity-crashes.patch
+++ b/Spigot-Server-Patches/Prevent-tile-entity-and-entity-crashes.patch
@@ -5,7 +5,7 @@ Subject: [PATCH] Prevent tile entity and entity crashes
 
 
 diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
-index c1f657808..dfdc55583 100644
+index 15f18ba1d..33e245e4c 100644
 --- a/src/main/java/net/minecraft/server/TileEntity.java
 +++ b/src/main/java/net/minecraft/server/TileEntity.java
 @@ -0,0 +0,0 @@ public abstract class TileEntity implements KeyedObject {
diff --git a/Spigot-Server-Patches/Properly-remove-entities-on-dimension-teleport.patch b/Spigot-Server-Patches/Properly-remove-entities-on-dimension-teleport.patch
index ee0b456cd3..0cc26a60b8 100644
--- a/Spigot-Server-Patches/Properly-remove-entities-on-dimension-teleport.patch
+++ b/Spigot-Server-Patches/Properly-remove-entities-on-dimension-teleport.patch
@@ -22,7 +22,7 @@ requirement, but plugins (such as my own) use this method to
 trigger a "reload" of the entity on the client.
 
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 00791faf2..70641d479 100644
+index fccaebfbd..5d1ea0d55 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
diff --git a/Spigot-Server-Patches/Store-reference-to-current-Chunk-for-Entity-and-Bloc.patch b/Spigot-Server-Patches/Store-reference-to-current-Chunk-for-Entity-and-Bloc.patch
index c22cc3417e..12003e9878 100644
--- a/Spigot-Server-Patches/Store-reference-to-current-Chunk-for-Entity-and-Bloc.patch
+++ b/Spigot-Server-Patches/Store-reference-to-current-Chunk-for-Entity-and-Bloc.patch
@@ -8,9 +8,18 @@ This enables us a fast reference to the entities current chunk instead
 of having to look it up by hashmap lookups.
 
 diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
-index 4bbebb25a..b40e60942 100644
+index 4bbebb25a..68008fe6a 100644
 --- a/src/main/java/net/minecraft/server/Chunk.java
 +++ b/src/main/java/net/minecraft/server/Chunk.java
+@@ -0,0 +0,0 @@ public class Chunk {
+     private final byte[] g;
+     private final int[] h;
+     private final boolean[] i;
+-    private boolean j;
++    private boolean j; public boolean isLoaded() { return j; } // Paper - OBFHELPER
+     public final World world;
+     public final int[] heightMap;
+     public final int locX;
 @@ -0,0 +0,0 @@ public class Chunk {
      private boolean m;
      public final Map<BlockPosition, TileEntity> tileEntities;
@@ -72,7 +81,7 @@ index 4bbebb25a..b40e60942 100644
      }
  
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 3a8902bf1..3829c8c40 100644
+index 3a8902bf1..ed090cc50 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
@@ -81,7 +90,8 @@ index 3a8902bf1..3829c8c40 100644
      // Paper start
 +    private java.lang.ref.WeakReference<Chunk> currentChunk = null;
 +    public Chunk getCurrentChunk() {
-+        return currentChunk != null ? currentChunk.get() : null;
++        final Chunk chunk = currentChunk != null ? currentChunk.get() : world.getChunkIfLoaded(getChunkX(), getChunkZ());
++        return chunk != null && chunk.isLoaded() ? chunk : null;
 +    }
 +    public void setCurrentChunk(Chunk chunk) {
 +        this.currentChunk = chunk != null ? new java.lang.ref.WeakReference<>(chunk) : null;
@@ -90,7 +100,7 @@ index 3a8902bf1..3829c8c40 100644
      public final String entityKeyString = entityKey != null ? entityKey.toString() : null;
  
 diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
-index 672ba3134..88cd5734d 100644
+index 672ba3134..d7132c3c0 100644
 --- a/src/main/java/net/minecraft/server/TileEntity.java
 +++ b/src/main/java/net/minecraft/server/TileEntity.java
 @@ -0,0 +0,0 @@ public abstract class TileEntity implements KeyedObject {
@@ -99,7 +109,8 @@ index 672ba3134..88cd5734d 100644
      // Paper start
 +    private java.lang.ref.WeakReference<Chunk> currentChunk = null;
 +    public Chunk getCurrentChunk() {
-+        return currentChunk != null ? currentChunk.get() : null;
++        final Chunk chunk = currentChunk != null ? currentChunk.get() : world.getChunkIfLoaded(position.getX() >> 4, position.getZ() >> 4);
++        return chunk != null && chunk.isLoaded() ? chunk : null;
 +    }
 +    public void setCurrentChunk(Chunk chunk) {
 +        this.currentChunk = chunk != null ? new java.lang.ref.WeakReference<>(chunk) : null;
diff --git a/Spigot-Server-Patches/Vehicle-Event-Cancellation-Changes.patch b/Spigot-Server-Patches/Vehicle-Event-Cancellation-Changes.patch
index 5930ba0049..b198961d5b 100644
--- a/Spigot-Server-Patches/Vehicle-Event-Cancellation-Changes.patch
+++ b/Spigot-Server-Patches/Vehicle-Event-Cancellation-Changes.patch
@@ -5,7 +5,7 @@ Subject: [PATCH] Vehicle Event Cancellation Changes
 
 
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 7314b4dc5..d1f07bbbd 100644
+index 08d872318..e6a5bc53c 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper