Skip to content

Commit

Permalink
refactor: Tidy AllayChunkService::loadChunk and add AllayChunkService…
Browse files Browse the repository at this point in the history
…::loadChunkImmediately method
  • Loading branch information
smartcmd committed Sep 1, 2023
1 parent 9105270 commit 36baa77
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public interface ChunkService extends ChunkAccessible {

CompletableFuture<Chunk> getOrLoadChunk(int x, int z);

@SlowOperation
CompletableFuture<Chunk> loadChunk(int x, int z);

@SlowOperation
Chunk loadChunkImmediately(int x, int z);

void unloadChunk(int x, int z);

void unloadChunk(long chunkHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.longs.*;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.cloudburstmc.protocol.bedrock.data.HeightMapDataType;
import org.cloudburstmc.protocol.bedrock.data.SubChunkData;
import org.cloudburstmc.protocol.bedrock.data.SubChunkRequestResult;
import org.cloudburstmc.protocol.bedrock.packet.SubChunkPacket;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.Vector3i;
Expand Down Expand Up @@ -201,7 +203,6 @@ public CompletableFuture<Chunk> getOrLoadChunk(int x, int z) {
return loadChunk(x, z);
}

@SlowOperation
@Override
public CompletableFuture<Chunk> loadChunk(int x, int z) {
var hashXZ = HashUtils.hashXZ(x, z);
Expand All @@ -214,23 +215,37 @@ public CompletableFuture<Chunk> loadChunk(int x, int z) {
}
var future = worldStorage.readChunk(x, z);
loadingChunks.put(hashXZ, future);
future.thenApplyAsync(loadedChunk -> {
if (loadedChunk != null) {
setChunk(x, z, loadedChunk);
loadingChunks.remove(hashXZ);
return loadedChunk;
}
var unsafeChunk = AllayUnsafeChunk.builder().emptyChunk(x, z, getWorldStorage().getWorldDataCache().getDimensionInfo());
var chunkGenerateContext = new ChunkGenerateContext(unsafeChunk, world);
world.getWorldGenerator().generate(chunkGenerateContext);
var safeChunk = new AllayChunk(unsafeChunk);
setChunk(x, z, safeChunk);
loadingChunks.remove(hashXZ);
return safeChunk;
}, Server.getInstance().getComputeThreadPool());
future.thenApplyAsync(loadedChunk -> generateChunkIfNull(x, z, loadedChunk), Server.getInstance().getComputeThreadPool());
return future;
}

@SneakyThrows
@SlowOperation
@Override
public Chunk loadChunkImmediately(int x, int z) {
return generateChunkIfNull(
x, z,
worldStorage.readChunk(x, z).get()
);
}

private Chunk generateChunkIfNull(int x, int z, Chunk loadedChunk) {
long hashXZ = HashUtils.hashXZ(x, z);
if (loadedChunk == null) {
loadedChunk = generateChunkImmediately(x, z);
}
setChunk(x, z, loadedChunk);
loadingChunks.remove(hashXZ);
return loadedChunk;
}

private AllayChunk generateChunkImmediately(int x, int z) {
var unsafeChunk = AllayUnsafeChunk.builder().emptyChunk(x, z, getWorldStorage().getWorldDataCache().getDimensionInfo());
var chunkGenerateContext = new ChunkGenerateContext(unsafeChunk, world);
world.getWorldGenerator().generate(chunkGenerateContext);
return new AllayChunk(unsafeChunk);
}

public void unloadChunk(int x, int z) {
unloadChunk(HashUtils.hashXZ(x, z));
}
Expand Down

0 comments on commit 36baa77

Please sign in to comment.