Skip to content

Commit

Permalink
formatting and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Jun 8, 2020
1 parent 08cf64b commit 49e613b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 67 deletions.
67 changes: 33 additions & 34 deletions src/main/java/net/querz/mca/Chunk.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.querz.mca;

import java.rmi.UnexpectedException;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.ListTag;
import net.querz.nbt.io.NamedTag;
Expand Down Expand Up @@ -64,45 +63,45 @@ private void initReferences(long loadFlags) {
if ((level = data.getCompoundTag("Level")) == null) {
throw new IllegalArgumentException("data does not contain \"Level\" tag");
}
this.dataVersion = data.getInt("DataVersion");
this.inhabitedTime = level.getLong("InhabitedTime");
this.lastUpdate = level.getLong("LastUpdate");
if((loadFlags & BIOMES) != 0) {
this.biomes = level.getIntArray("Biomes");
dataVersion = data.getInt("DataVersion");
inhabitedTime = level.getLong("InhabitedTime");
lastUpdate = level.getLong("LastUpdate");
if ((loadFlags & BIOMES) != 0) {
biomes = level.getIntArray("Biomes");
}
if((loadFlags & HEIGHTMAPS) != 0) {
this.heightMaps = level.getCompoundTag("Heightmaps");
if ((loadFlags & HEIGHTMAPS) != 0) {
heightMaps = level.getCompoundTag("Heightmaps");
}
if((loadFlags & CARVINGMASKS) != 0) {
this.carvingMasks = level.getCompoundTag("CarvingMasks");
if ((loadFlags & CARVING_MASKS) != 0) {
carvingMasks = level.getCompoundTag("CarvingMasks");
}
if((loadFlags & ENTITIES) != 0) {
this.entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null;
if ((loadFlags & ENTITIES) != 0) {
entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null;
}
if((loadFlags & TILE_ENTITIES) != 0) {
this.tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null;
if ((loadFlags & TILE_ENTITIES) != 0) {
tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null;
}
if((loadFlags & TILE_TICKS) != 0) {
this.tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null;
if ((loadFlags & TILE_TICKS) != 0) {
tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null;
}
if((loadFlags & LIQUID_TICKS) != 0) {
this.liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null;
if ((loadFlags & LIQUID_TICKS) != 0) {
liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null;
}
if((loadFlags & LIGHTS) != 0) {
this.lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null;
if ((loadFlags & LIGHTS) != 0) {
lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null;
}
if((loadFlags & LIQUIDS_TO_BE_TICKED) != 0) {
this.liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null;
if ((loadFlags & LIQUIDS_TO_BE_TICKED) != 0) {
liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null;
}
if((loadFlags & TO_BE_TICKED) != 0) {
this.toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null;
if ((loadFlags & TO_BE_TICKED) != 0) {
toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null;
}
if((loadFlags & POST_PROCESSING) != 0) {
this.postProcessing = level.containsKey("PostProcessing") ? level.getListTag("PostProcessing").asListTagList() : null;
if ((loadFlags & POST_PROCESSING) != 0) {
postProcessing = level.containsKey("PostProcessing") ? level.getListTag("PostProcessing").asListTagList() : null;
}
this.status = level.getString("Status");
if((loadFlags & STRUCTURES) != 0) {
this.structures = level.getCompoundTag("Structures");
status = level.getString("Status");
if ((loadFlags & STRUCTURES) != 0) {
structures = level.getCompoundTag("Structures");
}
if ((loadFlags & (BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT)) != 0 && level.containsKey("Sections")) {
for (CompoundTag section : level.getListTag("Sections").asCompoundTagList()) {
Expand All @@ -114,14 +113,14 @@ private void initReferences(long loadFlags) {
if (newSection.isEmpty()) {
continue;
}
this.sections[sectionIndex] = newSection;
sections[sectionIndex] = newSection;
}
}

// If we haven't requested the full set of data we can drop the underlying raw data to let the GC handle it.
if(loadFlags != ALL_DATA) {
this.data = null;
this.partial = true;
if (loadFlags != ALL_DATA) {
data = null;
partial = true;
} else {
partial = false;
}
Expand All @@ -137,7 +136,7 @@ private void initReferences(long loadFlags) {
* @throws IOException When something went wrong during writing.
*/
public int serialize(RandomAccessFile raf, int xPos, int zPos) throws IOException {
if(this.partial) {
if (partial) {
throw new UnsupportedOperationException("Partially loaded chunks cannot be serialized");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/net/querz/mca/LoadFlags.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package net.querz.mca;

public class LoadFlags {
public static long BIOMES = 0x0001;
public static long HEIGHTMAPS = 0x0002;
public static long CARVINGMASKS = 0x0004;
public static long ENTITIES = 0x0008;
public static long TILE_ENTITIES = 0x0010;
public static long TILE_TICKS = 0x0040;
public static long LIQUID_TICKS = 0x0080;
public static long TO_BE_TICKED = 0x0100;
public static long POST_PROCESSING = 0x0200;
public static long STRUCTURES = 0x0400;
public static long BLOCK_LIGHTS = 0x0800;
public static long BLOCK_STATES = 0x1000;
public static long SKY_LIGHT = 0x2000;
public static long LIGHTS = 0x4000;
public static long LIQUIDS_TO_BE_TICKED = 0x8000;

public static long ALL_DATA = 0xffffffffffffffffL;
public static long BIOMES = 0x0001;
public static long HEIGHTMAPS = 0x0002;
public static long CARVING_MASKS = 0x0004;
public static long ENTITIES = 0x0008;
public static long TILE_ENTITIES = 0x0010;
public static long TILE_TICKS = 0x0040;
public static long LIQUID_TICKS = 0x0080;
public static long TO_BE_TICKED = 0x0100;
public static long POST_PROCESSING = 0x0200;
public static long STRUCTURES = 0x0400;
public static long BLOCK_LIGHTS = 0x0800;
public static long BLOCK_STATES = 0x1000;
public static long SKY_LIGHT = 0x2000;
public static long LIGHTS = 0x4000;
public static long LIQUIDS_TO_BE_TICKED = 0x8000;

public static long ALL_DATA = 0xffffffffffffffffL;


}
9 changes: 2 additions & 7 deletions src/main/java/net/querz/mca/MCAUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private MCAUtil() {}
* @throws IOException if something during deserialization goes wrong.
* */
public static MCAFile read(String file) throws IOException {
return read(new File(file));
return read(new File(file), LoadFlags.ALL_DATA);
}

/**
Expand All @@ -33,14 +33,9 @@ public static MCAFile read(String file) throws IOException {
* @throws IOException if something during deserialization goes wrong.
* */
public static MCAFile read(File file) throws IOException {
MCAFile mcaFile = newMCAFile(file);
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
mcaFile.deserialize(raf);
return mcaFile;
}
return read(file, LoadFlags.ALL_DATA);
}


/**
* @see MCAUtil#read(File)
* @param file The file to read the data from.
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/net/querz/mca/Section.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package net.querz.mca;

import static net.querz.mca.LoadFlags.*;

import net.querz.nbt.tag.ByteArrayTag;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.ListTag;
import net.querz.nbt.tag.LongArrayTag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -43,13 +41,13 @@ public Section(CompoundTag sectionRoot, int dataVersion, long loadFlags) {
LongArrayTag blockStates = sectionRoot.getLongArrayTag("BlockStates");
ByteArrayTag skyLight = sectionRoot.getByteArrayTag("SkyLight");

if((loadFlags & BLOCK_LIGHTS) != 0) {
if ((loadFlags & BLOCK_LIGHTS) != 0) {
this.blockLight = blockLight != null ? blockLight.getValue() : null;
}
if((loadFlags & BLOCK_STATES) != 0) {
if ((loadFlags & BLOCK_STATES) != 0) {
this.blockStates = blockStates != null ? blockStates.getValue() : null;
}
if((loadFlags & SKY_LIGHT) != 0) {
if ((loadFlags & SKY_LIGHT) != 0) {
this.skyLight = skyLight != null ? skyLight.getValue() : null;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/net/querz/NBTTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ protected void cleanupTmpDir() {
file.delete();
}
}
tmpDir.delete();
}

protected String calculateFileMD5(File file) {
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/net/querz/mca/MCAFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private void assertPartialChunk(Chunk c, long loadFlags) {
assertLoadFLag(c.getBiomes(), loadFlags, BIOMES);
assertLoadFLag(c.getHeightMaps(), loadFlags, HEIGHTMAPS);
assertLoadFLag(c.getEntities(), loadFlags, ENTITIES);
assertLoadFLag(c.getCarvingMasks(), loadFlags, CARVINGMASKS);
assertLoadFLag(c.getCarvingMasks(), loadFlags, CARVING_MASKS);
assertLoadFLag(c.getLights(), loadFlags, LIGHTS);
assertLoadFLag(c.getPostProcessing(), loadFlags, POST_PROCESSING);
assertLoadFLag(c.getLiquidTicks(), loadFlags, LIQUID_TICKS);
Expand All @@ -365,7 +365,7 @@ private void assertPartialChunk(Chunk c, long loadFlags) {
assertLoadFLag(c.getTileEntities(), loadFlags, TILE_ENTITIES);
assertLoadFLag(c.getToBeTicked(), loadFlags, TO_BE_TICKED);
assertLoadFLag(c.getSection(0), loadFlags, BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT);
if((loadFlags & (BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT)) != 0) {
if ((loadFlags & (BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT)) != 0) {
Section s = c.getSection(0);
assertNotNull(String.format("Section is null. Flags=%08x", loadFlags), s);
assertLoadFLag(s.getBlockStates(), loadFlags, BLOCK_STATES);
Expand All @@ -379,7 +379,7 @@ public void testPartialLoad() {
BIOMES,
HEIGHTMAPS,
ENTITIES,
CARVINGMASKS,
CARVING_MASKS,
LIGHTS,
POST_PROCESSING,
LIQUID_TICKS,
Expand Down Expand Up @@ -408,13 +408,12 @@ public void testPartialLoad() {
File tmp = this.getNewTmpFile("r.2.2.mca");
assertThrowsNoException(() -> MCAUtil.write(f, tmp));

for(long flag : flags) {
for (long flag : flags) {
MCAFile mcaFile = assertThrowsNoException(() -> MCAUtil.read(tmp, flag));
c = mcaFile.getChunk(0, 0);
assertPartialChunk(c, flag);
assertThrowsException(() -> MCAUtil.write(mcaFile, getNewTmpFile("r.12.34.mca")), UnsupportedOperationException.class);
}
tmp.delete();
}

public void test1_15GetBiomeAt() throws IOException {
Expand Down

0 comments on commit 49e613b

Please sign in to comment.