read block palette from schematic file

This commit is contained in:
2023-05-10 00:07:51 +02:00
parent 8a76ec3c03
commit b1bce0c85a
2 changed files with 27 additions and 13 deletions
@@ -1,14 +1,9 @@
package me.youhavetrouble.blockedit; package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.util.Clipboard; import me.youhavetrouble.blockedit.util.Clipboard;
import net.querz.nbt.io.NBTInputStream; import me.youhavetrouble.blockedit.util.Schematic;
import net.querz.nbt.io.NamedTag;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.Tag;
import org.bukkit.block.BlockState;
import java.io.*; import java.io.*;
import java.util.zip.GZIPInputStream;
public class SchematicHandler { public class SchematicHandler {
@@ -41,7 +36,13 @@ public class SchematicHandler {
*/ */
public Clipboard loadSchematic(String schematicName) { public Clipboard loadSchematic(String schematicName) {
File schematicFile = new File(plugin.getDataFolder(), "schematics/" + schematicName + ".schematic"); File schematicFile = new File(plugin.getDataFolder(), "schematics/" + schematicName + ".schematic");
if (!schematicFile.exists()) {
schematicFile = new File(plugin.getDataFolder(), "schematics/" + schematicName + ".schem");
}
if (!schematicFile.exists()) {
return null;
}
Schematic schematic = new Schematic(schematicFile);
return null; return null;
@@ -1,23 +1,24 @@
package me.youhavetrouble.blockedit.util; package me.youhavetrouble.blockedit.util;
import me.youhavetrouble.blockedit.commands.arguments.BlockDataArgument;
import me.youhavetrouble.blockedit.exception.SchematicLoadException; import me.youhavetrouble.blockedit.exception.SchematicLoadException;
import net.querz.nbt.io.NBTInputStream; import net.querz.nbt.io.NBTInputStream;
import net.querz.nbt.io.NamedTag; import net.querz.nbt.io.NamedTag;
import net.querz.nbt.tag.CompoundTag; import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.Tag; import net.querz.nbt.tag.Tag;
import org.bukkit.block.BlockState; import org.bukkit.block.data.BlockData;
import java.io.*; import java.io.*;
import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
public class Schematic { public class Schematic {
private final short width, height, length; private final short width, height, length;
private final int[] palette;
private final int bitsPerBlock; private final int bitsPerBlock;
private final byte[] blocks; private final byte[] blocks;
private BlockState[] blockPalette = new BlockState[0]; private BlockData[] blockPalette = new BlockData[0];
public Schematic(File file) throws SchematicLoadException { public Schematic(File file) throws SchematicLoadException {
try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))))) { try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))))) {
@@ -30,13 +31,21 @@ public class Schematic {
this.height = compoundTag.getShort("Height"); this.height = compoundTag.getShort("Height");
this.length = compoundTag.getShort("Length"); this.length = compoundTag.getShort("Length");
this.palette = compoundTag.getIntArray("Palette"); CompoundTag paletteMap = compoundTag.getCompoundTag("Palette");
this.blocks = compoundTag.getByteArray("BlockData"); this.blocks = compoundTag.getByteArray("BlockData");
this.bitsPerBlock = blocks.length >> 6; this.bitsPerBlock = blocks.length >> 6;
if (this.palette.length > 0) { if (paletteMap.size() > 0) {
this.blockPalette = new BlockState[this.palette.length]; this.blockPalette = new BlockData[paletteMap.size()];
}
for (String key : paletteMap.keySet()) {
int index = paletteMap.getInt(key);
String[] split = key.split(":");
if (split.length == 2) {
key = split[1];
}
this.blockPalette[index] = BlockDataArgument.getBlockData(key);
} }
} catch (IOException e) { } catch (IOException e) {
@@ -55,4 +64,8 @@ public class Schematic {
public short getLength() { public short getLength() {
return length; return length;
} }
public List<BlockData> getBlockPalette() {
return List.of(blockPalette);
}
} }