2019-12-18 16:24:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Baritone.
|
|
|
|
*
|
|
|
|
* Baritone is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Baritone is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package baritone.utils.schematic.parse;
|
|
|
|
|
2019-12-24 23:20:00 +00:00
|
|
|
import baritone.api.schematic.parse.ISchematicParser;
|
2019-12-24 00:05:50 +00:00
|
|
|
import baritone.utils.schematic.StaticSchematic;
|
2019-12-24 23:20:00 +00:00
|
|
|
import baritone.utils.schematic.format.DefaultSchematicFormats;
|
2019-12-19 17:40:38 +00:00
|
|
|
import baritone.utils.type.VarInt;
|
2019-12-18 16:24:43 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.properties.IProperty;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
/**
|
2019-12-24 23:20:00 +00:00
|
|
|
* An implementation of {@link ISchematicParser} for {@link DefaultSchematicFormats#SPONGE}
|
2019-12-18 16:24:43 +00:00
|
|
|
*
|
|
|
|
* @author Brady
|
|
|
|
* @since 12/16/2019
|
|
|
|
*/
|
|
|
|
public enum SpongeParser implements ISchematicParser {
|
|
|
|
INSTANCE;
|
|
|
|
|
|
|
|
@Override
|
2019-12-24 00:05:50 +00:00
|
|
|
public StaticSchematic parse(InputStream input) throws IOException {
|
2019-12-18 16:24:43 +00:00
|
|
|
NBTTagCompound nbt = CompressedStreamTools.readCompressed(input);
|
|
|
|
int version = nbt.getInteger("Version");
|
|
|
|
switch (version) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
return new SpongeSchematic(nbt);
|
|
|
|
default:
|
2019-12-24 00:05:50 +00:00
|
|
|
throw new UnsupportedOperationException("Unsupported Version of a Sponge Schematic");
|
2019-12-18 16:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the Sponge Schematic Format supporting both V1 and V2. (For the current
|
|
|
|
* use case, there is no difference between reading a V1 and V2 schematic).
|
|
|
|
*/
|
2019-12-24 00:05:50 +00:00
|
|
|
private static final class SpongeSchematic extends StaticSchematic {
|
2019-12-18 16:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
SpongeSchematic(NBTTagCompound nbt) {
|
|
|
|
this.x = nbt.getInteger("Width");
|
|
|
|
this.y = nbt.getInteger("Height");
|
|
|
|
this.z = nbt.getInteger("Length");
|
|
|
|
this.states = new IBlockState[this.x][this.z][this.y];
|
|
|
|
|
|
|
|
Int2ObjectArrayMap<IBlockState> palette = new Int2ObjectArrayMap<>();
|
|
|
|
NBTTagCompound paletteTag = nbt.getCompoundTag("Palette");
|
|
|
|
for (String tag : paletteTag.getKeySet()) {
|
|
|
|
int index = paletteTag.getInteger(tag);
|
|
|
|
|
|
|
|
SerializedBlockState serializedState = SerializedBlockState.getFromString(tag);
|
|
|
|
if (serializedState == null) {
|
|
|
|
throw new IllegalArgumentException("Unable to parse palette tag");
|
|
|
|
}
|
|
|
|
|
|
|
|
IBlockState state = serializedState.deserialize();
|
|
|
|
if (state == null) {
|
|
|
|
throw new IllegalArgumentException("Unable to deserialize palette tag");
|
|
|
|
}
|
|
|
|
|
|
|
|
palette.put(index, state);
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:40:38 +00:00
|
|
|
// BlockData is stored as an NBT byte[], however, the actual data that is represented is a varint[]
|
2019-12-18 16:24:43 +00:00
|
|
|
byte[] rawBlockData = nbt.getByteArray("BlockData");
|
|
|
|
int[] blockData = new int[this.x * this.y * this.z];
|
2019-12-19 17:40:38 +00:00
|
|
|
int offset = 0;
|
2019-12-18 16:24:43 +00:00
|
|
|
for (int i = 0; i < blockData.length; i++) {
|
2019-12-20 04:15:14 +00:00
|
|
|
if (offset >= rawBlockData.length) {
|
2019-12-19 17:40:38 +00:00
|
|
|
throw new IllegalArgumentException("No remaining bytes in BlockData for complete schematic");
|
2019-12-18 16:24:43 +00:00
|
|
|
}
|
2019-12-19 17:40:38 +00:00
|
|
|
|
|
|
|
VarInt varInt = VarInt.read(rawBlockData, offset);
|
|
|
|
blockData[i] = varInt.getValue();
|
|
|
|
offset += varInt.getSize();
|
2019-12-18 16:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = 0; y < this.y; y++) {
|
|
|
|
for (int z = 0; z < this.z; z++) {
|
|
|
|
for (int x = 0; x < this.x; x++) {
|
|
|
|
int index = (y * this.z + z) * this.x + x;
|
2019-12-18 16:27:34 +00:00
|
|
|
IBlockState state = palette.get(blockData[index]);
|
|
|
|
if (state == null) {
|
|
|
|
throw new IllegalArgumentException("Invalid Palette Index " + index);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.states[x][z][y] = state;
|
2019-12-18 16:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class SerializedBlockState {
|
|
|
|
|
|
|
|
private static final Pattern REGEX = Pattern.compile("(?<location>(\\w+:)?\\w+)(\\[(?<properties>(\\w+=\\w+,?)+)])?");
|
|
|
|
|
|
|
|
private final ResourceLocation resourceLocation;
|
|
|
|
private final Map<String, String> properties;
|
|
|
|
private IBlockState blockState;
|
|
|
|
|
|
|
|
private SerializedBlockState(ResourceLocation resourceLocation, Map<String, String> properties) {
|
|
|
|
this.resourceLocation = resourceLocation;
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
IBlockState deserialize() {
|
|
|
|
if (this.blockState == null) {
|
2019-12-19 17:40:38 +00:00
|
|
|
Block block = Block.REGISTRY.getObject(this.resourceLocation);
|
|
|
|
this.blockState = block.getDefaultState();
|
2019-12-18 16:24:43 +00:00
|
|
|
|
|
|
|
this.properties.keySet().stream().sorted(String::compareTo).forEachOrdered(key -> {
|
2019-12-19 17:40:38 +00:00
|
|
|
IProperty<?> property = block.getBlockState().getProperty(key);
|
2019-12-20 04:15:14 +00:00
|
|
|
if (property != null) {
|
|
|
|
this.blockState = setPropertyValue(this.blockState, property, this.properties.get(key));
|
2019-12-18 16:24:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.blockState;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SerializedBlockState getFromString(String s) {
|
|
|
|
Matcher m = REGEX.matcher(s);
|
|
|
|
if (!m.matches()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
String location = m.group("location");
|
|
|
|
String properties = m.group("properties");
|
|
|
|
|
|
|
|
ResourceLocation resourceLocation = new ResourceLocation(location);
|
|
|
|
Map<String, String> propertiesMap = new HashMap<>();
|
|
|
|
if (properties != null) {
|
|
|
|
for (String property : properties.split(",")) {
|
|
|
|
String[] split = property.split("=");
|
|
|
|
propertiesMap.put(split[0], split[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SerializedBlockState(resourceLocation, propertiesMap);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 17:40:38 +00:00
|
|
|
|
|
|
|
private static <T extends Comparable<T>> IBlockState setPropertyValue(IBlockState state, IProperty<T> property, String value) {
|
|
|
|
Optional<T> parsed = property.parseValue(value).toJavaUtil();
|
|
|
|
if (parsed.isPresent()) {
|
|
|
|
return state.withProperty(property, parsed.get());
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Invalid value for property " + property);
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 16:24:43 +00:00
|
|
|
}
|
|
|
|
}
|