World generator reformatting

This commit is contained in:
James Seibel
2023-06-03 21:37:12 -05:00
parent 6c5b1501e9
commit 3ff4e0f9a6
4 changed files with 87 additions and 56 deletions
@@ -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<RegionFileStorageExternalCache> 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();
}
}
@@ -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)
@@ -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;
}
}
}