diff --git a/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java b/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java index d8049ce4..699f87bc 100644 --- a/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java +++ b/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java @@ -77,11 +77,11 @@ public enum DefaultSchematicFormats implements ISchematicFormat { NBTTagCompound nbt = CompressedStreamTools.readCompressed(input); int version = nbt.getInteger("Version"); switch (version) { - case 4: + case 4: //1.12 return new LitematicaSchematic(nbt); - case 5: - case 6: - throw new UnsupportedOperationException("This Schematic Verion is to new."); + case 5: //1.13-1.17 + case 6: //1.18+ + throw new UnsupportedOperationException("This litematic Verion is to new."); default: throw new UnsupportedOperationException("Unsuported Version of a Litematica Schematic"); } diff --git a/src/main/java/baritone/utils/schematic/format/defaults/LitematicaSchematic.java b/src/main/java/baritone/utils/schematic/format/defaults/LitematicaSchematic.java index df8c82ee..3f0e165f 100644 --- a/src/main/java/baritone/utils/schematic/format/defaults/LitematicaSchematic.java +++ b/src/main/java/baritone/utils/schematic/format/defaults/LitematicaSchematic.java @@ -17,24 +17,24 @@ package baritone.utils.schematic.format.defaults; -import baritone.utils.schematic.StaticSchematic; import baritone.utils.accessor.INBTTagLongArray; -import net.minecraft.block.*; +import baritone.utils.schematic.StaticSchematic; +import net.minecraft.block.Block; import net.minecraft.block.properties.IProperty; -import net.minecraft.nbt.*; -import net.minecraft.util.ResourceLocation; import net.minecraft.block.state.IBlockState; - +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.ResourceLocation; import org.apache.commons.lang3.Validate; + import javax.annotation.Nullable; -import java.util.*; +import java.util.Optional; /** * @author Emerson * @since 12/27/2020 * @author rycbar * @since 22.09.2022 - * */ public final class LitematicaSchematic extends StaticSchematic { private final int minX; @@ -42,13 +42,13 @@ public final class LitematicaSchematic extends StaticSchematic { private final int minZ; public LitematicaSchematic(NBTTagCompound nbt) { - int x=0; - int y=0; - int z=0; + int x = 0; + int y = 0; + int z = 0; for (String subReg : getRegions(nbt)) { - x = Math.min(x, getMinimumCord(nbt, subReg,"x")); - y = Math.min(y, getMinimumCord(nbt, subReg,"y")); - z = Math.min(z, getMinimumCord(nbt, subReg,"z")); + x = Math.min(x, getMinimumCoord(nbt, subReg, "x")); + y = Math.min(y, getMinimumCoord(nbt, subReg, "y")); + z = Math.min(z, getMinimumCoord(nbt, subReg, "z")); } this.minX = x; this.minY = y; @@ -81,16 +81,17 @@ public final class LitematicaSchematic extends StaticSchematic { } /** + * Gets both ends from schematic box for a given axis and returns the lower one. * @param s axis that should be read. * @return the lower coord of the requested axis. */ - private static int getMinimumCord(NBTTagCompound nbt, String subReg, String s) { + private static int getMinimumCoord(NBTTagCompound nbt, String subReg, String s) { int a = nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Position").getInteger(s); int b = nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger(s); if (b < 0) { b++; } - return Math.min(a,a+b); + return Math.min(a, a + b); } @@ -101,7 +102,7 @@ public final class LitematicaSchematic extends StaticSchematic { private static IBlockState[] getBlockList(NBTTagList blockStatePalette) { IBlockState[] blockList = new IBlockState[blockStatePalette.tagCount()]; - for (int i = 0; i< blockStatePalette.tagCount(); i++) { + for (int i = 0; i < blockStatePalette.tagCount(); i++) { Block block = Block.REGISTRY.getObject(new ResourceLocation((((NBTTagCompound) blockStatePalette.get(i)).getString("Name")))); NBTTagCompound properties = ((NBTTagCompound) blockStatePalette.get(i)).getCompoundTag("Properties"); @@ -111,7 +112,7 @@ public final class LitematicaSchematic extends StaticSchematic { } /** - * @param block block. + * @param block block. * @param properties List of Properties the block has. * @return A blockState. */ @@ -129,12 +130,7 @@ public final class LitematicaSchematic extends StaticSchematic { } /** - * i haven't written this and i wont try to decode it. - * @param state . - * @param property . - * @param value . - * @return . - * @param . + * @author Emerson */ private static > IBlockState setPropertyValue(IBlockState state, IProperty property, String value) { Optional parsed = property.parseValue(value).toJavaUtil(); @@ -150,10 +146,12 @@ public final class LitematicaSchematic extends StaticSchematic { * @return amount of bits used to encode a block. */ private static int getBitsPerBlock(int amountOfBlockTypes) { - return (int) Math.floor((Math.log(amountOfBlockTypes)) / Math.log(2))+1; + return (int) Math.floor((Math.log(amountOfBlockTypes)) / Math.log(2)) + 1; } /** + * Calculates the volume of the subregion. As size can be a negative value we take the absolute value of the + * multiplication as the volume still holds a positive amount of blocks. * @return the volume of the subregion. */ private static long getVolume(NBTTagCompound nbt, String subReg) { @@ -171,19 +169,36 @@ public final class LitematicaSchematic extends StaticSchematic { } /** - * @param blockList list with the different block types used in the schematic - * @param bitArray bit array that holds the placement pattern + * Subregion don't have to be the same size as the enclosing size of the schematic. If they are smaller we check here if the current block is part of the subregion. + * @param x coord of the block relative to the minimum corner. + * @param y coord of the block relative to the minimum corner. + * @param z coord of the block relative to the minimum corner. + * @return if the current block is part of the subregion. */ + private static boolean inSubregion(NBTTagCompound nbt, String subReg, int x, int y, int z) { + return + x < Math.abs(nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("x")) && + y < Math.abs(nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("y")) && + z < Math.abs(nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("z")); + } + + /** + * @param blockList list with the different block types used in the schematic + * @param bitArray bit array that holds the placement pattern + */ + //x,y,z are the releative positons to the minimum corner of the enclosing box + //minX,minY,minZ are correction terms if the schematic origin isn't the minimum corner + //posX,posY,posZ are the subregions offset relative to the minimum corner private void writeSubregionIntoSchematic(NBTTagCompound nbt, String subReg, IBlockState[] blockList, LitematicaBitArray bitArray) { - int posX = getMinimumCord(nbt, subReg,"x"); - int posY = getMinimumCord(nbt, subReg,"y"); - int posZ = getMinimumCord(nbt, subReg,"z"); + int posX = getMinimumCoord(nbt, subReg, "x"); + int posY = getMinimumCoord(nbt, subReg, "y"); + int posZ = getMinimumCoord(nbt, subReg, "z"); int index = 0; for (int y = 0; y < this.y; y++) { for (int z = 0; z < this.z; z++) { for (int x = 0; x < this.x; x++) { if (inSubregion(nbt, subReg, x, y, z)) { - this.states[x-(minX- posX)][z-(minZ- posZ)][y-(minY- posY)] = blockList[bitArray.getAt(index)]; + this.states[x - (minX - posX)][z - (minZ - posZ)][y - (minY - posY)] = blockList[bitArray.getAt(index)]; index++; } } @@ -191,95 +206,51 @@ public final class LitematicaSchematic extends StaticSchematic { } } - /** - * @param x coord of the schematic. - * @param y coord of the schematic. - * @param z coord of the schematic. - * @return if the current block is inside the subregion. - */ - private static boolean inSubregion(NBTTagCompound nbt, String subReg, int x, int y, int z) { - return - x < Math.abs(nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("x")) && - y < Math.abs(nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("y")) && - z < Math.abs(nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("z")); - } - /** * @author maruohon * Class from the Litematica mod by maruohon - * https://github.com/maruohon/litematica + * ... */ - private static class LitematicaBitArray - { - /** The long array that is used to store the data for this BitArray. */ + private static class LitematicaBitArray { + /** + * The long array that is used to store the data for this BitArray. + */ private final long[] longArray; - /** Number of bits a single entry takes up */ + /** + * Number of bits a single entry takes up + */ private final int bitsPerEntry; /** * The maximum value for a single entry. This also works as a bitmask for a single entry. * For instance, if bitsPerEntry were 5, this value would be 31 (ie, {@code 0b00011111}). */ private final long maxEntryValue; - /** Number of entries in this array (not the length of the long array that internally backs this array) */ + /** + * Number of entries in this array (not the length of the long array that internally backs this array) + */ private final long arraySize; - public LitematicaBitArray(int bitsPerEntryIn, long arraySizeIn, @Nullable long[] longArrayIn) - { - Validate.inclusiveBetween(1L, 32L, (long) bitsPerEntryIn); + public LitematicaBitArray(int bitsPerEntryIn, long arraySizeIn, @Nullable long[] longArrayIn) { + Validate.inclusiveBetween(1L, 32L, bitsPerEntryIn); this.arraySize = arraySizeIn; this.bitsPerEntry = bitsPerEntryIn; this.maxEntryValue = (1L << bitsPerEntryIn) - 1L; - if (longArrayIn != null) - { + if (longArrayIn != null) { this.longArray = longArrayIn; - } - else - { - this.longArray = new long[(int) (roundUp((long) arraySizeIn * (long) bitsPerEntryIn, 64L) / 64L)]; + } else { + this.longArray = new long[(int) (roundUp(arraySizeIn * (long) bitsPerEntryIn, 64L) / 64L)]; } } - public int getAt(long index) - { - Validate.inclusiveBetween(0L, this.arraySize - 1L, (long) index); - 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 - - if (startArrIndex == endArrIndex) - { - return (int) (this.longArray[startArrIndex] >>> startBitOffset & this.maxEntryValue); - } - else - { - int endOffset = 64 - startBitOffset; - return (int) ((this.longArray[startArrIndex] >>> startBitOffset | this.longArray[endArrIndex] << endOffset) & this.maxEntryValue); - } - } - - - public long size() - { - return this.arraySize; - } - - public static long roundUp(long number, long interval) - { + public static long roundUp(long number, long interval) { int sign = 1; - if (interval == 0) - { + if (interval == 0) { return 0; - } - else if (number == 0) - { + } else if (number == 0) { return interval; - } - else - { - if (number < 0) - { + } else { + if (number < 0) { sign = -1; } @@ -287,5 +258,24 @@ public final class LitematicaSchematic extends StaticSchematic { return i == 0 ? number : number + (interval * sign) - i; } } + + public int getAt(long index) { + Validate.inclusiveBetween(0L, this.arraySize - 1L, index); + 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 + + if (startArrIndex == endArrIndex) { + return (int) (this.longArray[startArrIndex] >>> startBitOffset & this.maxEntryValue); + } else { + int endOffset = 64 - startBitOffset; + return (int) ((this.longArray[startArrIndex] >>> startBitOffset | this.longArray[endArrIndex] << endOffset) & this.maxEntryValue); + } + } + + public long size() { + return this.arraySize; + } } } \ No newline at end of file