Skip to content

Commit

Permalink
Fixed logical operation bugs. Added tests. Added check for partial ch…
Browse files Browse the repository at this point in the history
…unk on save. Upgraded gradle
  • Loading branch information
prydin committed Jun 6, 2020
1 parent 2f38501 commit ef9433b
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 55 deletions.
13 changes: 13 additions & 0 deletions NBT.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="NBT" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="net.querz.nbt" external.system.module.version="5.4" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Wed Mar 29 16:28:10 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
57 changes: 34 additions & 23 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,16 +44,16 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
warn () {
echo "$*"
}

die ( ) {
die () {
echo
echo "$*"
echo
Expand Down Expand Up @@ -109,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
Expand Down Expand Up @@ -138,35 +154,30 @@ if $cygwin ; then
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
i=`expr $i + 1`
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

# Escape application args
save ( ) {
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
APP_ARGS=`save "$@"`

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
18 changes: 17 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
42 changes: 27 additions & 15 deletions src/main/java/net/querz/mca/Chunk.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 All @@ -18,6 +19,8 @@ public class Chunk {

public static final int DEFAULT_DATA_VERSION = 1628;

private boolean partial;

private int lastMCAUpdate;

private CompoundTag data;
Expand Down Expand Up @@ -64,48 +67,50 @@ private void initReferences(long loadFlags) {
this.dataVersion = data.getInt("DataVersion");
this.inhabitedTime = level.getLong("InhabitedTime");
this.lastUpdate = level.getLong("LastUpdate");
if((loadFlags | BIOMES) != 0) {
if((loadFlags & BIOMES) != 0) {
this.biomes = level.getIntArray("Biomes");
}
if((loadFlags | HEIGHTMAPS) != 0) {
if((loadFlags & HEIGHTMAPS) != 0) {
this.heightMaps = level.getCompoundTag("Heightmaps");
}
if((loadFlags | CARVING_MARKS) != 0) {
if((loadFlags & CARVINGMASKS) != 0) {
this.carvingMasks = level.getCompoundTag("CarvingMasks");
}
if((loadFlags | ENTITIES) != 0) {
if((loadFlags & ENTITIES) != 0) {
this.entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null;
}
if((loadFlags | TILE_ENTITIES) != 0) {
if((loadFlags & TILE_ENTITIES) != 0) {
this.tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null;
}
if((loadFlags | TILE_TICKS) != 0) {
if((loadFlags & TILE_TICKS) != 0) {
this.tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null;
}
if((loadFlags | LIQUID_TILE_TICKS) != 0) {
if((loadFlags & LIQUID_TICKS) != 0) {
this.liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null;
}
if((loadFlags | LIGHTS) != 0) {
if((loadFlags & LIGHTS) != 0) {
this.lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null;
}
if((loadFlags | LIQUIDS_TO_BE_TICKED) != 0) {
if((loadFlags & LIQUIDS_TO_BE_TICKED) != 0) {
this.liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null;
}
if((loadFlags | TO_BE_TICKED) != 0) {
if((loadFlags & TO_BE_TICKED) != 0) {
this.toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null;
}
if((loadFlags | POST_PROCESSING) != 0) {
if((loadFlags & POST_PROCESSING) != 0) {
this.postProcessing = level.containsKey("PostProcessing") ? level.getListTag("PostProcessing").asListTagList() : null;
}
this.status = level.getString("Status");
this.structures = level.getCompoundTag("Structures");
if (level.containsKey("Sections")) {
if((loadFlags & STRUCTURES) != 0) {
this.structures = level.getCompoundTag("Structures");
}
if ((loadFlags & (BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT)) != 0 && level.containsKey("Sections")) {
for (CompoundTag section : level.getListTag("Sections").asCompoundTagList()) {
int sectionIndex = section.getByte("Y");
if (sectionIndex > 15 || sectionIndex < 0) {
continue;
}
Section newSection = new Section(section, dataVersion);
Section newSection = new Section(section, dataVersion, loadFlags);
if (newSection.isEmpty()) {
continue;
}
Expand All @@ -116,6 +121,9 @@ private void initReferences(long loadFlags) {
// 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;
} else {
partial = false;
}
}

Expand All @@ -125,9 +133,13 @@ private void initReferences(long loadFlags) {
* @param xPos The x-coordinate of the chunk.
* @param zPos The z-coodrinate of the chunk.
* @return The amount of bytes written to the RandomAccessFile.
* @throws UnsupportedOperationException When something went wrong during writing.
* @throws IOException When something went wrong during writing.
*/
public int serialize(RandomAccessFile raf, int xPos, int zPos) throws IOException {
if(this.partial) {
throw new UnsupportedOperationException("Partially loaded chunks cannot be serialized");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
try (BufferedOutputStream nbtOut = new BufferedOutputStream(CompressionType.ZLIB.compress(baos))) {
new NBTSerializer(false).toStream(new NamedTag(null, updateHandle(xPos, zPos)), nbtOut);
Expand Down Expand Up @@ -608,7 +620,7 @@ public CompoundTag updateHandle(int xPos, int zPos) {
} else {
if (biomes != null && biomes.length == 1024) level.putIntArray("Biomes", biomes);
}
if (heightMaps != null) level.put("HeightMaps", heightMaps);
if (heightMaps != null) level.put("Heightmaps", heightMaps);
if (carvingMasks != null) level.put("CarvingMasks", carvingMasks);
if (entities != null) level.put("Entities", entities);
if (tileEntities != null) level.put("TileEntities", tileEntities);
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/net/querz/mca/LoadFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
public class LoadFlags {
public static long BIOMES = 0x0001;
public static long HEIGHTMAPS = 0x0002;
public static long CARVINGMARKS = 0x0004;
public static long CARVINGMASKS = 0x0004;
public static long ENTITIES = 0x0008;
public static long TILE_ENTITIES = 0x0010;
public static long CARVING_MARKS = 0x0020;
public static long TILE_TICKS = 0x0040;
public static long LIQUID_TILE_TICKS = 0x0040;
public static long TO_BE_TICKED = 0x0080;
public static long POST_PROCESSING = 0x0100;
public static long STRUCTURES = 0x0200;
public static long BLOCK_LIGHTS = 0x0400;
public static long BLOCK_STATES = 0x0800;
public static long SKY_LIGHT = 0x1000;
public static long LIGHTS = 0x2000;
public static long LIQUIDS_TO_BE_TICKED = 0x2000;
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;

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/querz/mca/MCAUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ public static MCAFile read(File file) throws IOException {
}
}


/**
* @see MCAUtil#read(File)
* @param file The file to read the data from.
* @return An in-memory representation of the MCA file with decompressed chunk data.
* @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded
* @throws IOException if something during deserialization goes wrong.
* */
public static MCAFile read(String file, long loadFlags) throws IOException {
return read(new File(file), loadFlags);
}

/**
* Reads an MCA file and loads all of its chunks.
* @param file The file to read the data from.
* @return An in-memory representation of the MCA file with decompressed chunk data
* @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded
* @throws IOException if something during deserialization goes wrong.
* */
public static MCAFile read(File file, long loadFlags) throws IOException {
MCAFile mcaFile = newMCAFile(file);
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
mcaFile.deserialize(raf, loadFlags);
return mcaFile;
}
}

/**
* Calls {@link MCAUtil#write(MCAFile, File, boolean)} without changing the timestamps.
* @see MCAUtil#write(MCAFile, File, boolean)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/querz/mca/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,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

0 comments on commit ef9433b

Please sign in to comment.