Async Forceloading

Hi folks, I’m a contributor to a Spigot plugin that manages and keeps selected chunks loaded at all times.

Now and then, it is possible that 500+ chunks may be set to force load at the same time, which obviously can cause performance issues and crashes. I’d like to make this behavior asynchronous, but I’m having difficulty understanding how to accomplish this given that Bukkit API calls cannot be made async (specifically org.bukkit.world.setForceLoaded() ).

Is there an effective way to accomplish this with Paper? I’m assuming that Paper asynchronously load chunks without any sort of written async routine in main?

Thanks

Typing fast to beat cat, force loaded chunks are async loaded, they use the ticket system

I was debating on trying to find the time to see if it was possible to just not load the chunk sync when force loading, afaik, the chunk system should already load it, only concern is the behavioral break

You should be able to just use the async API to load the chunk, and then mark it as force loaded, the load attempt by the chunk will instantly be there as it’s already loaded

Thanks for the super fast response! Just to clarify, the chunks may or may not already be loaded beforehand. I’ve included some slightly out of context code snippets. Now I’m a little confused on how to handle the CompletableFuture instance returned by getChunkAtAsync().

Current Spigot Version:
Utilities.chunks.add(chunk); // Just an Internal list for tracking
Bukkit.getServer().getWorld(world).loadChunk(x, z);
Bukkit.getServer().getWorld(world).setChunkForceLoaded(x, z, true);

Revised w/ async:
Utilities.chunks.add(chunk);
CompletableFuture <Chunk> cf = Bukkit.getServer().getWorld(world).getChunkAtAsync​(x,z);
Bukkit.getServer().getWorld(world).setChunkForceLoaded(x, z, true);

It can’t be this straightforward, right? :rofl:

set the chunk to loaded in the future, that way it will set the chunk as force loaded once it’s loaded

@electronicboy A little delayed on the response, but how does this look?

                   try {
                       CompletableFuture<Chunk> cf = Bukkit.getServer().getWorld(world).getChunkAtAsync(x, z);
                       cf.thenAccept(marked -> {Bukkit.getServer().getWorld(world).setChunkForceLoaded(x, z, true); });

                       Utilities.chunks.add(chunk);
                       Utilities.data.set("chunks", new ArrayList<>(Utilities.chunks));
                   }catch(Exception e){
                       Utilities.msg(s, "FAILED to async load chunk (" + x + "," + z + ") in world '" + world + "'...");
                   }