Skip to content

Commit

Permalink
fix: avoid unnecessary exception during world generating
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 19, 2024
1 parent 90957dd commit 77dc998
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ default void createBlockEntityAt(int x, int y, int z, Dimension dimension) {
}
var presentBlockEntity = chunk.getBlockEntity(x & 15, y, z & 15);
if (presentBlockEntity != null) {
throw new IllegalStateException("Trying to create a block entity in Dimension: " + dimension + " at pos " + x + ", " + y + ", " + z + "!");
throw new IllegalStateException("Trying to create a block entity where block entity already exists! Dimension: " + dimension + " at pos " + x + ", " + y + ", " + z + "!");
}
var blockEntity = getBlockEntityType().createBlockEntity(SimpleBlockEntityInitInfo.builder().pos(x, y, z).dimension(dimension).build());
chunk.addBlockEntity(blockEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ public Chunk generateChunk(int x, int z) {
}

private Chunk getOrGenerateProtoChunk(int x, int z) {
if (protoChunkBeingSet(x, z)) {
log.error("Trying to access a proto chunk which is being set: x: " + x + ", z: " + z);
return AllayUnsafeChunk.builder().emptyChunk(x, z, dimension.getDimensionInfo()).toSafeChunk();
}
CompletableFuture<Chunk> future = new CompletableFuture<>();
// 这里的putIfAbsent()保证了只有一个线程可以写入future,其他线程只能获取这一个线程写入的future
var presentFuture = protoChunks.putIfAbsent(HashUtils.hashXZ(x, z), future);
Expand Down Expand Up @@ -207,7 +203,7 @@ public Chunk getChunk(int x, int z) {

// 先获取future,避免在此过程中区块完成加载导致future被删除
CompletableFuture<Chunk> future = chunkService.getChunkLoadingFuture(x, z);
if (protoChunkBeingSet(x, z)) {
if (future != null && protoChunkBeingSet(x, z)) {
// 即使在此过程中future被删除(区块完成加载),此代码依然可以工作因为
// 区块完成加载后,future.join()等效于chunkService.getChunk(x, z)
return future.join();
Expand Down

0 comments on commit 77dc998

Please sign in to comment.