Solved Saving Schematics to File with WorldEdit API

Discussion in 'Spigot Plugin Development' started by Havelka6, Sep 30, 2017.

  1. Hello, i am doing simple Chunk Snapshot Taker, before they are being edited. And i need to save the whole Chunk into .schematic file to restore it later. I have been looking around the Forum also in WE API, and cannot find a solution, schematic file is created and also i am able to load the .schem, but its empty -> (No block saved , just all air).

    Code so far

    Thanks for help, HamAndRock.
  2. have you looked into json structure storing? that is what I am using to store structures, though if you neeed schematics
    Code (Java):
    File file = new File("mySchem.schematic");
    Vector bot = new Vector(0, 0, 0);
    Vector top = new Vector(50, 255, 50);
    CuboidRegion region = new CuboidRegion(new BukkitWorld(world), bot, top);
    Schematic schem = new Schematic(region);, ClipboardFormat.SCHEMATIC);
    json structure saving: create an object that stores the list of blocks (and their relative locations to the copied location) then use gson or something to save that object.
    #2 Z3tr0nix, Sep 30, 2017
    Last edited: Sep 30, 2017
  3. I am looking how to save schematic not to load one, function for loading is completed. BTW can you link me your json Structure saver ?
  4. The code above is the code needed to save a schematic to a file. Personally, I would highly suggest using the FAWE option as FAWE is a much cleaner and optimized version of WE, which includes an easier to use API that isn't as "hacky" as WE's own API is.
  5. Ok, but the WE code is for loading from file not to write to it.
  6. yes i realised, was in rush, can't seem to find it without fawe, or deprecation.
  7. Ok, tried the FAWE version, but it gives me " Not in GZIP format ",

    Code (Text):
            CuboidRegion cuboidRegion = new CuboidRegion(new BukkitWorld(world), region.getMinimumPoint(), region.getMaximumPoint());
            Schematic schem = new Schematic(cuboidRegion);
  , ClipboardFormat.SCHEMATIC);
  8. + CuboidRegion is Depracated and i need the WE function not the FAWE, but thanks for help...
  9. W/E = lag filled deprecation fest, FAWE (requires W/E to optimize it) = (amazingly fast not hanging easy api)-fest
  10. Then, please i would like a code that will save a selection to file without depracation. But the FAWE version uses the Deprecated CuboidRegion + also throws error when i try it Not in GZIP format .
  11. where does it throw the error, and you have to use FAWE in your dependency (dont use the cuboidregion from worldedit, use the one from fawe, same for clipboardformat)
  12. ok, that will be the problem i guess ..
  13. There is nothing like CuboirdRegion when i include FAWE as maven dependcy / Or .jar directly, its still marks it as deprecated.
  14. How does it then know from what world should copy it from ? :D
  15. idk, its smart af
  16. So i made my personal solution based on WE, i used the Schematic Reader + Schematic Writer and edited it, removed useless data and created "own" schematic type, it have same size and also its not deprecated :3.
  17. I've just finished working on something pretty similar. I've seen lots of questions about schematics and WorldEdit due to all the deprecated methods. Here are the two methods I wrote for creating and saving cuboid schematics using the WorldEdit API

    Code (Text):

    public void loadSchem(String filename, int x, int y, int z, org.bukkit.World world) {
            File dataDirectory = new File (plugin.getDataFolder(), "maps");
            File file = new File(dataDirectory, filename); // The schematic file
            Vector to = new Vector(x, y, z); // Where you want to paste
            World weWorld = new BukkitWorld(world);
            WorldData worldData = weWorld.getWorldData();
            Clipboard clipboard;
            try {
                clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(file)).read(worldData);
                Extent source = clipboard;
                Extent destination = WorldEdit.getInstance().getEditSessionFactory().getEditSession(weWorld, -1);
                ForwardExtentCopy copy = new ForwardExtentCopy(source, clipboard.getRegion(), clipboard.getOrigin(), destination, to);
                copy.setSourceMask(new ExistingBlockMask(clipboard));
            } catch (IOException | WorldEditException e) {
    Code (Text):

    public void saveSchem(String filename, int x1, int y1, int z1, int x2, int y2, int z2, org.bukkit.World world) {
            World weWorld = new BukkitWorld(world);
            WorldData worldData = weWorld.getWorldData();
            Vector pos1 = new Vector(x1, y1, z1); //First corner of your cuboid
            Vector pos2 = new Vector(x2, y2, z2); //Second corner fo your cuboid
            CuboidRegion cReg = new CuboidRegion(weWorld, pos1, pos2);
            File dataDirectory = new File (plugin.getDataFolder(), "maps");
            File file = new File(dataDirectory, filename + ".schematic"); // The schematic file
            try {
                BlockArrayClipboard clipboard = new BlockArrayClipboard(cReg);      
                Extent source = WorldEdit.getInstance().getEditSessionFactory().getEditSession(weWorld, -1);
                Extent destination = clipboard;
                ForwardExtentCopy copy = new ForwardExtentCopy(source, cReg, clipboard.getOrigin(), destination, pos1);
                copy.setSourceMask(new ExistingBlockMask(source));
                ClipboardFormat.SCHEMATIC.getWriter(new FileOutputStream(file)).write(clipboard, worldData);          
            } catch (IOException | MaxChangedBlocksException e1) {
                // TODO Auto-generated catch block
    #18 walrusone, Oct 5, 2017
    Last edited: Oct 5, 2017
  18. I know this thread is a year old, but I'm getting an error of the schematic file not being saved as a schematic file. When I view it on WorldEdit, it's being read as "unknown" and not "schematic" format.

    Getting an error on
    Code (Text):
    clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(file)).read(worldData);
    Code (Text):

    [20:52:00 WARN]: Unexpected end of ZLIB input stream
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at com.sk89q.jnbt.NBTInputStream.readNamedTag(
    [20:52:00 WARN]:     at com.sk89q.jnbt.NBTInputStream.readNamedTag(
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at me.markiscool.dungeoncreator.handlers.SchematicHandler.pasteSchematic(
    [20:52:00 WARN]:     at me.markiscool.dungeoncreator.commands.DebugCommand.onCommand(
    [20:52:00 WARN]:     at org.bukkit.command.PluginCommand.execute(
    [20:52:00 WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(
    [20:52:00 WARN]:     at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PlayerConnection.a(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$
    [20:52:00 WARN]:     at java.util.concurrent.Executors$
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.D(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.DedicatedServer.D(
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.C(
    [20:52:00 WARN]:     at
    [20:52:00 WARN]:     at
    SchematicHandler:java:50 is the line I just pasted.

    Yes, I'm using WorldEdit and running spigot 1.12.2.
    #19 MarkIsCool, Mar 9, 2019
    Last edited: Mar 9, 2019