Remove redundant parser layer
This commit is contained in:
parent
d712839c25
commit
812bc0d8c4
@ -18,9 +18,11 @@
|
||||
package baritone.api.schematic.format;
|
||||
|
||||
import baritone.api.schematic.ISchematic;
|
||||
import baritone.api.schematic.parse.ISchematicParser;
|
||||
import baritone.api.schematic.IStaticSchematic;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* The base of a {@link ISchematic} file format
|
||||
@ -33,7 +35,7 @@ public interface ISchematicFormat {
|
||||
/**
|
||||
* @return The parser for creating schematics of this format
|
||||
*/
|
||||
ISchematicParser getParser();
|
||||
IStaticSchematic parse(InputStream input) throws IOException;
|
||||
|
||||
/**
|
||||
* @param file The file to check against
|
||||
|
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.api.schematic.parse;
|
||||
|
||||
import baritone.api.schematic.IStaticSchematic;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 12/13/2019
|
||||
*/
|
||||
public interface ISchematicParser {
|
||||
|
||||
IStaticSchematic parse(InputStream input) throws IOException;
|
||||
}
|
@ -124,7 +124,7 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil
|
||||
|
||||
ISchematic parsed;
|
||||
try {
|
||||
parsed = format.get().getParser().parse(new FileInputStream(schematic));
|
||||
parsed = format.get().parse(new FileInputStream(schematic));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
|
@ -17,13 +17,17 @@
|
||||
|
||||
package baritone.utils.schematic.format;
|
||||
|
||||
import baritone.api.schematic.IStaticSchematic;
|
||||
import baritone.api.schematic.format.ISchematicFormat;
|
||||
import baritone.api.schematic.parse.ISchematicParser;
|
||||
import baritone.utils.schematic.parse.MCEditParser;
|
||||
import baritone.utils.schematic.parse.SpongeParser;
|
||||
import baritone.utils.schematic.format.defaults.MCEditSchematic;
|
||||
import baritone.utils.schematic.format.defaults.SpongeSchematic;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Default implementations of {@link ISchematicFormat}
|
||||
@ -36,26 +40,39 @@ public enum DefaultSchematicFormats implements ISchematicFormat {
|
||||
/**
|
||||
* The MCEdit schematic specification. Commonly denoted by the ".schematic" file extension.
|
||||
*/
|
||||
MCEDIT("schematic", MCEditParser.INSTANCE),
|
||||
MCEDIT("schematic") {
|
||||
|
||||
@Override
|
||||
public IStaticSchematic parse(InputStream input) throws IOException {
|
||||
return new MCEditSchematic(CompressedStreamTools.readCompressed(input));
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* The SpongePowered Schematic Specification. Commonly denoted by the ".schem" file extension.
|
||||
*
|
||||
* @see <a href="https://github.com/SpongePowered/Schematic-Specification">Sponge Schematic Specification</a>
|
||||
*/
|
||||
SPONGE("schem", SpongeParser.INSTANCE);
|
||||
SPONGE("schem") {
|
||||
|
||||
@Override
|
||||
public IStaticSchematic parse(InputStream input) throws IOException {
|
||||
NBTTagCompound nbt = CompressedStreamTools.readCompressed(input);
|
||||
int version = nbt.getInteger("Version");
|
||||
switch (version) {
|
||||
case 1:
|
||||
case 2:
|
||||
return new SpongeSchematic(nbt);
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unsupported Version of a Sponge Schematic");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final String extension;
|
||||
private final ISchematicParser parser;
|
||||
|
||||
DefaultSchematicFormats(String extension, ISchematicParser parser) {
|
||||
DefaultSchematicFormats(String extension) {
|
||||
this.extension = extension;
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ISchematicParser getParser() {
|
||||
return this.parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.format.defaults;
|
||||
|
||||
import baritone.utils.schematic.StaticSchematic;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 12/27/2019
|
||||
*/
|
||||
public final class MCEditSchematic extends StaticSchematic {
|
||||
|
||||
public MCEditSchematic(NBTTagCompound schematic) {
|
||||
String type = schematic.getString("Materials");
|
||||
if (!type.equals("Alpha")) {
|
||||
throw new IllegalStateException("bad schematic " + type);
|
||||
}
|
||||
this.x = schematic.getInteger("Width");
|
||||
this.y = schematic.getInteger("Height");
|
||||
this.z = schematic.getInteger("Length");
|
||||
byte[] blocks = schematic.getByteArray("Blocks");
|
||||
byte[] metadata = schematic.getByteArray("Data");
|
||||
|
||||
byte[] additional = null;
|
||||
if (schematic.hasKey("AddBlocks")) {
|
||||
byte[] addBlocks = schematic.getByteArray("AddBlocks");
|
||||
additional = new byte[addBlocks.length * 2];
|
||||
for (int i = 0; i < addBlocks.length; i++) {
|
||||
additional[i * 2 + 0] = (byte) ((addBlocks[i] >> 4) & 0xF); // lower nibble
|
||||
additional[i * 2 + 1] = (byte) ((addBlocks[i] >> 0) & 0xF); // upper nibble
|
||||
}
|
||||
}
|
||||
this.states = new IBlockState[this.x][this.z][this.y];
|
||||
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 blockInd = (y * this.z + z) * this.x + x;
|
||||
|
||||
int blockID = blocks[blockInd] & 0xFF;
|
||||
if (additional != null) {
|
||||
// additional is 0 through 15 inclusive since it's & 0xF above
|
||||
blockID |= additional[blockInd] << 8;
|
||||
}
|
||||
Block block = Block.REGISTRY.getObjectById(blockID);
|
||||
int meta = metadata[blockInd] & 0xFF;
|
||||
this.states[x][z][y] = block.getStateFromMeta(meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -15,22 +15,17 @@
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package baritone.utils.schematic.parse;
|
||||
package baritone.utils.schematic.format.defaults;
|
||||
|
||||
import baritone.api.schematic.parse.ISchematicParser;
|
||||
import baritone.utils.schematic.StaticSchematic;
|
||||
import baritone.utils.schematic.format.DefaultSchematicFormats;
|
||||
import baritone.utils.type.VarInt;
|
||||
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;
|
||||
@ -38,83 +33,59 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ISchematicParser} for {@link DefaultSchematicFormats#SPONGE}
|
||||
*
|
||||
* @author Brady
|
||||
* @since 12/16/2019
|
||||
* @since 12/27/2019
|
||||
*/
|
||||
public enum SpongeParser implements ISchematicParser {
|
||||
INSTANCE;
|
||||
public final class SpongeSchematic extends StaticSchematic {
|
||||
|
||||
@Override
|
||||
public StaticSchematic parse(InputStream input) throws IOException {
|
||||
NBTTagCompound nbt = CompressedStreamTools.readCompressed(input);
|
||||
int version = nbt.getInteger("Version");
|
||||
switch (version) {
|
||||
case 1:
|
||||
case 2:
|
||||
return new SpongeSchematic(nbt);
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unsupported Version of a Sponge Schematic");
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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).
|
||||
*/
|
||||
private static final class SpongeSchematic extends StaticSchematic {
|
||||
|
||||
|
||||
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);
|
||||
// BlockData is stored as an NBT byte[], however, the actual data that is represented is a varint[]
|
||||
byte[] rawBlockData = nbt.getByteArray("BlockData");
|
||||
int[] blockData = new int[this.x * this.y * this.z];
|
||||
int offset = 0;
|
||||
for (int i = 0; i < blockData.length; i++) {
|
||||
if (offset >= rawBlockData.length) {
|
||||
throw new IllegalArgumentException("No remaining bytes in BlockData for complete schematic");
|
||||
}
|
||||
|
||||
// BlockData is stored as an NBT byte[], however, the actual data that is represented is a varint[]
|
||||
byte[] rawBlockData = nbt.getByteArray("BlockData");
|
||||
int[] blockData = new int[this.x * this.y * this.z];
|
||||
int offset = 0;
|
||||
for (int i = 0; i < blockData.length; i++) {
|
||||
if (offset >= rawBlockData.length) {
|
||||
throw new IllegalArgumentException("No remaining bytes in BlockData for complete schematic");
|
||||
}
|
||||
VarInt varInt = VarInt.read(rawBlockData, offset);
|
||||
blockData[i] = varInt.getValue();
|
||||
offset += varInt.getSize();
|
||||
}
|
||||
|
||||
VarInt varInt = VarInt.read(rawBlockData, offset);
|
||||
blockData[i] = varInt.getValue();
|
||||
offset += varInt.getSize();
|
||||
}
|
||||
|
||||
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;
|
||||
IBlockState state = palette.get(blockData[index]);
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("Invalid Palette Index " + index);
|
||||
}
|
||||
|
||||
this.states[x][z][y] = state;
|
||||
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;
|
||||
IBlockState state = palette.get(blockData[index]);
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("Invalid Palette Index " + index);
|
||||
}
|
||||
|
||||
this.states[x][z][y] = state;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import baritone.api.schematic.parse.ISchematicParser;
|
||||
import baritone.utils.schematic.StaticSchematic;
|
||||
import baritone.utils.schematic.format.DefaultSchematicFormats;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ISchematicParser} for {@link DefaultSchematicFormats#MCEDIT}
|
||||
*
|
||||
* @author Brady
|
||||
* @since 12/16/2019
|
||||
*/
|
||||
public enum MCEditParser implements ISchematicParser {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public StaticSchematic parse(InputStream input) throws IOException {
|
||||
return new MCEditSchematic(CompressedStreamTools.readCompressed(input));
|
||||
}
|
||||
|
||||
private static final class MCEditSchematic extends StaticSchematic {
|
||||
|
||||
MCEditSchematic(NBTTagCompound schematic) {
|
||||
String type = schematic.getString("Materials");
|
||||
if (!type.equals("Alpha")) {
|
||||
throw new IllegalStateException("bad schematic " + type);
|
||||
}
|
||||
this.x = schematic.getInteger("Width");
|
||||
this.y = schematic.getInteger("Height");
|
||||
this.z = schematic.getInteger("Length");
|
||||
byte[] blocks = schematic.getByteArray("Blocks");
|
||||
byte[] metadata = schematic.getByteArray("Data");
|
||||
|
||||
byte[] additional = null;
|
||||
if (schematic.hasKey("AddBlocks")) {
|
||||
byte[] addBlocks = schematic.getByteArray("AddBlocks");
|
||||
additional = new byte[addBlocks.length * 2];
|
||||
for (int i = 0; i < addBlocks.length; i++) {
|
||||
additional[i * 2 + 0] = (byte) ((addBlocks[i] >> 4) & 0xF); // lower nibble
|
||||
additional[i * 2 + 1] = (byte) ((addBlocks[i] >> 0) & 0xF); // upper nibble
|
||||
}
|
||||
}
|
||||
this.states = new IBlockState[this.x][this.z][this.y];
|
||||
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 blockInd = (y * this.z + z) * this.x + x;
|
||||
|
||||
int blockID = blocks[blockInd] & 0xFF;
|
||||
if (additional != null) {
|
||||
// additional is 0 through 15 inclusive since it's & 0xF above
|
||||
blockID |= additional[blockInd] << 8;
|
||||
}
|
||||
Block block = Block.REGISTRY.getObjectById(blockID);
|
||||
int meta = metadata[blockInd] & 0xFF;
|
||||
this.states[x][z][y] = block.getStateFromMeta(meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user