mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Simplify optionalLoadAll
This commit is contained in:
parent
1dc80d6b87
commit
a074117ea5
@ -546,7 +546,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
final long[] visibleChunks = ChunkUtils.getChunksInRange(spawnPosition, 0);
|
final long[] visibleChunks = ChunkUtils.getChunksInRange(spawnPosition, 0);
|
||||||
|
|
||||||
return ChunkUtils.optionalLoadAll(instance, visibleChunks, null)
|
return ChunkUtils.optionalLoadAll(instance, visibleChunks, null)
|
||||||
.thenAccept(chunk -> spawnPlayer(instance, spawnPosition, firstSpawn, dimensionChange, true));
|
.thenRun(() -> spawnPlayer(instance, spawnPosition, firstSpawn, dimensionChange, true));
|
||||||
} else {
|
} else {
|
||||||
// The player already has the good version of all the chunks.
|
// 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
|
// We just need to refresh his entity viewing list and add him to the instance
|
||||||
|
@ -6,16 +6,17 @@ import net.minestom.server.instance.Chunk;
|
|||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.utils.MathUtils;
|
import net.minestom.server.utils.MathUtils;
|
||||||
import net.minestom.server.utils.callback.OptionalCallback;
|
import net.minestom.server.utils.callback.OptionalCallback;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
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;
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
public final class ChunkUtils {
|
public final class ChunkUtils {
|
||||||
|
|
||||||
private ChunkUtils() {
|
private ChunkUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,29 +32,24 @@ public final class ChunkUtils {
|
|||||||
* @param eachCallback the optional callback when a chunk get loaded
|
* @param eachCallback the optional callback when a chunk get loaded
|
||||||
* @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<@Nullable Chunk> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks,
|
public static @NotNull CompletableFuture<Void> optionalLoadAll(@NotNull Instance instance, long @NotNull [] chunks,
|
||||||
@Nullable ChunkCallback eachCallback) {
|
@Nullable ChunkCallback eachCallback) {
|
||||||
CompletableFuture<Chunk> completableFuture = new CompletableFuture<>();
|
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
|
||||||
final int length = chunks.length;
|
|
||||||
AtomicInteger counter = new AtomicInteger(0);
|
AtomicInteger counter = new AtomicInteger(0);
|
||||||
for (long visibleChunk : chunks) {
|
for (long visibleChunk : chunks) {
|
||||||
final int chunkX = ChunkUtils.getChunkCoordX(visibleChunk);
|
// WARNING: if auto-load is disabled and no chunks are loaded beforehand, player will be stuck.
|
||||||
final int chunkZ = ChunkUtils.getChunkCoordZ(visibleChunk);
|
instance.loadOptionalChunk(getChunkCoordX(visibleChunk), getChunkCoordZ(visibleChunk))
|
||||||
|
.thenAccept((chunk) -> {
|
||||||
final ChunkCallback callback = (chunk) -> {
|
|
||||||
OptionalCallback.execute(eachCallback, chunk);
|
OptionalCallback.execute(eachCallback, chunk);
|
||||||
final boolean isLast = counter.get() == length - 1;
|
final boolean isLast = counter.get() == chunks.length - 1;
|
||||||
if (isLast) {
|
if (isLast) {
|
||||||
// This is the last chunk to be loaded , spawn player
|
// This is the last chunk to be loaded , spawn player
|
||||||
completableFuture.complete(chunk);
|
completableFuture.complete(null);
|
||||||
} else {
|
} else {
|
||||||
// Increment the counter of current loaded chunks
|
// Increment the counter of current loaded chunks
|
||||||
counter.incrementAndGet();
|
counter.incrementAndGet();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// WARNING: if auto-load is disabled and no chunks are loaded beforehand, player will be stuck.
|
|
||||||
instance.loadOptionalChunk(chunkX, chunkZ).thenAccept(callback);
|
|
||||||
}
|
}
|
||||||
return completableFuture;
|
return completableFuture;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user