2015-05-25 12:37:24 +02:00
--- a/net/minecraft/server/ChunkProviderServer.java
+++ b/net/minecraft/server/ChunkProviderServer.java
2018-12-25 22:00:00 +01:00
@@ -16,6 +16,11 @@
2014-11-25 22:32:16 +01:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+// CraftBukkit start
+import org.bukkit.craftbukkit.chunkio.ChunkIOExecutor;
+import org.bukkit.event.world.ChunkUnloadEvent;
+// CraftBukkit end
+
public class ChunkProviderServer implements IChunkProvider {
2016-02-29 22:32:46 +01:00
private static final Logger a = LogManager.getLogger();
2018-12-25 22:00:00 +01:00
@@ -34,7 +39,7 @@
2018-07-15 02:00:00 +02:00
this.chunkLoader = ichunkloader;
this.chunkGenerator = chunkgenerator;
2018-08-26 04:00:00 +02:00
this.asyncTaskHandler = iasynctaskhandler;
- this.chunkScheduler = new ChunkTaskScheduler(2, worldserver, chunkgenerator, ichunkloader, iasynctaskhandler);
+ this.chunkScheduler = new ChunkTaskScheduler(0, worldserver, chunkgenerator, ichunkloader, iasynctaskhandler); // CraftBukkit - very buggy, broken in lots of __subtle__ ways. Same goes for async chunk loading. Also Bukkit API / plugins can't handle async events at all anyway.
2018-12-25 22:00:00 +01:00
this.batchScheduler = new SchedulerBatch<>(this.chunkScheduler);
2018-07-15 02:00:00 +02:00
}
2018-12-25 22:00:00 +01:00
@@ -112,6 +117,22 @@
2014-11-25 22:32:16 +01:00
}
2018-07-15 02:00:00 +02:00
}
2014-11-25 22:32:16 +01:00
2018-07-25 11:32:06 +02:00
+ // CraftBukkit start
2018-08-26 04:00:00 +02:00
+ public Chunk generateChunk(int x, int z) {
+ try {
+ this.batchScheduler.b();
+ ChunkCoordIntPair pos = new ChunkCoordIntPair(x, z);
+ this.chunkScheduler.forcePolluteCache(pos);
+ this.batchScheduler.a(pos);
+ CompletableFuture<ProtoChunk> completablefuture = this.batchScheduler.c();
2018-07-25 11:32:06 +02:00
+
2018-08-26 04:00:00 +02:00
+ return (Chunk) completablefuture.thenApply(this::a).join();
+ } catch (RuntimeException runtimeexception) {
+ throw this.a(x, z, (Throwable) runtimeexception);
2018-07-25 11:32:06 +02:00
+ }
2018-08-26 04:00:00 +02:00
+ }
+ // CraftBukkit end
+
public IChunkAccess a(int i, int j, boolean flag) {
Chunk chunk = this.getChunkAt(i, j, true, false);
2014-11-25 22:32:16 +01:00
2018-12-25 22:00:00 +01:00
@@ -249,10 +270,12 @@
2018-08-26 04:00:00 +02:00
Chunk chunk = (Chunk) this.chunks.get(olong);
2016-05-11 07:34:16 +02:00
2018-08-26 04:00:00 +02:00
if (chunk != null) {
- chunk.removeEntities();
- this.saveChunk(chunk);
- this.chunks.remove(olong);
- this.lastChunk = null;
+ // CraftBukkit start - move unload logic to own method
+ if (!unloadChunk(chunk, true)) {
+ continue;
+ }
+ // CraftBukkit end
2014-11-25 22:32:16 +01:00
+
2018-08-26 04:00:00 +02:00
++i;
}
2016-06-22 01:08:09 +02:00
}
2018-12-25 22:00:00 +01:00
@@ -265,6 +288,42 @@
2016-06-22 01:08:09 +02:00
return false;
}
+ // CraftBukkit start
+ public boolean unloadChunk(Chunk chunk, boolean save) {
2016-07-11 13:10:56 +02:00
+ ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk, save);
2016-06-22 01:08:09 +02:00
+ this.world.getServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ return false;
+ }
2016-07-11 13:10:56 +02:00
+ save = event.isSaveChunk();
2016-06-22 01:08:09 +02:00
+
+ // Update neighbor counts
+ for (int x = -2; x < 3; x++) {
+ for (int z = -2; z < 3; z++) {
+ if (x == 0 && z == 0) {
+ continue;
+ }
+
2018-08-26 04:00:00 +02:00
+ Chunk neighbor = this.getChunkAt(chunk.locX + x, chunk.locZ + z, false, false);
2016-06-22 01:08:09 +02:00
+ if (neighbor != null) {
+ neighbor.setNeighborUnloaded(-x, -z);
+ chunk.setNeighborUnloaded(x, z);
+ }
+ }
+ }
+ // Moved from unloadChunks above
2018-09-10 11:13:03 +02:00
+ synchronized (this.chunkLoader) {
+ chunk.removeEntities();
+ if (save) {
+ this.saveChunk(chunk);
+ }
+ this.chunks.remove(chunk.chunkKey);
+ this.lastChunk = null;
2016-06-22 01:08:09 +02:00
+ }
+ return true;
+ }
+ // CraftBukkit end
+
2018-08-26 04:00:00 +02:00
public boolean d() {
2016-06-22 01:08:09 +02:00
return !this.world.savingDisabled;
}