Rework async chunk api implementation
Firstly, the old methods all routed to the CompletableFuture method. However, the CF method could not guarantee that if the caller was off-main that the future would be "completed" on-main. Since the callback methods used the CF one, this meant that the callback methods did not guarantee that the callbacks were to be called on the main thread. Now, all methods route to getChunkAtAsync(x, z, gen, urgent, cb) so that the methods with the callback are guaranteed to invoke the callback on the main thread. The CF behavior remains unchanged; it may still appear to complete on main if invoked off-main. Secondly, remove the scheduleOnMain invocation in the async chunk completion. This unnecessarily delays the callback by 1 tick. Thirdly, add getChunksAtAsync(minX, minZ, maxX, maxZ, ...) which will load chunks within an area. This method is provided as a helper as keeping all chunks loaded within an area can be complicated to implement for plugins (due to the lacking ticket API), and is already implemented internally anyways. Fourthly, remove the ticket addition that occured with getChunkAt and getChunkAtAsync. The ticket addition may delay the unloading of the chunk unnecessarily. It also fixes a very rare timing bug where the future/callback would be completed after the chunk unloads.
This commit is contained in:
@@ -43,9 +43,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
@Override
|
||||
public Chunk getChunkAt(int x, int z) {
|
||||
+ warnUnsafeChunk("getting a faraway chunk", x, z); // Paper
|
||||
// Paper start - add ticket to hold chunk for a little while longer if plugin accesses it
|
||||
net.minecraft.world.level.chunk.LevelChunk chunk = this.world.getChunkSource().getChunkAtIfLoadedImmediately(x, z);
|
||||
if (chunk == null) {
|
||||
net.minecraft.world.level.chunk.LevelChunk chunk = (net.minecraft.world.level.chunk.LevelChunk) this.world.getChunk(x, z, ChunkStatus.FULL, true);
|
||||
return new CraftChunk(chunk);
|
||||
}
|
||||
@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World {
|
||||
if (!unloadChunk0(x, z, false)) {
|
||||
return false;
|
||||
@@ -87,10 +87,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
return this.world.getChunk(x >> 4, z >> 4).getHeight(CraftHeightMap.toNMS(heightMap), x, z);
|
||||
}
|
||||
@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World {
|
||||
// Spigot end
|
||||
// Paper start
|
||||
public java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(int x, int z, boolean gen, boolean urgent) {
|
||||
@Override
|
||||
public void getChunkAtAsync(int x, int z, boolean gen, boolean urgent, @NotNull Consumer<? super Chunk> cb) {
|
||||
+ warnUnsafeChunk("getting a faraway chunk async", x, z); // Paper
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
net.minecraft.world.level.chunk.LevelChunk immediate = this.world.getChunkSource().getChunkAtIfLoadedImmediately(x, z);
|
||||
if (immediate != null) {
|
||||
ca.spottedleaf.moonrise.common.util.ChunkSystem.scheduleChunkLoad(
|
||||
this.getHandle(), x, z, gen, ChunkStatus.FULL, true,
|
||||
urgent ? ca.spottedleaf.concurrentutil.util.Priority.HIGHER : ca.spottedleaf.concurrentutil.util.Priority.NORMAL,
|
||||
@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World {
|
||||
|
||||
@Override
|
||||
public void getChunksAtAsync(int minX, int minZ, int maxX, int maxZ, boolean urgent, Runnable cb) {
|
||||
+ warnUnsafeChunk("getting a faraway chunk async", minX, minZ); // Paper
|
||||
+ warnUnsafeChunk("getting a faraway chunk async", maxX, maxZ); // Paper
|
||||
this.getHandle().loadChunks(
|
||||
minX, minZ, maxX, maxZ,
|
||||
urgent ? ca.spottedleaf.concurrentutil.util.Priority.HIGHER : ca.spottedleaf.concurrentutil.util.Priority.NORMAL,
|
||||
|
||||
Reference in New Issue
Block a user