From 37b557548479379e404053d9721f612961f3c5b2 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 11 Jul 2021 03:14:17 +0200 Subject: [PATCH] Support async chunk loading --- .../java/net/minestom/server/instance/AnvilLoader.java | 10 ++++++++++ .../net/minestom/server/instance/IChunkLoader.java | 5 ++--- .../minestom/server/instance/InstanceContainer.java | 8 ++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/AnvilLoader.java b/src/main/java/net/minestom/server/instance/AnvilLoader.java index 20384befe..97aa9e2bc 100644 --- a/src/main/java/net/minestom/server/instance/AnvilLoader.java +++ b/src/main/java/net/minestom/server/instance/AnvilLoader.java @@ -254,4 +254,14 @@ public class AnvilLoader implements IChunkLoader { } chunkColumn.setTileEntities(tileEntities); } + + @Override + public boolean supportsParallelLoading() { + return true; + } + + @Override + public boolean supportsParallelSaving() { + return true; + } } diff --git a/src/main/java/net/minestom/server/instance/IChunkLoader.java b/src/main/java/net/minestom/server/instance/IChunkLoader.java index 07423693d..7d4a2b773 100644 --- a/src/main/java/net/minestom/server/instance/IChunkLoader.java +++ b/src/main/java/net/minestom/server/instance/IChunkLoader.java @@ -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 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 saveChunks(@NotNull Collection chunks) { if (supportsParallelSaving()) { diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index defc672f2..8e00799d4 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -318,7 +318,7 @@ public class InstanceContainer extends Instance { protected CompletableFuture retrieveChunk(int chunkX, int chunkZ) { CompletableFuture 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; }