From 7deccaf19c1c8fb20dfa13daadae5943e4100f3d Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Thu, 4 Apr 2024 19:23:40 +0800 Subject: [PATCH] fix: fix a possible NPE in chunk service --- .../allaymc/server/world/service/AllayChunkService.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Allay-Server/src/main/java/org/allaymc/server/world/service/AllayChunkService.java b/Allay-Server/src/main/java/org/allaymc/server/world/service/AllayChunkService.java index af56f8d8a..aa2acd742 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/world/service/AllayChunkService.java +++ b/Allay-Server/src/main/java/org/allaymc/server/world/service/AllayChunkService.java @@ -176,15 +176,16 @@ public CompletableFuture loadChunk(int x, int z) { if (isChunkLoaded(hashXZ)) { throw new IllegalStateException("Chunk is already loaded"); } + CompletableFuture future; // Prevent multiple threads from putting the same chunk into loadingChunks at the same time and wasting computing resources - var presentValue = loadingChunks.putIfAbsent(hashXZ, worldStorage.readChunk(x, z, DimensionInfo.OVERWORLD) + var presentValue = loadingChunks.putIfAbsent(hashXZ, future = worldStorage.readChunk(x, z, DimensionInfo.OVERWORLD) .exceptionally(t -> { - log.error("Error while reading chunk (" + x + "," + z + ") !", t); + log.error("Error while reading chunk ({},{}) !", x, z, t); return AllayUnsafeChunk.builder().emptyChunk(x, z, dimension.getDimensionInfo()).toSafeChunk(); }) .thenApplyAsync(this::generateChunkIfNeed, Server.getInstance().getComputeThreadPool()) .exceptionally(t -> { - log.error("Error while generating chunk (" + x + "," + z + ") !", t); + log.error("Error while generating chunk ({},{}) !", x, z, t); return AllayUnsafeChunk.builder().emptyChunk(x, z, dimension.getDimensionInfo()).toSafeChunk(); }) .thenApply(prepareChunk -> { @@ -196,7 +197,7 @@ public CompletableFuture loadChunk(int x, int z) { }) ); if (presentValue == null) { - return loadingChunks.get(hashXZ); + return future; } else { return presentValue; }