Merge branch 'master' into LitematicaCommand

This commit is contained in:
rycbar0 2022-09-30 00:42:17 +02:00
commit 3a4168a661
4 changed files with 117 additions and 131 deletions

View File

@ -32,6 +32,4 @@ public abstract class MixinNBTTagLongArray implements INBTTagLongArray {
@Accessor("data") @Accessor("data")
@Override @Override
public abstract long[] getLongArray(); public abstract long[] getLongArray();
} }

View File

@ -23,6 +23,7 @@
"MixinItemStack", "MixinItemStack",
"MixinItemTool", "MixinItemTool",
"MixinMinecraft", "MixinMinecraft",
"MixinNBTTagLongArray",
"MixinNetHandlerPlayClient", "MixinNetHandlerPlayClient",
"MixinNetworkManager", "MixinNetworkManager",
"MixinPlayerControllerMP", "MixinPlayerControllerMP",
@ -31,7 +32,6 @@
"MixinStateImplementation", "MixinStateImplementation",
"MixinTabCompleter", "MixinTabCompleter",
"MixinVboRenderList", "MixinVboRenderList",
"MixinWorldClient", "MixinWorldClient"
"MixinNBTTagLongArray"
] ]
} }

View File

@ -19,9 +19,9 @@ package baritone.utils.schematic.format;
import baritone.api.schematic.IStaticSchematic; import baritone.api.schematic.IStaticSchematic;
import baritone.api.schematic.format.ISchematicFormat; import baritone.api.schematic.format.ISchematicFormat;
import baritone.utils.schematic.format.defaults.LitematicaSchematic;
import baritone.utils.schematic.format.defaults.MCEditSchematic; import baritone.utils.schematic.format.defaults.MCEditSchematic;
import baritone.utils.schematic.format.defaults.SpongeSchematic; import baritone.utils.schematic.format.defaults.SpongeSchematic;
import baritone.utils.schematic.format.defaults.LitematicaSchematic;
import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
@ -71,17 +71,17 @@ public enum DefaultSchematicFormats implements ISchematicFormat {
/** /**
* The Litematica schematic specification. Commonly denoted by the ".litematic" file extension. * The Litematica schematic specification. Commonly denoted by the ".litematic" file extension.
*/ */
Litematica("litematic") { LITEMATICA("litematic") {
@Override @Override
public IStaticSchematic parse(InputStream input) throws IOException { public IStaticSchematic parse(InputStream input) throws IOException {
NBTTagCompound nbt = CompressedStreamTools.readCompressed(input); NBTTagCompound nbt = CompressedStreamTools.readCompressed(input);
int version = nbt.getInteger("Version"); int version = nbt.getInteger("Version");
switch (version) { switch (version) {
case 4: case 4: //1.12
return new LitematicaSchematic(nbt); return new LitematicaSchematic(nbt);
case 5: case 5: //1.13-1.17
case 6: case 6: //1.18+
throw new UnsupportedOperationException("This Schematic Verion is to new."); throw new UnsupportedOperationException("This litematic Verion is to new.");
default: default:
throw new UnsupportedOperationException("Unsuported Version of a Litematica Schematic"); throw new UnsupportedOperationException("Unsuported Version of a Litematica Schematic");
} }

View File

@ -17,47 +17,50 @@
package baritone.utils.schematic.format.defaults; package baritone.utils.schematic.format.defaults;
import baritone.utils.schematic.StaticSchematic;
import baritone.utils.accessor.INBTTagLongArray; 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.block.properties.IProperty;
import net.minecraft.nbt.*;
import net.minecraft.util.ResourceLocation;
import net.minecraft.block.state.IBlockState; 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 org.apache.commons.lang3.Validate;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.Optional;
/** /**
* @author Emerson * @author Emerson
* @since 12/27/2020 * @since 12/27/2020
* @author rycbar * @author rycbar
* @since 22.09.2022 * @since 22.09.2022
*
*/ */
public final class LitematicaSchematic extends StaticSchematic { public final class LitematicaSchematic extends StaticSchematic {
int minX=0; private final int minX;
int minY=0; private final int minY;
int minZ=0; private final int minZ;
private static final String reg = "Regions";
private static final String meta = "Metadata";
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";
public LitematicaSchematic(NBTTagCompound nbt) { public LitematicaSchematic(NBTTagCompound nbt) {
getMinimumCorner(nbt); int x = 0;
int y = 0;
int z = 0;
for (String subReg : getRegions(nbt)) {
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;
this.minZ = z;
this.x = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("x")); this.x = Math.abs(nbt.getCompoundTag("Metadata").getCompoundTag("EnclosingSize").getInteger("x"));
this.y = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("y")); this.y = Math.abs(nbt.getCompoundTag("Metadata").getCompoundTag("EnclosingSize").getInteger("y"));
this.z = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("z")); this.z = Math.abs(nbt.getCompoundTag("Metadata").getCompoundTag("EnclosingSize").getInteger("z"));
this.states = new IBlockState[this.x][this.z][this.y]; this.states = new IBlockState[this.x][this.z][this.y];
for (String subReg : getRegions(nbt)) { for (String subReg : getRegions(nbt)) {
NBTTagList usedBlockTypes = nbt.getCompoundTag(reg).getCompoundTag(subReg).getTagList(blStPl, 10); NBTTagList usedBlockTypes = nbt.getCompoundTag("Regions").getCompoundTag(subReg).getTagList("BlockStatePalette", 10);
IBlockState[] blockList = getBlockList(usedBlockTypes); IBlockState[] blockList = getBlockList(usedBlockTypes);
int bitsPerBlock = getBitsPerBlock(usedBlockTypes.tagCount()); int bitsPerBlock = getBitsPerBlock(usedBlockTypes.tagCount());
@ -74,32 +77,22 @@ public final class LitematicaSchematic extends StaticSchematic {
* @return Array of subregion names. * @return Array of subregion names.
*/ */
private static String[] getRegions(NBTTagCompound nbt) { private static String[] getRegions(NBTTagCompound nbt) {
return nbt.getCompoundTag(reg).getKeySet().toArray(new String[0]); return nbt.getCompoundTag("Regions").getKeySet().toArray(new String[0]);
}
/**
* Calculates the minimum cords/origin of the schematic as litematica schematics
* can have a non-minimum origin.
*/
private void getMinimumCorner(NBTTagCompound nbt) {
for (String subReg : getRegions(nbt)) {
this.minX = Math.min(this.minX, getMinimumCord(nbt, subReg,"x"));
this.minY = Math.min(this.minY, getMinimumCord(nbt, subReg,"y"));
this.minZ = Math.min(this.minZ, getMinimumCord(nbt, subReg,"z"));
}
} }
/** /**
* Gets both ends from schematic box for a given axis and returns the lower one.
*
* @param s axis that should be read. * @param s axis that should be read.
* @return the lower cord of the requested axis. * @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(reg).getCompoundTag(subReg).getCompoundTag(pos).getInteger(s); int a = nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Position").getInteger(s);
int b = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger(s); int b = nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger(s);
if (b < 0) { if (b < 0) {
b++; b++;
} }
return Math.min(a,a+b); return Math.min(a, a + b);
} }
@ -110,7 +103,7 @@ public final class LitematicaSchematic extends StaticSchematic {
private static IBlockState[] getBlockList(NBTTagList blockStatePalette) { private static IBlockState[] getBlockList(NBTTagList blockStatePalette) {
IBlockState[] blockList = new IBlockState[blockStatePalette.tagCount()]; 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")))); Block block = Block.REGISTRY.getObject(new ResourceLocation((((NBTTagCompound) blockStatePalette.get(i)).getString("Name"))));
NBTTagCompound properties = ((NBTTagCompound) blockStatePalette.get(i)).getCompoundTag("Properties"); NBTTagCompound properties = ((NBTTagCompound) blockStatePalette.get(i)).getCompoundTag("Properties");
@ -120,7 +113,7 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
/** /**
* @param block block. * @param block block.
* @param properties List of Properties the block has. * @param properties List of Properties the block has.
* @return A blockState. * @return A blockState.
*/ */
@ -138,12 +131,7 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
/** /**
* i haven't written this and i wont try to decode it. * @author Emerson
* @param state .
* @param property .
* @param value .
* @return .
* @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) {
Optional<T> parsed = property.parseValue(value).toJavaUtil(); Optional<T> parsed = property.parseValue(value).toJavaUtil();
@ -159,40 +147,61 @@ public final class LitematicaSchematic extends StaticSchematic {
* @return amount of bits used to encode a block. * @return amount of bits used to encode a block.
*/ */
private static int getBitsPerBlock(int amountOfBlockTypes) { 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. * @return the volume of the subregion.
*/ */
private static long getVolume(NBTTagCompound nbt, String subReg) { private static long getVolume(NBTTagCompound nbt, String subReg) {
return Math.abs( return Math.abs(
nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("x") * nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("x") *
nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("y") * nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("y") *
nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("z")); nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("z"));
} }
/** /**
* @return array of Long values. * @return array of Long values.
*/ */
private static long[] getBlockStates(NBTTagCompound nbt, String subReg) { private static long[] getBlockStates(NBTTagCompound nbt, String subReg) {
return ((INBTTagLongArray) nbt.getCompoundTag(reg).getCompoundTag(subReg).getTag(blSt)).getLongArray(); return ((INBTTagLongArray) nbt.getCompoundTag("Regions").getCompoundTag(subReg).getTag("BlockStates")).getLongArray();
}
/**
* 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 blockList list with the different block types used in the schematic
* @param bitArray bit array that holds the placement pattern * @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) { private void writeSubregionIntoSchematic(NBTTagCompound nbt, String subReg, IBlockState[] blockList, LitematicaBitArray bitArray) {
int posX = getMinimumCord(nbt, subReg,"x"); int posX = getMinimumCoord(nbt, subReg, "x");
int posY = getMinimumCord(nbt, subReg,"y"); int posY = getMinimumCoord(nbt, subReg, "y");
int posZ = getMinimumCord(nbt, subReg,"z"); int posZ = getMinimumCoord(nbt, subReg, "z");
int index = 0; int index = 0;
for (int y = 0; y < this.y; y++) { for (int y = 0; y < this.y; y++) {
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(nbt, subReg, x, y, z)) { 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++; index++;
} }
} }
@ -201,90 +210,50 @@ public final class LitematicaSchematic extends StaticSchematic {
} }
/** /**
* @param x cord of the schematic. * @author maruohon
* @param y cord of the schematic. * Class from the Litematica mod by maruohon
* @param z cord of the schematic. * <a href="https://github.com/maruohon/litematica">...</a>
* @return if the current block is inside the subregion.
*/ */
private static boolean inSubregion(NBTTagCompound nbt, String subReg, int x, int y, int z) { private static class LitematicaBitArray {
return /**
x < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("x")) && * The long array that is used to store the data for this BitArray.
y < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("y")) && */
z < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("z"));
}
/** LitematicaBitArray class from litematica */
private static class LitematicaBitArray
{
/** The long array that is used to store the data for this BitArray. */
private final long[] longArray; private final long[] longArray;
/** Number of bits a single entry takes up */ /**
* Number of bits a single entry takes up
*/
private final int bitsPerEntry; private final int bitsPerEntry;
/** /**
* The maximum value for a single entry. This also works as a bitmask for a single entry. * 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}). * For instance, if bitsPerEntry were 5, this value would be 31 (ie, {@code 0b00011111}).
*/ */
private final long maxEntryValue; private final long maxEntryValue;
/** Number of entries in this array (<b>not</b> the length of the long array that internally backs this array) */ /**
* Number of entries in this array (<b>not</b> the length of the long array that internally backs this array)
*/
private final long arraySize; private final long arraySize;
public LitematicaBitArray(int bitsPerEntryIn, long arraySizeIn, @Nullable long[] longArrayIn) public LitematicaBitArray(int bitsPerEntryIn, long arraySizeIn, @Nullable long[] longArrayIn) {
{ Validate.inclusiveBetween(1L, 32L, bitsPerEntryIn);
Validate.inclusiveBetween(1L, 32L, (long) bitsPerEntryIn);
this.arraySize = arraySizeIn; this.arraySize = arraySizeIn;
this.bitsPerEntry = bitsPerEntryIn; this.bitsPerEntry = bitsPerEntryIn;
this.maxEntryValue = (1L << bitsPerEntryIn) - 1L; this.maxEntryValue = (1L << bitsPerEntryIn) - 1L;
if (longArrayIn != null) if (longArrayIn != null) {
{
this.longArray = longArrayIn; this.longArray = longArrayIn;
} } else {
else this.longArray = new long[(int) (roundUp(arraySizeIn * (long) bitsPerEntryIn, 64L) / 64L)];
{
this.longArray = new long[(int) (roundUp((long) arraySizeIn * (long) bitsPerEntryIn, 64L) / 64L)];
} }
} }
public int getAt(long index) public static long roundUp(long number, long interval) {
{
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)
{
int sign = 1; int sign = 1;
if (interval == 0) if (interval == 0) {
{
return 0; return 0;
} } else if (number == 0) {
else if (number == 0)
{
return interval; return interval;
} } else {
else if (number < 0) {
{
if (number < 0)
{
sign = -1; sign = -1;
} }
@ -292,5 +261,24 @@ public final class LitematicaSchematic extends StaticSchematic {
return i == 0 ? number : number + (interval * sign) - i; 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;
}
} }
} }