Pasting/loading Schematics

Discussion in 'Resources' started by p000ison, Jul 18, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Weasel_Squeezer

    I know I am grave digging a little bit here, but would you by any chance have a method for saving schematics with world a WorldEdit Selection? Like be able to take what the user has selected and create a new schematic file with it then be able to load it with this awesome class.
     
  2. Offline

    Weasel_Squeezer

    This class is great, but not too great. It throws an ArrayIndexOutOfBoundsException in the paste method for the variable: blocks[index] which will sometimes be -122 or -117. I did extensive debugging on this because I have to get a working schematic loading and pasting methods. I printed out lots of the data of the iterations through the 3 for loops to many files and even did some more extensive testing that I will explain below.
    So first off, I decided to just throw in a check before it tries to set the blocks id and data that if the id was < 0, don't do it. I printed to a file every time the id was less than 0. it was usually -122, but occasionally there was a -117. I looked for the locations of those blocks, but that was too hard, So I decided to place almost every minecraft block, make a schematic, and try loading that. the below are my results.

    Here is the original schematic pasted perfectly with worldedit:

    [​IMG]

    Here is the schematic pasted with your class and methods:

    [​IMG]

    As you can see, there is a fair amount of useful blocks missing. The blocks that are missing are always missing (or return a negative number) regardless of the schematic. The way the bytes in the original NBT file is loaded must have a couple hiccups or imperfections. I hope this debugging will help you fix this problem!
    I may be taking on my own (much less efficient) schematic system, because I really need to be able to save and load selections and I am not having a good time trying to get schematics to load right without executing the world edit command with player.performCommand() >.>

    EDIT!!:
    ok so I decided to take one more good look at the worldedit API and actually try to read the code rather than piecing the classes together like a puzzle to try to use the API, and I found that your load schematic method has some identical parts to worldEdits load method, but was missing a good chunk of it, and usefull parts too. So I copied the parts of World Edits load method that were missing, and added them to your load method, and instead of returning an instance of your schematic class, I used the iterations the worldedit Load method had for adding the blocks to the CuboicClipboard to add set the blocks Id and data. AND THAT WORKED! It took me wayyy to long to get this working >.>

    TL;DR
    Fix your loadSchematic method to include all the necessary parts.
     
  3. Offline

    procnole

  4. Offline

    neol3108

    Could you send me that library? :)
    Cause I got the same error?
     
  5. Offline

    libraryaddict

    Sorry. No longer use it
     
  6. Offline

    neol3108

    :\
    Can somebody else help me with that error? :O
     
  7. Offline

    Meatiex

    Sorry to bump, But pasteSchematic and schematic showed up red on the first line of code and I couldn't figure out why...
    Any help? Thanks :)
    Code:java
    1. public static void pasteSchematic(Location loc, Schematic schematic) {
    2. byte[] blocks = schematic.getBlocks();
    3. byte[] blockData = schematic.getData();
    4.  
    5. short length = schematic.getLenght();
    6. short width = schematic.getWidth();
    7. short height = schematic.getHeight();
    8.  
    9. for (int x = 0; x < width; ++x) {
    10. for (int y = 0; y < height; ++y) {
    11. for (int z = 0; z < length; ++z) {
    12. int index = y * width * length + z * width + x;
    13. Block block = new Location(world2, x + loc.getX(), y + loc.getY(), z + loc.getZ()).getBlock();
    14. block.setTypeIdAndData(blocks[index], blockData[index], true);
    15. }
    16. }
    17. }
    18. }
     
  8. Offline

    Monkeyboystein

    FANTASTICO! This is absoloutly fantastic. Ive been looking for a method like this for about a year. On and off of course but thank you!
     
  9. (Sorry if I say crap right now, Im mad of failing since a few hours at this)
    Well I hate it to bump again, but I have the same problem as notrodash.
    I already figured out that the problem appears for blocks with a sub-id or however you want to call it (e. g. 130:5 for a enderchest with its rotation). If Im not wrong, the byteArray calculate the '130 / 5' wich returns -126.
    Anyone got a idea to fix this?
     
  10. Offline

    notrodash

    I managed to solve this by referring to WorldEdit's code. You should be able to extrapolate all the required information from here.
     
  11. Im really sorry, but I can't figure out how to do this :(
    Can you please give me a code example?
     
  12. Offline

    stink123456

    READ:
    i updated the paste method to work with longer block id's:
    Code:java
    1. public static Schematic loadSchematic(File file) throws IOException {
    2. FileInputStream stream = new FileInputStream(file);
    3. NBTInputStream nbtStream = new NBTInputStream(stream);
    4.  
    5. CompoundTag schematicTag = (CompoundTag) nbtStream.readTag();
    6. if (!schematicTag.getName().equals("Schematic")) {
    7. throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
    8. }
    9.  
    10. Map<String, Tag> schematic = schematicTag.getValue();
    11. if (!schematic.containsKey("Blocks")) {
    12. throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
    13. }
    14.  
    15. short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
    16. short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
    17. short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
    18.  
    19.  
    20. // Get blocks
    21. byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
    22. byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
    23. byte[] addId = new byte[0];
    24. short[] blocks = new short[blockId.length]; // Have to later combine IDs
    25.  
    26. // We support 4096 block IDs using the same method as vanilla Minecraft, where
    27. // the highest 4 bits are stored in a separate byte array.
    28. if (schematic.containsKey("AddBlocks")) {
    29. addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
    30. }
    31.  
    32. // Combine the AddBlocks data with the first 8-bit block ID
    33. for (int index = 0; index < blockId.length; index++) {
    34. if ((index >> 1) >= addId.length) { // No corresponding AddBlocks index
    35. blocks[index] = (short) (blockId[index] & 0xFF);
    36. } else {
    37. if ((index & 1) == 0) {
    38. blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
    39. } else {
    40. blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
    41. }
    42. }
    43. }
    44.  
    45. return new Schematic(blocks, blockData, width, length, height);
    46. }
     
  13. What did you change in the Schematic class
     
  14. Offline

    stink123456

    I think nothing needs to be changed there.
     
  15. Offline

    luca4499

    I have a problem... It seems like that the text on signs don't get pasted...
     
  16. Offline

    Hex_27

    @p000ison Mine keeps throwing error after error, and now, I'm not sure how to fix it. It shows an error at loadSchematic. Here's the stack trace:

    [02:55:08 WARN]: java.util.zip.ZipException: Not in GZIP format
    [02:55:08 WARN]: at java.util.zip.GZIPInputStream.readHeader(GZIPInputStr
    eam.java:165)
    [02:55:08 WARN]: at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.
    java:79)
    [02:55:08 WARN]: at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.
    java:91)
    [02:55:08 WARN]: at org.jnbt.NBTInputStream.<init>(NBTInputStream.java:71
    )
    [02:55:08 WARN]: at me.leothepro555.space.Main.loadSchematic(Main.java:16
    8)
    [02:55:08 WARN]: at me.leothepro555.space.Main.onCommand(Main.java:93)
    [02:55:08 WARN]: at org.bukkit.command.PluginCommand.execute(PluginComman
    d.java:44)
    [02:55:08 WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:180)
    [02:55:08 WARN]: at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
    mmand(CraftServer.java:701)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.PlayerConnection.handleC
    ommand(PlayerConnection.java:956)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.PlayerConnection.a(Playe
    rConnection.java:817)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(Packe
    tPlayInChat.java:28)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(
    PacketPlayInChat.java:47)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.NetworkManager.a(Network
    Manager.java:157)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.ServerConnection.c(Sourc
    eFile:134)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.v(Minecr
    aftServer.java:667)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:260)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:558)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:469)
    [02:55:08 WARN]: at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
     
  17. Offline

    Hex_27

    @p000ison Please I can't get anything else to work so far.
     
  18. Offline

    Haggard.nl

    @Hex_27

    Seems like your schematic-file isn't the right format.
    Try to make a new one with WE in it's standard schematic format.
    (the code form this post runs fine with my plugin's)
     
  19. Offline

    Hex_27

    @Haggard.nl I figured out the worldedit solution. It worked fine on the same schematic...
     
  20. Offline

    Tommy Raids

    Last edited: Jan 18, 2015
  21. Offline

    onVoid

    My code says there is something wrong with extending Tag?
     
  22. Offline

    gogobebe2

    The
    Code:
        private short width;
        private short length;
        private short height;
    Are all shorts. What if I want go copy something that's really really big? >:)
     
  23. Offline

    mrCookieSlime

    @gogobebe2
    Well, I highly doubt you would need to save a Schematic larger than 32767 * 32767 * 32767
    At least you would most likely crash your Server with that anyway...
     
    The_BloodHound likes this.
  24. Offline

    gogobebe2

    lol
     
    The_BloodHound likes this.
  25. Offline

    M4UR0

    @p000ison This is my error ... Anyone can help me ?? Is it urgent

    Code:
     [14:55:18] [thread Server / WARN]: java.util.zip.ZipException: Não em formato GZIP
    [14:55:18] [thread Server / WARN]: em java.util.zip.GZIPInputStream.readHeader (origem desconhecida)
    [14:55:18] [thread Server / WARN]:. Em java.util.zip.GZIPInputStream <o init> (origem desconhecida)
    [14:55:18] [thread Server / WARN]:. Em java.util.zip.GZIPInputStream <o init> (origem desconhecida)
    [14:55:18] [thread Server / WARN]:. Em org.jnbt.NBTInputStream <o init> (NBTInputStream.java:71)
    [14:55:18] [thread Server / WARN]: em me.mauro.eventos.Schematic.loadSchematic (Schematic.java:103)
    [14:55:18] [thread Server / WARN]: em me.mauro.SmallHG.Main.onEnable (Main.java:96)
    [14:55:18] [thread Server / WARN]: em org.bukkit.plugin.java.JavaPlugin.setEnabled (JavaPlugin.java:316)
    [14:55:18] [thread Server / WARN]: em org.bukkit.plugin.java.JavaPluginLoader.enablePlugin (JavaPluginLoader.java:332)
    [14:55:18] [thread Server / WARN]: em org.bukkit.plugin.SimplePluginManager.enablePlugin (SimplePluginManager.java:417)
    [14:55:18] [thread Server / WARN]: em org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin (CraftServer.java:476)
    [14:55:18] [thread Server / WARN]: em org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins (CraftServer.java:394)
    [14:55:18] [thread Server / WARN]: em net.minecraft.server.v1_7_R4.MinecraftServer.n (MinecraftServer.java:360)
    [14:55:18] [thread Server / WARN]: em net.minecraft.server.v1_7_R4.MinecraftServer.g (MinecraftServer.java:334)
    [14:55:18] [thread Server / WARN]: em net.minecraft.server.v1_7_R4.MinecraftServer.a (MinecraftServer.java:290)
    [14:55:18] [thread Server / WARN]: em net.minecraft.server.v1_7_R4.DedicatedServer.init (DedicatedServer.java:210)
    [14:55:18] [thread Server / WARN]: em net.minecraft.server.v1_7_R4.MinecraftServer.run (MinecraftServer.java:458)
    [14:55:18] [thread Server / WARN]: em net.minecraft.server.v1_7_R4.ThreadServerApplication.run (SourceFile: 628)
     
  26. @M4UR0
    It would have been nice if you translated the error to english instead of portuguese or whatever language.
    From my experience, I think it says Not in GZIP format. Go to the loadSchematic method and change:
    Code:
    NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(stream));
    to:
    Code:
    NBTInputStream nbtStream = new NBTInputStream(stream);
     
  27. Offline

    M4UR0

    @megamichiel Continued with an error
    @megamichiel please help me

    Code:
    [20:22:27] [Server thread/ERROR]: Error occurred while enabling SmallHG v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_7_R4.util.CraftMagicNumbers.getBlock(CraftMagicNumbers.java:80) ~[craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.util.CraftMagicNumbers.getBlock(CraftMagicNumbers.java:36) ~[craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.block.CraftBlock.getNMSBlock(CraftBlock.java:55) ~[craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.block.CraftBlock.setTypeIdAndData(CraftBlock.java:129) ~[craftbukkit.jar:git-Spigot-1649]
        at me.mauro.eventos.Schematic.pasteSchematic(Schematic.java:94) ~[?:?]
        at me.mauro.SmallHG.Main.onEnable(Main.java:96) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:360) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:334) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:290) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:210) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Spigot-1649]
     
    Last edited: Dec 22, 2015
  28. Offline

    Go Hard

    For everyone having trouble loading the schematic file with this error
    Code:
    java.util.zip.ZipException: Not in GZIP format
    You need to put your schematic file in gzip format. How do you do that? You need do download a zip archive software that supports gzip format. I use 7zip. If you don't have it just install it. After its installed and you have your schematic file ready here is how to put it in a gzip. Locate your schematic file. Right click the file. You should see "7-Zip". Hover over it and find where it says "Add to archive" and click it
    Picture 1 (open)
    [​IMG]


    Now locate where is says "Archive format". Click the drop down box and click "gzip"​
    Picture 2 (open)
    [​IMG]


    You can change the file name to anything you was but make sure the Archive format is on gzip and the file name ends with .gz. For instance my schematic file name is "g_arrow.schematic.gz" I could change it to "g_arrow.gz" to make things easier. That's it! Just leave all the other settings alone. Click OK and it will create your gzip formatted schematic file. Now when you want to load that schematic make sure you end it with .gz like so
    Code:
    loadSchematic(new File("plugins/SCHEMATICPATH/g_arrow.gz"))
    Hope this helped!​
     
  29. Offline

    jusjus112

    Very usefull libary! But i have a problem with this, everything works with pasting and loading. But my schematic.gz files are not pasting everything. It seems it doenst support all blocks and data. If i have 159 (Stained Clay) with some colors in the schematic, it gives me this error:

    Code:
    28.02 11:13:48 [Server] ERROR null
    28.02 11:13:48 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'plak' in plugin SchemPlakFunctie v1.0
    28.02 11:13:48 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_66]
    28.02 11:13:48 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_66]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
    28.02 11:13:48 [Server] INFO Caused by: java.lang.NullPointerException
    28.02 11:13:48 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.util.CraftMagicNumbers.getBlock(CraftMagicNumbers.java:83) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.util.CraftMagicNumbers.getBlock(CraftMagicNumbers.java:39) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.block.CraftBlock.getNMSBlock(CraftBlock.java:47) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.block.CraftBlock.setTypeIdAndData(CraftBlock.java:134) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO at troller.PAsteload.pasteSchematic(PAsteload.java:39) ~[?:?]
    28.02 11:13:48 [Server] INFO at troller.Main.onCommand(Main.java:35) ~[?:?]
    28.02 11:13:48 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit25.jar:git-Bukkit-1092acb]
    28.02 11:13:48 [Server] INFO ... 14 more
    Line 39 from the Paste/Load method are:
    Code:
    block.setTypeIdAndData(blocks[index], blockData[index], true);
    He only paste dirt, stone or blocks with no data. I hope you can help me out ;)
     
  30. Offline

    MrFrozen

    Can I please have all the correct Imports?

    I keep getting errors, and for some Things you can import from JBNT and WE
     
Thread Status:
Not open for further replies.

Share This Page