Pasting Schematics?

Discussion in 'Plugin Development' started by TrollTaylor, Mar 11, 2014.

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

    TrollTaylor

    How do i paste schematics using world edit API?
     
  2. Offline

    MrGermanrain

  3. Offline

    Wizehh

  4. Offline

    TrollTaylor

    MrGermanrain Yeah i tryed that gave me a error and only spawned one of the blocks..
     
  5. Offline

    MrGermanrain

    Could you show your code that you used and your error ?
     
  6. Offline

    TrollTaylor

    MrGermanrain
    Schematic Class
    Code:java
    1. package me.trolltaylor.plugin.util;
    2.  
    3. public class Schematic
    4. {
    5.  
    6. private byte[] blocks;
    7. private byte[] data;
    8. private short width;
    9. private short lenght;
    10. private short height;
    11.  
    12. public Schematic(byte[] blocks, byte[] data, short width, short lenght, short height)
    13. {
    14. this.blocks = blocks;
    15. this.data = data;
    16. this.width = width;
    17. this.lenght = lenght;
    18. this.height = height;
    19. }
    20.  
    21. /**
    22. * @return the blocks
    23. */
    24. public byte[] getBlocks()
    25. {
    26. return blocks;
    27. }
    28.  
    29. /**
    30. * @return the data
    31. */
    32. public byte[] getData()
    33. {
    34. return data;
    35. }
    36.  
    37. /**
    38. * @return the width
    39. */
    40. public short getWidth()
    41. {
    42. return width;
    43. }
    44.  
    45. /**
    46. * @return the lenght
    47. */
    48. public short getLenght()
    49. {
    50. return lenght;
    51. }
    52.  
    53. /**
    54. * @return the height
    55. */
    56. public short getHeight()
    57. {
    58. return height;
    59. }
    60. }

    Pasting / Loading Methods Class
    Code:java
    1. package me.trolltaylor.plugin.util;
    2.  
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.IOException;
    6. import java.util.Map;
    7. import java.util.zip.GZIPInputStream;
    8.  
    9. import org.bukkit.Location;
    10. import org.bukkit.World;
    11. import org.bukkit.block.Block;
    12.  
    13. import com.sk89q.jnbt.ByteArrayTag;
    14. import com.sk89q.jnbt.CompoundTag;
    15. import com.sk89q.jnbt.NBTInputStream;
    16. import com.sk89q.jnbt.ShortTag;
    17. import com.sk89q.jnbt.StringTag;
    18. import com.sk89q.jnbt.Tag;
    19.  
    20. public class SchematicBuilder {
    21. public static void pasteSchematic(World world, Location loc, Schematic schematic)
    22. {
    23. byte[] blocks = schematic.getBlocks();
    24. byte[] blockData = schematic.getData();
    25.  
    26. short length = schematic.getLenght();
    27. short width = schematic.getWidth();
    28. short height = schematic.getHeight();
    29.  
    30. for (int x = 0; x < width; ++x) {
    31. for (int y = 0; y < height; ++y) {
    32. for (int z = 0; z < length; ++z) {
    33. int index = y * width * length + z * width + x;
    34. Block block = new Location(world, x + loc.getX(), y + loc.getY(), z + loc.getZ()).getBlock();
    35. block.setTypeIdAndData(blocks[index], blockData[index], true);
    36. }
    37. }
    38. }
    39. }
    40.  
    41. public static Schematic loadSchematic(File file) throws IOException
    42. {
    43. FileInputStream stream = new FileInputStream(file);
    44. NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(stream));
    45.  
    46. CompoundTag schematicTag = (CompoundTag) nbtStream.readTag();
    47. if (!schematicTag.getName().equals("Schematic")) {
    48. throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
    49. }
    50.  
    51. Map<String, Tag> schematic = schematicTag.getValue();
    52. if (!schematic.containsKey("Blocks")) {
    53. throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
    54. }
    55.  
    56. short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
    57. short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
    58. short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
    59.  
    60. String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
    61. if (!materials.equals("Alpha")) {
    62. throw new IllegalArgumentException("Schematic file is not an Alpha schematic");
    63. }
    64.  
    65. byte[] blocks = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
    66. byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
    67. return new Schematic(blocks, blockData, width, length, height);
    68. }
    69.  
    70. /**
    71. * Get child tag of a NBT structure.
    72. *
    73. * @param items The parent tag map
    74. * @param key The name of the tag to get
    75. * @param expected The expected type of the tag
    76. * @return child tag casted to the expected type
    77. * @throws DataException if the tag does not exist or the tag is not of the
    78. * expected type
    79. */
    80. private static <T extends Tag> T getChildTag(Map<String, Tag> items, String key, Class<T> expected) throws IllegalArgumentException
    81. {
    82. if (!items.containsKey(key)) {
    83. throw new IllegalArgumentException("Schematic file is missing a \"" + key + "\" tag");
    84. }
    85. Tag tag = items.get(key);
    86. if (!expected.isInstance(tag)) {
    87. throw new IllegalArgumentException(key + " tag is not of tag type " + expected.getName());
    88. }
    89. return expected.cast(tag);
    90. }
    91. }
    92.  

    Triggering It
    Code:java
    1. File Builds = new File(this.getDataFolder() + File.separator + "builds");
    2. File sch = new File(Builds, File.separator + "worktable.schematic");
    3.  
    4. try {
    5. sc = SchematicBuilder.loadSchematic(sch);
    6. SchematicBuilder.pasteSchematic(event.getPlayer().getWorld(), event.getClickedBlock().getLocation(), sc);
    7. } catch (IOException e2) {
    8. e2.printStackTrace();
    9. }

    Error
    Code:java
    1. [21:56:54 ERROR]: Could not pass event PlayerInteractEvent to Plugin v1.0
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    4. va:427) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    5. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    6. a:62) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    7. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    8. ava:477) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    9. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    10. ava:462) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    11. at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    12. ractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-
    13. g2220179-b2948jnks]
    14. at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInt
    15. eractManager.java:374) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948j
    16. nks]
    17. at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    18. :626) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    19. at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60)
    20. [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    21. at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile
    22. :9) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    23. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    24. ) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    25. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    26. tbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    27. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    28. 51) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    29. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    30. 50) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    31. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    32. 41) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    33. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    34. :453) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    35. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    36. 17) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    37. Caused by: java.lang.NullPointerException
    38. at org.bukkit.craftbukkit.v1_7_R1.util.CraftMagicNumbers.getBlock(CraftM
    39. agicNumbers.java:62) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jn
    40. ks]
    41. at org.bukkit.craftbukkit.v1_7_R1.util.CraftMagicNumbers.getBlock(CraftM
    42. agicNumbers.java:18) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jn
    43. ks]
    44. at org.bukkit.craftbukkit.v1_7_R1.block.CraftBlock.getNMSBlock(CraftBloc
    45. k.java:53) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    46. at org.bukkit.craftbukkit.v1_7_R1.block.CraftBlock.setTypeIdAndData(Craf
    47. tBlock.java:127) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
    48. at me.trolltaylor.plugin.util.SchematicBuilder.pasteSchematic(Sche
    49. maticBuilder.java:35) ~[?:?]
    50. at me.trolltaylor.plugin.main.Main.onPlayerInteract(Main.java:77)
    51. ~[?:?]
    52. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    53. _17]
    54. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    55. _17]
    56. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    57. .7.0_17]
    58. at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_17]
    59. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    60. va:425) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-34-g2220179-b2948jnks]
     
Thread Status:
Not open for further replies.

Share This Page