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 https://pastebin.com/aZh8EixA

    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
    FAWE:
    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);
    schem.save(file, ClipboardFormat.SCHEMATIC);
    src: https://github.com/boy0001/FastAsyncWorldedit/wiki/Pasting-a-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 " java.util.zip.ZipException: Not in GZIP format ",

    Code (Text):
            CuboidRegion cuboidRegion = new CuboidRegion(new BukkitWorld(world), region.getMinimumPoint(), region.getMaximumPoint());
            Schematic schem = new Schematic(cuboidRegion);
            schem.save(file, 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 ava.util.zip.ZipException: 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 6.1.7.3.

    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));
                Operations.completeLegacy(copy);
            } catch (IOException | WorldEditException e) {
                e.printStackTrace();
            }
    }
    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));
                Operations.completeLegacy(copy);
                ClipboardFormat.SCHEMATIC.getWriter(new FileOutputStream(file)).write(clipboard, worldData);          
            } catch (IOException | MaxChangedBlocksException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
     
     
    #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);
    Error:
    Code (Text):

    [20:52:00 WARN]: java.io.EOFException: Unexpected end of ZLIB input stream
    [20:52:00 WARN]:     at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
    [20:52:00 WARN]:     at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    [20:52:00 WARN]:     at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
    [20:52:00 WARN]:     at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:122)
    [20:52:00 WARN]:     at java.io.DataInputStream.readByte(DataInputStream.java:265)
    [20:52:00 WARN]:     at com.sk89q.jnbt.NBTInputStream.readNamedTag(NBTInputStream.java:73)
    [20:52:00 WARN]:     at com.sk89q.jnbt.NBTInputStream.readNamedTag(NBTInputStream.java:62)
    [20:52:00 WARN]:     at com.sk89q.worldedit.extent.clipboard.io.SchematicReader.read(SchematicReader.java:84)
    [20:52:00 WARN]:     at me.markiscool.dungeoncreator.handlers.SchematicHandler.pasteSchematic(SchematicHandler.java:50)
    [20:52:00 WARN]:     at me.markiscool.dungeoncreator.commands.DebugCommand.onCommand(DebugCommand.java:23)
    [20:52:00 WARN]:     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [20:52:00 WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [20:52:00 WARN]:     at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1401)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1236)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13)
    [20:52:00 WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    [20:52:00 WARN]:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    [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(MinecraftServer.java:748)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
    [20:52:00 WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
    [20:52:00 WARN]:     at java.lang.Thread.run(Thread.java:748)
     
    SchematicHandler:java:50 is the line I just pasted.

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