From a074117ea5209cead8356b2f500100c6c654390d Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 20 Jul 2021 19:10:53 +0200 Subject: [PATCH] Simplify `optionalLoadAll` --- .../net/minestom/server/entity/Player.java | 2 +- .../server/utils/chunk/ChunkUtils.java | 38 +++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index e267d097f..b01fdec8e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -546,7 +546,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, final long[] visibleChunks = ChunkUtils.getChunksInRange(spawnPosition, 0); return ChunkUtils.optionalLoadAll(instance, visibleChunks, null) - .thenAccept(chunk -> spawnPlayer(instance, spawnPosition, firstSpawn, dimensionChange, true)); + .thenRun(() -> spawnPlayer(instance, spawnPosition, firstSpawn, dimensionChange, true)); } else { // The player already has the good version of all the chunks. // We just need to refresh his entity viewing list and add him to the instance 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 13970de9b..82d566e70 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java @@ -6,16 +6,17 @@ import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.callback.OptionalCallback; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicInteger; +@ApiStatus.Internal public final class ChunkUtils { private ChunkUtils() { - } /** @@ -31,29 +32,24 @@ 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 @NotNull CompletableFuture<@Nullable Chunk> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks, - @Nullable ChunkCallback eachCallback) { - CompletableFuture completableFuture = new CompletableFuture<>(); - final int length = chunks.length; + public static @NotNull CompletableFuture optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks, + @Nullable ChunkCallback eachCallback) { + CompletableFuture completableFuture = new CompletableFuture<>(); AtomicInteger counter = new AtomicInteger(0); for (long visibleChunk : chunks) { - final int chunkX = ChunkUtils.getChunkCoordX(visibleChunk); - final int chunkZ = ChunkUtils.getChunkCoordZ(visibleChunk); - - final ChunkCallback callback = (chunk) -> { - OptionalCallback.execute(eachCallback, chunk); - final boolean isLast = counter.get() == length - 1; - if (isLast) { - // This is the last chunk to be loaded , spawn player - completableFuture.complete(chunk); - } else { - // Increment the counter of current loaded chunks - counter.incrementAndGet(); - } - }; - // WARNING: if auto-load is disabled and no chunks are loaded beforehand, player will be stuck. - instance.loadOptionalChunk(chunkX, chunkZ).thenAccept(callback); + instance.loadOptionalChunk(getChunkCoordX(visibleChunk), getChunkCoordZ(visibleChunk)) + .thenAccept((chunk) -> { + OptionalCallback.execute(eachCallback, chunk); + final boolean isLast = counter.get() == chunks.length - 1; + if (isLast) { + // This is the last chunk to be loaded , spawn player + completableFuture.complete(null); + } else { + // Increment the counter of current loaded chunks + counter.incrementAndGet(); + } + }); } return completableFuture; }