Support async chunk loading

This commit is contained in:
TheMode 2021-07-11 03:14:17 +02:00
parent 1ead7c923a
commit 37b5575484
3 changed files with 18 additions and 5 deletions

View File

@ -254,4 +254,14 @@ public class AnvilLoader implements IChunkLoader {
}
chunkColumn.setTileEntities(tileEntities);
}
@Override
public boolean supportsParallelLoading() {
return true;
}
@Override
public boolean supportsParallelSaving() {
return true;
}
}

View File

@ -1,7 +1,6 @@
package net.minestom.server.instance;
import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.thread.MinestomThread;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -33,7 +32,7 @@ public interface IChunkLoader {
*
* @param chunk the {@link Chunk} to save
* @return a {@link CompletableFuture} executed when the {@link Chunk} is done saving,
* * should be called even if the saving failed (you can throw an exception).
* should be called even if the saving failed (you can throw an exception).
*/
@NotNull CompletableFuture<Void> saveChunk(@NotNull Chunk chunk);
@ -44,7 +43,7 @@ public interface IChunkLoader {
*
* @param chunks the chunks to save
* @return a {@link CompletableFuture} executed when the {@link Chunk} is done saving,
* * should be called even if the saving failed (you can throw an exception).
* should be called even if the saving failed (you can throw an exception).
*/
default @NotNull CompletableFuture<Void> saveChunks(@NotNull Collection<Chunk> chunks) {
if (supportsParallelSaving()) {

View File

@ -318,7 +318,7 @@ public class InstanceContainer extends Instance {
protected CompletableFuture<Chunk> retrieveChunk(int chunkX, int chunkZ) {
CompletableFuture<Chunk> completableFuture = new CompletableFuture<>();
chunkLoader.loadChunk(this, chunkX, chunkZ)
final Runnable loader = () -> chunkLoader.loadChunk(this, chunkX, chunkZ)
.whenComplete((chunk, throwable) -> {
if (chunk != null) {
// Successfully loaded
@ -335,7 +335,11 @@ public class InstanceContainer extends Instance {
completableFuture.complete(c));
}
});
if (chunkLoader.supportsParallelLoading()) {
CompletableFuture.runAsync(loader);
} else {
loader.run();
}
// Chunk is being loaded
return completableFuture;
}