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")
@Override
public abstract long[] getLongArray();
}

View File

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

View File

@ -19,9 +19,9 @@ package baritone.utils.schematic.format;
import baritone.api.schematic.IStaticSchematic;
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.SpongeSchematic;
import baritone.utils.schematic.format.defaults.LitematicaSchematic;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
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.
*/
Litematica("litematic") {
LITEMATICA("litematic") {
@Override
public IStaticSchematic parse(InputStream input) throws IOException {
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");
}

View File

@ -17,47 +17,50 @@
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 {
int minX=0;
int minY=0;
int minZ=0;
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";
private final int minX;
private final int minY;
private final int minZ;
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.y = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("y"));
this.z = Math.abs(nbt.getCompoundTag(meta).getCompoundTag(schemSize).getInteger("z"));
this.x = Math.abs(nbt.getCompoundTag("Metadata").getCompoundTag("EnclosingSize").getInteger("x"));
this.y = Math.abs(nbt.getCompoundTag("Metadata").getCompoundTag("EnclosingSize").getInteger("y"));
this.z = Math.abs(nbt.getCompoundTag("Metadata").getCompoundTag("EnclosingSize").getInteger("z"));
this.states = new IBlockState[this.x][this.z][this.y];
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);
int bitsPerBlock = getBitsPerBlock(usedBlockTypes.tagCount());
@ -74,32 +77,22 @@ public final class LitematicaSchematic extends StaticSchematic {
* @return Array of subregion names.
*/
private static String[] getRegions(NBTTagCompound nbt) {
return nbt.getCompoundTag(reg).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"));
}
return nbt.getCompoundTag("Regions").getKeySet().toArray(new String[0]);
}
/**
* 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 cord of the requested axis.
* @return the lower coord of the requested axis.
*/
private static int getMinimumCord(NBTTagCompound nbt, String subReg, String s) {
int a = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(pos).getInteger(s);
int b = nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger(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);
}
@ -110,7 +103,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");
@ -120,7 +113,7 @@ public final class LitematicaSchematic extends StaticSchematic {
}
/**
* @param block block.
* @param block block.
* @param properties List of Properties the block has.
* @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.
* @param state .
* @param property .
* @param value .
* @return .
* @param <T> .
* @author Emerson
*/
private static <T extends Comparable<T>> IBlockState setPropertyValue(IBlockState state, IProperty<T> property, String value) {
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.
*/
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) {
return Math.abs(
nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("x") *
nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("y") *
nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("z"));
nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("x") *
nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("y") *
nbt.getCompoundTag("Regions").getCompoundTag(subReg).getCompoundTag("Size").getInteger("z"));
}
/**
* @return array of Long values.
*/
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 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) {
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++;
}
}
@ -201,90 +210,50 @@ public final class LitematicaSchematic extends StaticSchematic {
}
/**
* @param x cord of the schematic.
* @param y cord of the schematic.
* @param z cord of the schematic.
* @return if the current block is inside the subregion.
* @author maruohon
* Class from the Litematica mod by maruohon
* <a href="https://github.com/maruohon/litematica">...</a>
*/
private static boolean inSubregion(NBTTagCompound nbt, String subReg, int x, int y, int z) {
return
x < Math.abs(nbt.getCompoundTag(reg).getCompoundTag(subReg).getCompoundTag(size).getInteger("x")) &&
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 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 (<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;
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;
}
@ -292,5 +261,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;
}
}
}