diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/BatchGenerationEnvironment.java b/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/BatchGenerationEnvironment.java index 50331216c..de96dbf15 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/BatchGenerationEnvironment.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/BatchGenerationEnvironment.java @@ -38,10 +38,7 @@ import com.seibel.lod.core.wrapperInterfaces.chunk.IChunkWrapper; import com.seibel.lod.core.wrapperInterfaces.worldGeneration.AbstractBatchGenerationEnvironmentWrapper; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.LinkedList; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -182,11 +179,14 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv private AtomicReference regionFileStorageCache = new AtomicReference<>(); - public RegionFileStorageExternalCache getOrCreateRegionFileCache(RegionFileStorage storage) { + public RegionFileStorageExternalCache getOrCreateRegionFileCache(RegionFileStorage storage) + { RegionFileStorageExternalCache cache = regionFileStorageCache.get(); - if (cache == null) { + if (cache == null) + { cache = new RegionFileStorageExternalCache(storage); - if (!regionFileStorageCache.compareAndSet(null, cache)) { + if (!regionFileStorageCache.compareAndSet(null, cache)) + { cache = regionFileStorageCache.get(); } } diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/ChunkLoader.java b/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/ChunkLoader.java index 66d6188dd..dc5743922 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/ChunkLoader.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/ChunkLoader.java @@ -200,13 +200,14 @@ public class ChunkLoader #else CompoundTag tagLevel = chunkData; #endif - + ChunkPos actualPos = new ChunkPos(tagLevel.getInt("xPos"), tagLevel.getInt("zPos")); - if (!Objects.equals(chunkPos, actualPos)) { - LOGGER.error("Chunk file at {} is in the wrong location; Ignoring. (Expected {}, got {})", chunkPos, chunkPos, actualPos); + if (!Objects.equals(chunkPos, actualPos)) + { + LOGGER.error("Chunk file at "+chunkPos+" is in the wrong location; Ignoring. (Expected "+chunkPos+", got "+actualPos+")"); return null; } - + ChunkStatus.ChunkType chunkType = readChunkType(tagLevel); #if PRE_MC_1_18_1 if (chunkType != ChunkStatus.ChunkType.LEVELCHUNK) diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/RegionFileStorageExternalCache.java b/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/RegionFileStorageExternalCache.java index 38405c60c..47eb22fa7 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/RegionFileStorageExternalCache.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/worldGeneration/mimicObject/RegionFileStorageExternalCache.java @@ -43,50 +43,80 @@ public class RegionFileStorageExternalCache implements AutoCloseable { } @Nullable - public RegionFile getRegionFile(ChunkPos pos) throws IOException { - long posLong = ChunkPos.asLong(pos.getRegionX(), pos.getRegionZ()); - RegionFile rFile; - // First check our custom cache - for (RegionFileCache cache : this.regionFileCache) { - if (cache.pos == posLong) return cache.file; - } - - // Then check vanilla cache - while (true) { - try { - rFile = this.storage.regionCache.getOrDefault(posLong, null); - break; - } catch (ArrayIndexOutOfBoundsException e) { - BatchGenerationEnvironment.LOAD_LOGGER.warn("Concurrency issue detected when getting region file for chunk at " + pos + ". Retrying..."); - } - } - if (rFile != null) return rFile; - - // Otherwise, check if file exist, and if so, add it to the cache - Path p = storage.folder; - if (!Files.exists(p)) return null; - Path rFilePath = p.resolve("r." + pos.getRegionX() + "." + pos.getRegionZ() + ".mca"); - rFile = new RegionFile(rFilePath, p, false); - regionFileCache.add(new RegionFileCache(ChunkPos.asLong(pos.getRegionX(), pos.getRegionZ()), rFile)); - while (regionFileCache.size() > MAX_CACHE_SIZE) { - regionFileCache.poll().file.close(); - } - return rFile; - } - - + public RegionFile getRegionFile(ChunkPos pos) throws IOException + { + long posLong = ChunkPos.asLong(pos.getRegionX(), pos.getRegionZ()); + RegionFile rFile; + // First check our custom cache + for (RegionFileCache cache : this.regionFileCache) + { + if (cache.pos == posLong) + { + return cache.file; + } + } + + // Then check vanilla cache + while (true) + { + try + { + rFile = this.storage.regionCache.getOrDefault(posLong, null); + break; + } + catch (ArrayIndexOutOfBoundsException e) + { + BatchGenerationEnvironment.LOAD_LOGGER.warn("Concurrency issue detected when getting region file for chunk at " + pos + ". Retrying..."); + } + } + + if (rFile != null) + { + return rFile; + } + + // Otherwise, check if file exist, and if so, add it to the cache + Path p = storage.folder; + if (!Files.exists(p)) + { + return null; + } + + Path rFilePath = p.resolve("r." + pos.getRegionX() + "." + pos.getRegionZ() + ".mca"); + rFile = new RegionFile(rFilePath, p, false); + regionFileCache.add(new RegionFileCache(ChunkPos.asLong(pos.getRegionX(), pos.getRegionZ()), rFile)); + while (regionFileCache.size() > MAX_CACHE_SIZE) + { + regionFileCache.poll().file.close(); + } + + return rFile; + } + + @Nullable - public CompoundTag read(ChunkPos pos) throws IOException { - RegionFile file = getRegionFile(pos); - if (file == null) return null; - - try (DataInputStream stream = file.getChunkDataInputStream(pos)) { - if (stream == null) return null; - return NbtIo.read(stream); - } - catch (Throwable e) { - return null; - } - } + public CompoundTag read(ChunkPos pos) throws IOException + { + RegionFile file = getRegionFile(pos); + if (file == null) + { + return null; + } + + + try (DataInputStream stream = file.getChunkDataInputStream(pos)) + { + if (stream == null) + { + return null; + } + + return NbtIo.read(stream); + } + catch (Throwable e) + { + return null; + } + } } diff --git a/coreSubProjects b/coreSubProjects index e00d1c95e..f58ac4b64 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit e00d1c95e3a84a54f8965002a2b21b5f6e777f2e +Subproject commit f58ac4b647ee20a5f1a79159c30f18555629b071