diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java index 9c991774d5..bf60f4eb51 100644 --- a/src/main/java/net/minecraft/server/ChunkProviderServer.java +++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java @@ -14,6 +14,7 @@ import org.apache.logging.log4j.Logger; import java.util.Random; import org.bukkit.Server; +import org.bukkit.craftbukkit.chunkio.ChunkIOExecutor; import org.bukkit.craftbukkit.util.LongHash; import org.bukkit.craftbukkit.util.LongHashSet; import org.bukkit.craftbukkit.util.LongObjectHashMap; @@ -90,20 +91,37 @@ public class ChunkProviderServer implements IChunkProvider { public Chunk getChunkAt(int i, int j, Runnable runnable) { this.unloadQueue.remove(i, j); - Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j)); - boolean newChunk = false; + Chunk chunk = this.chunks.get(LongHash.toLong(i, j)); ChunkRegionLoader loader = null; if (this.f instanceof ChunkRegionLoader) { loader = (ChunkRegionLoader) this.f; } - // If the chunk exists but isn't loaded do it async - if (chunk == null && runnable != null && loader != null && loader.chunkExists(this.world, i, j)) { - org.bukkit.craftbukkit.chunkio.ChunkIOExecutor.queueChunkLoad(this.world, loader, this, i, j, runnable); - return null; + // We can only use the queue for already generated chunks + if (chunk == null && loader != null && loader.chunkExists(this.world, i, j)) { + if (runnable != null) { + ChunkIOExecutor.queueChunkLoad(this.world, loader, this, i, j, runnable); + return null; + } else { + chunk = ChunkIOExecutor.syncChunkLoad(this.world, loader, this, i, j); + } + } else if (chunk == null) { + chunk = this.originalGetChunkAt(i, j); } - // CraftBukkit end + + // If we didn't load the chunk async and have a callback run it now + if (runnable != null) { + runnable.run(); + } + + return chunk; + } + + public Chunk originalGetChunkAt(int i, int j) { + this.unloadQueue.remove(i, j); + Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j)); + boolean newChunk = false; if (chunk == null) { chunk = this.loadChunk(i, j); @@ -143,12 +161,6 @@ public class ChunkProviderServer implements IChunkProvider { chunk.a(this, this, i, j); } - // CraftBukkit start - If we didn't need to load the chunk run the callback now - if (runnable != null) { - runnable.run(); - } - // CraftBukkit end - return chunk; } diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java index 92fbc4f95d..afcf764b63 100644 --- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java +++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java @@ -5,7 +5,6 @@ import net.minecraft.server.ChunkProviderServer; import net.minecraft.server.ChunkRegionLoader; import net.minecraft.server.World; import org.bukkit.craftbukkit.util.AsynchronousExecutor; -import org.bukkit.craftbukkit.util.LongHash; public class ChunkIOExecutor { static final int BASE_THREADS = 1; @@ -13,12 +12,12 @@ public class ChunkIOExecutor { private static final AsynchronousExecutor instance = new AsynchronousExecutor(new ChunkIOProvider(), BASE_THREADS); - public static void waitForChunkLoad(World world, int x, int z) { - instance.get(new QueuedChunk(LongHash.toLong(x, z), null, world, null)); + public static Chunk syncChunkLoad(World world, ChunkRegionLoader loader, ChunkProviderServer provider, int x, int z) { + return instance.getSkipQueue(new QueuedChunk(x, z, loader, world, provider)); } public static void queueChunkLoad(World world, ChunkRegionLoader loader, ChunkProviderServer provider, int x, int z, Runnable runnable) { - instance.add(new QueuedChunk(LongHash.toLong(x, z), loader, world, provider), runnable); + instance.add(new QueuedChunk(x, z, loader, world, provider), runnable); } public static void adjustPoolSize(int players) { diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java index b9de12d88b..8feadd112a 100644 --- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java +++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java @@ -16,7 +16,7 @@ class ChunkIOProvider implements AsynchronousExecutor.CallBackProvider