Code clean up

This commit is contained in:
rycbar0 2022-09-26 01:18:56 +02:00
parent 3cd8ce8323
commit 75a3fc699e

View File

@ -37,27 +37,26 @@ import java.util.*;
*/ */
public final class LitematicaSchematic extends StaticSchematic { public final class LitematicaSchematic extends StaticSchematic {
int minX=0,minY=0,minZ=0; int minX=0,minY=0,minZ=0;
private static String reg = "Regions"; private static final String reg = "Regions";
private static String meta = "Metadata"; private static final String meta = "Metadata";
private static String schemSize = "EnclosingSize"; private static final String schemSize = "EnclosingSize";
private static final String blSt = "BlockStates";
private static final String blStPl = "BlockStatePalette";
private static final String pos = "Position";
private static final String size = "Size";
private static String[] regNames; private static String[] regNames;
private static String blSt = "BlockStates";
private static String blStPl = "BlockStatePalette";
private static String pos = "Position";
private static String size = "Size";
private static NBTTagCompound nbt; private static NBTTagCompound nbt;
public LitematicaSchematic(NBTTagCompound nbt) { public LitematicaSchematic(NBTTagCompound nbt) {
this.nbt = nbt;
regNames = getRegions(); regNames = getRegions();
LitematicaSchematic.nbt = nbt;
minCord();
this.x = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("x")); this.x = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("x"));
this.y = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("y")); this.y = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("y"));
this.z = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("z")); this.z = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("z"));
this.states = new IBlockState[this.x][this.z][this.y]; this.states = new IBlockState[this.x][this.z][this.y];
minCord();
for (String subReg : regNames) { for (String subReg : regNames) {
NBTTagList blockStatePalette = nbt.getCompoundTag(reg).getCompoundTag(subReg).getTagList(blStPl, 10); NBTTagList blockStatePalette = nbt.getCompoundTag(reg).getCompoundTag(subReg).getTagList(blStPl, 10);
// ListTag blockStatePalette = nbt.getCompound(reg).getCompound(subReg).getList(blStPl,10); // ListTag blockStatePalette = nbt.getCompound(reg).getCompound(subReg).getList(blStPl,10);
@ -82,8 +81,7 @@ public final class LitematicaSchematic extends StaticSchematic {
for (int z = 0; z < this.z; z++) { for (int z = 0; z < this.z; z++) {
for (int x = 0; x < this.x; x++) { for (int x = 0; x < this.x; x++) {
if (inSubregion(x, y, z, subReg)) { if (inSubregion(x, y, z, subReg)) {
int paletteIndex = bitArray.getAt(index); this.states[x-(minX-posX)][z-(minZ-posZ)][y-(minY-posY)] = paletteBlockStates[bitArray.getAt(index)];
this.states[x-(minX-posX)][z-(minZ-posZ)][y-(minY-posY)] = paletteBlockStates[paletteIndex];
index++; index++;
} }
} }
@ -92,14 +90,25 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
} }
/**
* @param x cord of the schematic.
* @param y cord of the schematic.
* @param z cord of the schematic.
* @param subReg name of the subregion.
* @return if the current block is inside the subregion.
*/
private static boolean inSubregion(int x, int y, int z, String subReg) { private static boolean inSubregion(int x, int y, int z, String subReg) {
boolean inspect = return
x < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("x")) && x < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("x")) &&
y < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("y")) && y < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("y")) &&
z < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("z")); z < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("z"));
return inspect;
} }
/**
* @param subReg name of the subregion.
* @param s axis that should be read.
* @return the lower cord of the requested axis.
*/
private static int getMin(String subReg,String s) { private static int getMin(String subReg,String s) {
int a = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(pos).getInteger(s); int a = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(pos).getInteger(s);
int b = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger(s); int b = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger(s);
@ -110,6 +119,10 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
/**
* Calculates the minimum cords/origin of the schematic as litematica schematics
* can have a non minimum origin.
*/
private void minCord() { private void minCord() {
for (String subReg : regNames) { for (String subReg : regNames) {
this.minX = Math.min(this.minX,getMin(subReg,"x")); this.minX = Math.min(this.minX,getMin(subReg,"x"));
@ -118,6 +131,9 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
} }
/**
* @return Array of subregion names.
*/
private static String[] getRegions() { private static String[] getRegions() {
return nbt.getCompoundTag(reg).getKeySet().toArray(new String[0]); return nbt.getCompoundTag(reg).getKeySet().toArray(new String[0]);
} }
@ -165,11 +181,11 @@ public final class LitematicaSchematic extends StaticSchematic {
/** /**
* i haven't written this and i wont try to decode it. * i haven't written this and i wont try to decode it.
* @param state * @param state .
* @param property * @param property .
* @param value * @param value .
* @return * @return .
* @param <T> * @param <T> .
*/ */
private static <T extends Comparable<T>> IBlockState setPropertyValue(IBlockState state, IProperty<T> property, String value) { private static <T extends Comparable<T>> IBlockState setPropertyValue(IBlockState state, IProperty<T> property, String value) {
//private static <T extends Comparable<T>> BlockState setPropertyValue(BlockState state, Property<T> property, String value) { //private static <T extends Comparable<T>> BlockState setPropertyValue(BlockState state, Property<T> property, String value) {
@ -209,7 +225,7 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
/** /**
* @return the amount of blocks in the schematic, including air blocks. * @return the volume of the subregion.
*/ */
private static long getVolume(String subReg) { private static long getVolume(String subReg) {
return Math.abs( return Math.abs(
@ -275,24 +291,6 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
} }
public void setAt(long index, int value)
{
Validate.inclusiveBetween(0L, this.arraySize - 1L, (long) index);
Validate.inclusiveBetween(0L, this.maxEntryValue, (long) value);
long startOffset = index * (long) this.bitsPerEntry;
int startArrIndex = (int) (startOffset >> 6); // startOffset / 64
int endArrIndex = (int) (((index + 1L) * (long) this.bitsPerEntry - 1L) >> 6);
int startBitOffset = (int) (startOffset & 0x3F); // startOffset % 64
this.longArray[startArrIndex] = this.longArray[startArrIndex] & ~(this.maxEntryValue << startBitOffset) | ((long) value & this.maxEntryValue) << startBitOffset;
if (startArrIndex != endArrIndex)
{
int endOffset = 64 - startBitOffset;
int j1 = this.bitsPerEntry - endOffset;
this.longArray[endArrIndex] = this.longArray[endArrIndex] >>> j1 << j1 | ((long) value & this.maxEntryValue) >> endOffset;
}
}
public int getAt(long index) public int getAt(long index)
{ {
Validate.inclusiveBetween(0L, this.arraySize - 1L, (long) index); Validate.inclusiveBetween(0L, this.arraySize - 1L, (long) index);