From 389f85f9d7dd18045e8a2dfc51a403831748d703 Mon Sep 17 00:00:00 2001 From: youhavetrouble Date: Mon, 1 May 2023 20:34:10 +0200 Subject: [PATCH] some random schematic work --- .../blockedit/SchematicHandler.java | 13 +---- .../exception/SchematicLoadException.java | 9 +++ .../blockedit/util/Schematic.java | 58 +++++++++++++++++++ 3 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 src/main/java/me/youhavetrouble/blockedit/exception/SchematicLoadException.java create mode 100644 src/main/java/me/youhavetrouble/blockedit/util/Schematic.java diff --git a/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java b/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java index 6ada5f8..16cfb08 100644 --- a/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java +++ b/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java @@ -5,6 +5,7 @@ import net.querz.nbt.io.NBTInputStream; 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.util.zip.GZIPInputStream; @@ -41,19 +42,7 @@ public class SchematicHandler { public Clipboard loadSchematic(String schematicName) { File schematicFile = new File(plugin.getDataFolder(), "schematics/" + schematicName + ".schematic"); - try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(schematicFile))))) { - NamedTag tag = new NBTInputStream(dis).readTag(Tag.DEFAULT_MAX_DEPTH); - if (tag == null) return null; - if (!(tag.getTag() instanceof CompoundTag compoundTag)) return null; - - plugin.getLogger().info(compoundTag.get("Width").toString()); - plugin.getLogger().info(compoundTag.get("Height").toString()); - plugin.getLogger().info(compoundTag.get("Length").toString()); - - } catch (IOException e) { - return null; - } return null; } diff --git a/src/main/java/me/youhavetrouble/blockedit/exception/SchematicLoadException.java b/src/main/java/me/youhavetrouble/blockedit/exception/SchematicLoadException.java new file mode 100644 index 0000000..a837c2a --- /dev/null +++ b/src/main/java/me/youhavetrouble/blockedit/exception/SchematicLoadException.java @@ -0,0 +1,9 @@ +package me.youhavetrouble.blockedit.exception; + +public class SchematicLoadException extends RuntimeException { + + public SchematicLoadException(String message) { + super(message); + } + +} diff --git a/src/main/java/me/youhavetrouble/blockedit/util/Schematic.java b/src/main/java/me/youhavetrouble/blockedit/util/Schematic.java new file mode 100644 index 0000000..c3a83d4 --- /dev/null +++ b/src/main/java/me/youhavetrouble/blockedit/util/Schematic.java @@ -0,0 +1,58 @@ +package me.youhavetrouble.blockedit.util; + +import me.youhavetrouble.blockedit.exception.SchematicLoadException; +import net.querz.nbt.io.NBTInputStream; +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.util.zip.GZIPInputStream; + +public class Schematic { + + private final short width, height, length; + private final int[] palette; + private final int bitsPerBlock; + private final byte[] blocks; + + private BlockState[] blockPalette = new BlockState[0]; + + public Schematic(File file) throws SchematicLoadException { + try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))))) { + NamedTag tag = new NBTInputStream(dis).readTag(Tag.DEFAULT_MAX_DEPTH); + + if (tag == null) throw new SchematicLoadException("Could not load schematic. Invalid file format"); + if (!(tag.getTag() instanceof CompoundTag compoundTag)) throw new SchematicLoadException("Could not load schematic. Invalid file format"); + + this.width = compoundTag.getShort("Width"); + this.height = compoundTag.getShort("Height"); + this.length = compoundTag.getShort("Length"); + + this.palette = compoundTag.getIntArray("Palette"); + this.blocks = compoundTag.getByteArray("BlockData"); + this.bitsPerBlock = blocks.length >> 6; + + if (this.palette.length > 0) { + this.blockPalette = new BlockState[this.palette.length]; + + } + + } catch (IOException e) { + throw new SchematicLoadException("Could not load schematic due to I/O error"); + } + } + + public short getWidth() { + return width; + } + + public short getHeight() { + return height; + } + + public short getLength() { + return length; + } +}