diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 1e0b19014..d0bca84e7 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -167,8 +167,9 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @param chunkX the chunk X * @param chunkZ the chunk Z + * @return a {@link CompletableFuture} completed once the chunk has been loaded */ - public abstract CompletableFuture loadChunk(int chunkX, int chunkZ); + public abstract @NotNull CompletableFuture<@NotNull Chunk> loadChunk(int chunkX, int chunkZ); /** * Loads the chunk if the chunk is already loaded or if @@ -176,9 +177,9 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @param chunkX the chunk X * @param chunkZ the chunk Z - * @return a {@link CompletableFuture} completed once the chunk has been processed + * @return a {@link CompletableFuture} completed once the chunk has been processed, can be null if not loaded */ - public abstract @NotNull CompletableFuture loadOptionalChunk(int chunkX, int chunkZ); + public abstract @NotNull CompletableFuture<@Nullable Chunk> loadOptionalChunk(int chunkX, int chunkZ); /** * Schedules the removal of a {@link Chunk}, this method does not promise when it will be done. @@ -200,30 +201,29 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * @param chunkZ the chunk Z * @return the chunk at the specified position, null if not loaded */ - @Nullable - public abstract Chunk getChunk(int chunkX, int chunkZ); + public abstract @Nullable Chunk getChunk(int chunkX, int chunkZ); /** * Saves a {@link Chunk} to permanent storage. * * @param chunk the {@link Chunk} to save + * @return future called when the chunk is done saving */ - public abstract CompletableFuture saveChunkToStorage(@NotNull Chunk chunk); + public abstract @NotNull CompletableFuture saveChunkToStorage(@NotNull Chunk chunk); /** * Saves multiple chunks to permanent storage. * - * @param callback optional callback called when the chunks are done saving + * @return future called when the chunks are done saving */ - public abstract CompletableFuture saveChunksToStorage(); + public abstract @NotNull CompletableFuture saveChunksToStorage(); /** * Gets the instance {@link ChunkGenerator}. * * @return the {@link ChunkGenerator} of the instance */ - @Nullable - public abstract ChunkGenerator getChunkGenerator(); + public abstract @Nullable ChunkGenerator getChunkGenerator(); /** * Changes the instance {@link ChunkGenerator}. @@ -237,8 +237,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @return an unmodifiable containing all the instance chunks */ - @NotNull - public abstract Collection getChunks(); + public abstract @NotNull Collection<@NotNull Chunk> getChunks(); /** * Gets the instance {@link StorageLocation}. @@ -489,9 +488,9 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta /** * Loads the chunk at the given {@link Point} with a callback. * - * @param point the chunk position + * @param point the chunk position */ - public CompletableFuture loadChunk(@NotNull Point point) { + public @NotNull CompletableFuture<@NotNull Chunk> loadChunk(@NotNull Point point) { final int chunkX = ChunkUtils.getChunkCoordinate(point.x()); final int chunkZ = ChunkUtils.getChunkCoordinate(point.z()); return loadChunk(chunkX, chunkZ); @@ -502,9 +501,9 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * at the given {@link Point} with a callback. * * @param point the chunk position - * @return a {@link CompletableFuture} completed once the chunk has been processed + * @return a {@link CompletableFuture} completed once the chunk has been processed, null if not loaded */ - public @NotNull CompletableFuture loadOptionalChunk(@NotNull Point point) { + public @NotNull CompletableFuture<@Nullable Chunk> loadOptionalChunk(@NotNull Point point) { final int chunkX = ChunkUtils.getChunkCoordinate(point.x()); final int chunkZ = ChunkUtils.getChunkCoordinate(point.z()); return loadOptionalChunk(chunkX, chunkZ); diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 8e00799d4..be8919722 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -238,7 +238,7 @@ public class InstanceContainer extends Instance { } @Override - public CompletableFuture loadChunk(int chunkX, int chunkZ) { + public @NotNull CompletableFuture loadChunk(int chunkX, int chunkZ) { final Chunk chunk = getChunk(chunkX, chunkZ); if (chunk != null) { // Chunk already loaded @@ -306,17 +306,17 @@ public class InstanceContainer extends Instance { } @Override - public CompletableFuture saveChunkToStorage(@NotNull Chunk chunk) { + public @NotNull CompletableFuture saveChunkToStorage(@NotNull Chunk chunk) { return chunkLoader.saveChunk(chunk); } @Override - public CompletableFuture saveChunksToStorage() { + public @NotNull CompletableFuture saveChunksToStorage() { Collection chunksCollection = chunks.values(); return chunkLoader.saveChunks(chunksCollection); } - protected CompletableFuture retrieveChunk(int chunkX, int chunkZ) { + protected @NotNull CompletableFuture<@NotNull Chunk> retrieveChunk(int chunkX, int chunkZ) { CompletableFuture completableFuture = new CompletableFuture<>(); final Runnable loader = () -> chunkLoader.loadChunk(this, chunkX, chunkZ) .whenComplete((chunk, throwable) -> { @@ -331,8 +331,7 @@ public class InstanceContainer extends Instance { }); } else { // Not present - createChunk(chunkX, chunkZ).whenComplete((c, t) -> - completableFuture.complete(c)); + createChunk(chunkX, chunkZ).thenAccept(completableFuture::complete); } }); if (chunkLoader.supportsParallelLoading()) { @@ -344,7 +343,7 @@ public class InstanceContainer extends Instance { return completableFuture; } - protected CompletableFuture createChunk(int chunkX, int chunkZ) { + protected @NotNull CompletableFuture<@NotNull Chunk> createChunk(int chunkX, int chunkZ) { Biome[] biomes = new Biome[Biome.getBiomeCount(getDimensionType())]; if (chunkGenerator == null) { Arrays.fill(biomes, MinecraftServer.getBiomeManager().getById(0)); @@ -365,13 +364,12 @@ public class InstanceContainer extends Instance { if (chunkGenerator != null && chunk.shouldGenerate()) { // Execute the chunk generator to populate the chunk final ChunkGenerationBatch chunkBatch = new ChunkGenerationBatch(this, chunk); - return chunkBatch.generate(chunkGenerator) .whenComplete((c, t) -> chunkRegisterCallback.accept(c)); } else { // No chunk generator, execute the callback with the empty chunk - return CompletableFuture.completedFuture(chunk) - .whenComplete((c, t) -> chunkRegisterCallback.accept(c)); + chunkRegisterCallback.accept(chunk); + return CompletableFuture.completedFuture(chunk); } } diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index fbf676aa7..fd3b15c29 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -43,7 +43,7 @@ public class SharedInstance extends Instance { } @Override - public CompletableFuture loadChunk(int chunkX, int chunkZ) { + public @NotNull CompletableFuture loadChunk(int chunkX, int chunkZ) { return instanceContainer.loadChunk(chunkX, chunkZ); } @@ -63,12 +63,12 @@ public class SharedInstance extends Instance { } @Override - public CompletableFuture saveChunkToStorage(@NotNull Chunk chunk) { + public @NotNull CompletableFuture saveChunkToStorage(@NotNull Chunk chunk) { return instanceContainer.saveChunkToStorage(chunk); } @Override - public CompletableFuture saveChunksToStorage() { + public @NotNull CompletableFuture saveChunksToStorage() { return instanceContainer.saveChunksToStorage(); } diff --git a/src/main/java/net/minestom/server/instance/batch/ChunkGenerationBatch.java b/src/main/java/net/minestom/server/instance/batch/ChunkGenerationBatch.java index ba04d7a72..933011c99 100644 --- a/src/main/java/net/minestom/server/instance/batch/ChunkGenerationBatch.java +++ b/src/main/java/net/minestom/server/instance/batch/ChunkGenerationBatch.java @@ -24,7 +24,7 @@ public class ChunkGenerationBatch extends ChunkBatch { chunk.setBlock(x, y, z, block); } - public CompletableFuture generate(@NotNull ChunkGenerator chunkGenerator) { + public @NotNull CompletableFuture<@NotNull Chunk> generate(@NotNull ChunkGenerator chunkGenerator) { final CompletableFuture completableFuture = new CompletableFuture<>(); BLOCK_BATCH_POOL.execute(() -> { synchronized (chunk) { diff --git a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java index 0ad525306..907c2eea0 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java @@ -31,8 +31,8 @@ public final class ChunkUtils { * @param eachCallback the optional callback when a chunk get loaded * @return a {@link CompletableFuture} completed once all chunks have been processed */ - public static CompletableFuture optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks, - @Nullable ChunkCallback eachCallback) { + public static @NotNull CompletableFuture<@Nullable Chunk> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks, + @Nullable ChunkCallback eachCallback) { CompletableFuture completableFuture = new CompletableFuture<>(); final int length = chunks.length; AtomicInteger counter = new AtomicInteger(0);