From 812bc0d8c40ebe56cd637280e0d412190d1f4f7d Mon Sep 17 00:00:00 2001 From: Brady Date: Fri, 27 Dec 2019 01:14:31 -0600 Subject: [PATCH] Remove redundant parser layer --- .../schematic/format/ISchematicFormat.java | 6 +- .../api/schematic/parse/ISchematicParser.java | 32 ----- .../java/baritone/process/BuilderProcess.java | 2 +- .../format/DefaultSchematicFormats.java | 43 +++++-- .../format/defaults/MCEditSchematic.java | 69 ++++++++++ .../defaults/SpongeSchematic.java} | 119 +++++++----------- .../utils/schematic/parse/MCEditParser.java | 86 ------------- 7 files changed, 149 insertions(+), 208 deletions(-) delete mode 100644 src/api/java/baritone/api/schematic/parse/ISchematicParser.java create mode 100644 src/main/java/baritone/utils/schematic/format/defaults/MCEditSchematic.java rename src/main/java/baritone/utils/schematic/{parse/SpongeParser.java => format/defaults/SpongeSchematic.java} (53%) delete mode 100644 src/main/java/baritone/utils/schematic/parse/MCEditParser.java diff --git a/src/api/java/baritone/api/schematic/format/ISchematicFormat.java b/src/api/java/baritone/api/schematic/format/ISchematicFormat.java index 260ab4536..3fe045bcf 100644 --- a/src/api/java/baritone/api/schematic/format/ISchematicFormat.java +++ b/src/api/java/baritone/api/schematic/format/ISchematicFormat.java @@ -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 diff --git a/src/api/java/baritone/api/schematic/parse/ISchematicParser.java b/src/api/java/baritone/api/schematic/parse/ISchematicParser.java deleted file mode 100644 index ab0f12f49..000000000 --- a/src/api/java/baritone/api/schematic/parse/ISchematicParser.java +++ /dev/null @@ -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 . - */ - -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; -} diff --git a/src/main/java/baritone/process/BuilderProcess.java b/src/main/java/baritone/process/BuilderProcess.java index 5cbc3da70..4efa2c954 100644 --- a/src/main/java/baritone/process/BuilderProcess.java +++ b/src/main/java/baritone/process/BuilderProcess.java @@ -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; diff --git a/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java b/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java index 494d6ec8d..942bd72a3 100644 --- a/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java +++ b/src/main/java/baritone/utils/schematic/format/DefaultSchematicFormats.java @@ -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 Sponge Schematic Specification */ - 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 diff --git a/src/main/java/baritone/utils/schematic/format/defaults/MCEditSchematic.java b/src/main/java/baritone/utils/schematic/format/defaults/MCEditSchematic.java new file mode 100644 index 000000000..08e571c96 --- /dev/null +++ b/src/main/java/baritone/utils/schematic/format/defaults/MCEditSchematic.java @@ -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 . + */ + +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); + } + } + } + } +} diff --git a/src/main/java/baritone/utils/schematic/parse/SpongeParser.java b/src/main/java/baritone/utils/schematic/format/defaults/SpongeSchematic.java similarity index 53% rename from src/main/java/baritone/utils/schematic/parse/SpongeParser.java rename to src/main/java/baritone/utils/schematic/format/defaults/SpongeSchematic.java index 255916d18..0d03e5d87 100644 --- a/src/main/java/baritone/utils/schematic/parse/SpongeParser.java +++ b/src/main/java/baritone/utils/schematic/format/defaults/SpongeSchematic.java @@ -15,22 +15,17 @@ * along with Baritone. If not, see . */ -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 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 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; } } } diff --git a/src/main/java/baritone/utils/schematic/parse/MCEditParser.java b/src/main/java/baritone/utils/schematic/parse/MCEditParser.java deleted file mode 100644 index 3599b824b..000000000 --- a/src/main/java/baritone/utils/schematic/parse/MCEditParser.java +++ /dev/null @@ -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 . - */ - -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); - } - } - } - } - } -}