Generalize MCA implementation a bit more

This commit is contained in:
Lukas Rieger (Blue) 2023-04-12 18:06:50 +02:00
parent 8a85deb630
commit b3875597a4
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
5 changed files with 38 additions and 32 deletions

View File

@ -54,7 +54,6 @@ protected MCAChunk(MCAWorld world, CompoundTag chunkTag) {
@Override
public abstract boolean isGenerated();
@Override
public int getDataVersion() {
return dataVersion;
}
@ -100,10 +99,6 @@ public static MCAChunk create(MCAWorld world, CompoundTag chunkTag) throws IOExc
return new ChunkAnvil118(world, chunkTag);
}
public static MCAChunk empty() {
return EmptyChunk.INSTANCE;
}
@Override
public String toString() {
return "MCAChunk{" +

View File

@ -26,6 +26,8 @@
import com.flowpowered.math.vector.Vector2i;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.world.Chunk;
import de.bluecolored.bluemap.core.world.EmptyChunk;
import de.bluecolored.bluemap.core.world.Region;
import net.querz.nbt.CompoundTag;
import net.querz.nbt.Tag;
@ -55,8 +57,8 @@ public MCARegion(MCAWorld world, File regionFile) throws IllegalArgumentExceptio
}
@Override
public MCAChunk loadChunk(int chunkX, int chunkZ, boolean ignoreMissingLightData) throws IOException {
if (!regionFile.exists() || regionFile.length() == 0) return MCAChunk.empty();
public Chunk loadChunk(int chunkX, int chunkZ, boolean ignoreMissingLightData) throws IOException {
if (!regionFile.exists() || regionFile.length() == 0) return EmptyChunk.INSTANCE;
try (RandomAccessFile raf = new RandomAccessFile(regionFile, "r")) {
@ -70,7 +72,7 @@ public MCAChunk loadChunk(int chunkX, int chunkZ, boolean ignoreMissingLightData
int size = raf.readByte() * 4096;
if (size == 0) {
return MCAChunk.empty();
return EmptyChunk.INSTANCE;
}
raf.seek(offset + 4); // +4 skip chunk size
@ -85,7 +87,7 @@ public MCAChunk loadChunk(int chunkX, int chunkZ, boolean ignoreMissingLightData
Tag<?> tag = Tag.deserialize(dis, Tag.DEFAULT_MAX_DEPTH);
if (tag instanceof CompoundTag) {
MCAChunk chunk = MCAChunk.create(world, (CompoundTag) tag);
if (!chunk.isGenerated()) return MCAChunk.empty();
if (!chunk.isGenerated()) return EmptyChunk.INSTANCE;
return chunk;
} else {
throw new IOException("Invalid data tag: " + (tag == null ? "null" : tag.getClass().getName()));

View File

@ -32,8 +32,7 @@
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Vector2iCache;
import de.bluecolored.bluemap.core.world.Grid;
import de.bluecolored.bluemap.core.world.World;
import de.bluecolored.bluemap.core.world.*;
import net.querz.nbt.CompoundTag;
import net.querz.nbt.NBTUtil;
@ -64,8 +63,8 @@ public class MCAWorld implements World {
private final int skyLight;
private final boolean ignoreMissingLightData;
private final LoadingCache<Vector2i, MCARegion> regionCache;
private final LoadingCache<Vector2i, MCAChunk> chunkCache;
private final LoadingCache<Vector2i, Region> regionCache;
private final LoadingCache<Vector2i, Chunk> chunkCache;
public MCAWorld(Path worldFolder, int skyLight, boolean ignoreMissingLightData) throws IOException {
this.worldFolder = worldFolder.toRealPath();
@ -102,25 +101,25 @@ public MCAWorld(Path worldFolder, int skyLight, boolean ignoreMissingLightData)
}
@Override
public MCAChunk getChunkAtBlock(int x, int y, int z) {
public Chunk getChunkAtBlock(int x, int y, int z) {
return getChunk(x >> 4, z >> 4);
}
@Override
public MCAChunk getChunk(int x, int z) {
public Chunk getChunk(int x, int z) {
return getChunk(VECTOR_2_I_CACHE.get(x, z));
}
private MCAChunk getChunk(Vector2i pos) {
private Chunk getChunk(Vector2i pos) {
return chunkCache.get(pos);
}
@Override
public MCARegion getRegion(int x, int z) {
public Region getRegion(int x, int z) {
return getRegion(VECTOR_2_I_CACHE.get(x, z));
}
private MCARegion getRegion(Vector2i pos) {
private Region getRegion(Vector2i pos) {
return regionCache.get(pos);
}
@ -218,20 +217,20 @@ private File getMCAFile(int regionX, int regionZ) {
return getRegionFolder().resolve("r." + regionX + "." + regionZ + ".mca").toFile();
}
private MCARegion loadRegion(Vector2i regionPos) {
private Region loadRegion(Vector2i regionPos) {
return loadRegion(regionPos.getX(), regionPos.getY());
}
private MCARegion loadRegion(int x, int z) {
Region loadRegion(int x, int z) {
File regionPath = getMCAFile(x, z);
return new MCARegion(this, regionPath);
}
private MCAChunk loadChunk(Vector2i chunkPos) {
private Chunk loadChunk(Vector2i chunkPos) {
return loadChunk(chunkPos.getX(), chunkPos.getY());
}
private MCAChunk loadChunk(int x, int z) {
Chunk loadChunk(int x, int z) {
final int tries = 3;
final int tryInterval = 1000;
@ -256,7 +255,7 @@ private MCAChunk loadChunk(int x, int z) {
}
Logger.global.logDebug("Unexpected exception trying to load chunk (x:" + x + ", z:" + z + "):" + loadException);
return MCAChunk.empty();
return EmptyChunk.INSTANCE;
}

View File

@ -28,8 +28,6 @@ public interface Chunk {
boolean isGenerated();
int getDataVersion();
long getInhabitedTime();
BlockState getBlockState(int x, int y, int z);

View File

@ -22,15 +22,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.mca;
package de.bluecolored.bluemap.core.world;
import de.bluecolored.bluemap.core.world.Biome;
import de.bluecolored.bluemap.core.world.BlockState;
import de.bluecolored.bluemap.core.world.LightData;
public class EmptyChunk implements Chunk {
public class EmptyChunk extends MCAChunk {
public static final MCAChunk INSTANCE = new EmptyChunk();
public static final Chunk INSTANCE = new EmptyChunk();
@Override
public boolean isGenerated() {
@ -57,4 +53,20 @@ public String getBiome(int x, int y, int z) {
return Biome.DEFAULT.getFormatted();
}
@Override
public int getMaxY(int x, int z) {
return 255;
}
@Override
public int getMinY(int x, int z) {
return 0;
}
@Override
public int getWorldSurfaceY(int x, int z) { return 0; }
@Override
public int getOceanFloorY(int x, int z) { return 0; }
}