Cleanup optionalLoadAll

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-06-25 11:02:10 +02:00
parent 6ecede145e
commit 9f1fcd89ef

View File

@ -4,7 +4,6 @@ import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.function.IntegerBiConsumer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@ -12,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
@ApiStatus.Internal
public final class ChunkUtils {
@ -33,21 +33,17 @@ public final class ChunkUtils {
* @return a {@link CompletableFuture} completed once all chunks have been processed
*/
public static @NotNull CompletableFuture<Void> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks,
@Nullable ChunkCallback eachCallback) {
@Nullable Consumer<Chunk> eachCallback) {
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
AtomicInteger counter = new AtomicInteger(0);
for (long visibleChunk : chunks) {
// WARNING: if autoload is disabled and no chunks are loaded beforehand, player will be stuck.
instance.loadOptionalChunk(getChunkCoordX(visibleChunk), getChunkCoordZ(visibleChunk))
.thenAccept((chunk) -> {
OptionalCallback.execute(eachCallback, chunk);
final boolean isLast = counter.get() == chunks.length - 1;
if (isLast) {
if (eachCallback != null) eachCallback.accept(chunk);
if (counter.incrementAndGet() == chunks.length) {
// This is the last chunk to be loaded , spawn player
completableFuture.complete(null);
} else {
// Increment the counter of current loaded chunks
counter.incrementAndGet();
}
});
}