From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Mon, 8 Jul 2019 00:13:36 -0700
Subject: [PATCH] Use getChunkIfLoadedImmediately in places

This prevents us from hitting chunk loads for chunks at or less-than
ticket level 33 (yes getChunkIfLoaded will actually perform a chunk
load in that case).

diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 51e290adb44c9f7987ad79f22f158e5f93bedbbe..081c031f08c9817de6c4e1e6bf18b11efc3c668b 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -211,7 +211,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
     }
 
     @Override public LevelChunk getChunkIfLoaded(int x, int z) { // Paper - this was added in world too but keeping here for NMS ABI
-        return this.chunkSource.getChunk(x, z, false);
+        return this.chunkSource.getChunkAtIfLoadedImmediately(x, z); // Paper
     }
 
     @Override
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index b0c319175b40b0f1ac87152eb64b6b60ddc36464..8705df3ca3df6b4aa77b38e1a0b6ad5fc3bc3f28 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1309,7 +1309,7 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
                                 speed = this.player.getAbilities().walkingSpeed * 10f;
                             }
                             // Paper start - Prevent moving into unloaded chunks
-                            if (player.level.paperConfig.preventMovingIntoUnloadedChunks && (this.player.getX() != toX || this.player.getZ() != toZ) && !worldserver.hasChunk((int) Math.floor(toX) >> 4, (int) Math.floor(toZ) >> 4)) {
+                            if (player.level.paperConfig.preventMovingIntoUnloadedChunks && (this.player.getX() != toX || this.player.getZ() != toZ) && worldserver.getChunkIfLoadedImmediately((int) Math.floor(toX) >> 4, (int) Math.floor(toZ) >> 4) == null) { // Paper - use getIfLoadedImmediately
                                 this.internalTeleport(this.player.getX(), this.player.getY(), this.player.getZ(), this.player.getYRot(), this.player.getXRot(), Collections.emptySet(), true);
                                 return;
                             }
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index aaebbf265ba6ad98fc14d02d24b3db1a6fa904ba..7ac5755df29a82d053fdd3f9e83b0789cbdb0525 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -194,6 +194,13 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
         return (CraftServer) Bukkit.getServer();
     }
 
+    // Paper start
+    @Override
+    public boolean hasChunk(int chunkX, int chunkZ) {
+        return this.getChunkIfLoaded(chunkX, chunkZ) != null;
+    }
+    // Paper end
+
     public abstract ResourceKey<LevelStem> getTypeKey();
 
     protected Level(WritableLevelData worlddatamutable, ResourceKey<Level> resourcekey, final DimensionType dimensionmanager, Supplier<ProfilerFiller> supplier, boolean flag, boolean flag1, long i, org.bukkit.generator.ChunkGenerator gen, org.bukkit.generator.BiomeProvider biomeProvider, org.bukkit.World.Environment env, java.util.concurrent.Executor executor) { // Paper - Async-Anti-Xray - Pass executor
@@ -1366,7 +1373,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
 
         for (int l1 = j; l1 <= l; ++l1) {
             for (int i2 = k; i2 <= i1; ++i2) {
-                LevelChunk chunk = this.getChunkSource().getChunkNow(l1, i2);
+                LevelChunk chunk = (LevelChunk) this.getChunkIfLoadedImmediately(l1, i2); // Paper
 
                 if (chunk != null) {
                     for (int j2 = j1; j2 <= k1; ++j2) {
