mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 07:28:19 +01:00
Cleanup optionalLoadAll
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
6ecede145e
commit
9f1fcd89ef
@ -4,7 +4,6 @@ import net.minestom.server.coordinate.Point;
|
|||||||
import net.minestom.server.coordinate.Vec;
|
import net.minestom.server.coordinate.Vec;
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.utils.callback.OptionalCallback;
|
|
||||||
import net.minestom.server.utils.function.IntegerBiConsumer;
|
import net.minestom.server.utils.function.IntegerBiConsumer;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -12,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public final class ChunkUtils {
|
public final class ChunkUtils {
|
||||||
@ -33,21 +33,17 @@ public final class ChunkUtils {
|
|||||||
* @return a {@link CompletableFuture} completed once all chunks have been processed
|
* @return a {@link CompletableFuture} completed once all chunks have been processed
|
||||||
*/
|
*/
|
||||||
public static @NotNull CompletableFuture<Void> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks,
|
public static @NotNull CompletableFuture<Void> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks,
|
||||||
@Nullable ChunkCallback eachCallback) {
|
@Nullable Consumer<Chunk> eachCallback) {
|
||||||
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
|
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
|
||||||
AtomicInteger counter = new AtomicInteger(0);
|
AtomicInteger counter = new AtomicInteger(0);
|
||||||
for (long visibleChunk : chunks) {
|
for (long visibleChunk : chunks) {
|
||||||
// WARNING: if autoload is disabled and no chunks are loaded beforehand, player will be stuck.
|
// WARNING: if autoload is disabled and no chunks are loaded beforehand, player will be stuck.
|
||||||
instance.loadOptionalChunk(getChunkCoordX(visibleChunk), getChunkCoordZ(visibleChunk))
|
instance.loadOptionalChunk(getChunkCoordX(visibleChunk), getChunkCoordZ(visibleChunk))
|
||||||
.thenAccept((chunk) -> {
|
.thenAccept((chunk) -> {
|
||||||
OptionalCallback.execute(eachCallback, chunk);
|
if (eachCallback != null) eachCallback.accept(chunk);
|
||||||
final boolean isLast = counter.get() == chunks.length - 1;
|
if (counter.incrementAndGet() == chunks.length) {
|
||||||
if (isLast) {
|
|
||||||
// This is the last chunk to be loaded , spawn player
|
// This is the last chunk to be loaded , spawn player
|
||||||
completableFuture.complete(null);
|
completableFuture.complete(null);
|
||||||
} else {
|
|
||||||
// Increment the counter of current loaded chunks
|
|
||||||
counter.incrementAndGet();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user