From a8d90955c7f8bd1ddc0950674ae064c98c044196 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 22 Aug 2021 09:57:39 +0200 Subject: [PATCH] Fix chunks being registered multiple times (weirdly only happen on Windows?) Signed-off-by: TheMode --- .../minestom/server/instance/InstanceContainer.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index ae3089bd2..58d3db62d 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -49,6 +49,7 @@ public class InstanceContainer extends Instance { // (chunk index -> chunk) map, contains all the chunks in the instance // used as a monitor when access is required private final Long2ObjectMap chunks = new Long2ObjectOpenHashMap<>(); + private final Long2ObjectMap> loadingChunks = new Long2ObjectOpenHashMap<>(); private final Lock changingBlockLock = new ReentrantLock(); private final Map currentlyChangingBlocks = new HashMap<>(); @@ -259,6 +260,12 @@ public class InstanceContainer extends Instance { protected @NotNull CompletableFuture<@NotNull Chunk> retrieveChunk(int chunkX, int chunkZ) { CompletableFuture completableFuture = new CompletableFuture<>(); + synchronized (loadingChunks) { + final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); + CompletableFuture loadingChunk = loadingChunks.get(index); + if (loadingChunk != null) return loadingChunk; + this.loadingChunks.put(index, completableFuture); + } final IChunkLoader loader = chunkLoader; final Runnable retriever = () -> loader.loadChunk(this, chunkX, chunkZ) // create the chunk from scratch (with the generator) if the loader couldn't @@ -267,6 +274,9 @@ public class InstanceContainer extends Instance { .whenComplete((chunk, throwable) -> scheduleNextTick(instance -> { cacheChunk(chunk); EventDispatcher.call(new InstanceChunkLoadEvent(this, chunkX, chunkZ)); + synchronized (loadingChunks) { + this.loadingChunks.remove(ChunkUtils.getChunkIndex(chunk)); + } completableFuture.complete(chunk); })); if (loader.supportsParallelLoading()) {