title: Setting Block Data from Schematic in Bukkit?
body: <p>I am trying to load and paste a <code>.schematic</code> file (without hooking the MCEdit API) in bukkit. Below is the function/method i use to paste the schematic. While pasting, i keep getting a <code>NullPointerException</code> in the pasting process. When i logged what items were getting placed, i see grass blocks, stone, but not my chests, anything in the chests, or beacons (Maybe even more blocks). </p>
  
  <p>The error occurs on this line: <code>block.setData(blockData[index], true);</code></p>
  
  <p>I think this has to do something with the metaData, but how would i get that information from the schematic file and apply it to each of the blocks?</p>
  
  <p>Question: <code>How can i paste items with metaData like (Chest with contents, torches, beacons, etc.?</code></p>
  
  <pre><code>@SuppressWarnings("deprecation")
  public void pasteSchematic(World world, Location loc, Schematic schematic)
  {
      byte[] blocks = schematic.getBlocks();
      byte[] blockData = schematic.getData();
  
      short length = schematic.getLenght();
      short width = schematic.getWidth();
      short height = schematic.getHeight();
  
      for (int x = 0; x &lt; width; ++x) {
          for (int y = 0; y &lt; height; ++y) {
              for (int z = 0; z &lt; length; ++z) {
                  int index = y * width * length + z * width + x;
                  Block block = new Location(world, x + loc.getX(), y + loc.getY(), z + loc.getZ()).getBlock();
                  block.setTypeId(blocks[index], true);
                  block.setData(blockData[index], true);
                  if(block.getType() == Material.BEACON || block instanceof Beacon) {
                      // Add location up one block
                      getLogger().info("Block is a Beacon!");
                      spawnLocations.add(block.getLocation().add(new Location(block.getWorld(),0,1,0)));
                  } else {
                      getLogger().info("Block is a " + block.getType().toString() + " block!");
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And to load the schematic file:</p>
  
  <pre><code>public Schematic loadSchematic(File file) throws IOException
  {
      FileInputStream stream = new FileInputStream(file);
      @SuppressWarnings("resource")
      NBTInputStream nbtStream = new NBTInputStream(stream);
  
      CompoundTag schematicTag = (CompoundTag) nbtStream.readTag();
      if (!schematicTag.getName().equals("Schematic")) {
          throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
      }
  
      Map&lt;String, Tag&gt; schematic = schematicTag.getValue();
      if (!schematic.containsKey("Blocks")) {
          throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
      }
  
      short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
      short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
      short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
  
      String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
      if (!materials.equals("Alpha")) {
          throw new IllegalArgumentException("Schematic file is not an Alpha schematic");
      }
  
      byte[] blocks = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
      byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
      return new Schematic(blocks, blockData, width, length, height);
  }
  
  /**
  * Get child tag of a NBT structure.
  *
  * @param items The parent tag map
  * @param key The name of the tag to get
  * @param expected The expected type of the tag
  * @return child tag casted to the expected type
  * @throws DataException if the tag does not exist or the tag is not of the
  * expected type
  */
  private static &lt;T extends Tag&gt; T getChildTag(Map&lt;String, Tag&gt; items, String key, Class&lt;T&gt; expected) throws IllegalArgumentException
  {
      if (!items.containsKey(key)) {
          throw new IllegalArgumentException("Schematic file is missing a \"" + key + "\" tag");
      }
      Tag tag = items.get(key);
      if (!expected.isInstance(tag)) {
          throw new IllegalArgumentException(key + " tag is not of tag type " + expected.getName());
      }
      return expected.cast(tag);
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  After further testing, even if i remove the chest and the beacon (just grass and stone) the error still occurs. I am calling this event <code>onSignChange</code> if that helps.</p>
  
  <p>Below is the error in the console:</p>
  
  <pre><code>[21:34:22 ERROR]: Could not pass event SignChangeEvent to SkyWars v1.0.0
  org.bukkit.event.EventException
          at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
  va:294) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
  a:62) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
  ava:501) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
  ava:486) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
  :1586) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.PacketPlayInUpdateSign.a(SourceFile:48)
  [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.PacketPlayInUpdateSign.handle(SourceFile
  :9) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
  ) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [serv
  er.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
  67) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
  60) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
  58) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
  :469) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
  28) [server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
  Caused by: java.lang.NullPointerException
          at org.bukkit.craftbukkit.v1_7_R3.util.CraftMagicNumbers.getBlock(CraftM
  agicNumbers.java:80) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.craftbukkit.v1_7_R3.util.CraftMagicNumbers.getBlock(CraftM
  agicNumbers.java:36) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.craftbukkit.v1_7_R3.block.CraftBlock.getNMSBlock(CraftBloc
  k.java:55) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.craftbukkit.v1_7_R3.block.CraftBlock.setTypeIdAndData(Craf
  tBlock.java:129) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at org.bukkit.craftbukkit.v1_7_R3.block.CraftBlock.setTypeId(CraftBlock.
  java:124) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          at com.FeaRCode.SkyWars.SkyWars.pasteSchematic(SkyWars.java:132) ~[?:?]
          at com.FeaRCode.SkyWars.GameEvents.OnSignChange(GameEvents.java:33) ~[?:
  ?]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
  _05]
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
  _05]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
  .8.0_05]
          at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_05]
          at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
  va:292) ~[server.jar:git-Bukkit-1.7.9-R0.1-6-g4d832c3-b3090jnks]
          ... 13 more
  </code></pre>
  
  <p>Line 132 in skywars is this: <code>block.setData(blockData[index], true);</code>
  The line in GameEvents is when i call this Method.</p>
  
  <p><strong>Update 2</strong>
  Here is some code with the API Usage</p>
  
  <pre><code>public void pasteSchematic(World world, Location loc)
  {
      File schematic = new File(this.getDataFolder() + File.separator + fileName);
      Location topLeft;
      Location bottomRight;
      Vector v = new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
      BukkitWorld BWf = new BukkitWorld(currentWorld);
      EditSession es = new EditSession(BWf, -1);
      try {
          CuboidClipboard cc = SchematicFormat.getFormat(schematic).load(schematic);
          try {
              cc.paste(es, v, true);
              topLeft = new Location(currentWorld, loc.getBlockX() + cc.getWidth(), loc.getBlockY() + cc.getHeight(), loc.getBlockZ() + cc.getLength());
              bottomRight = new Location(currentWorld, loc.getBlockX() - cc.getWidth(), loc.getBlockY() - cc.getHeight(), loc.getBlockZ() - cc.getLength());
              calculateSpawnLocations(topLeft, bottomRight);
          } catch (MaxChangedBlocksException e) {
          e.printStackTrace();
          }
      } catch (IOException e) {
          e.printStackTrace();
      } catch (DataException e) {
          e.printStackTrace();
      }
  
  }
  
  public void calculateSpawnLocations(Location loc1, Location loc2)
  {
      int topBlockX = (loc1.getBlockX() &lt; loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
      int bottomBlockX = (loc1.getBlockX() &gt; loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
  
      int topBlockY = (loc1.getBlockY() &lt; loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
      int bottomBlockY = (loc1.getBlockY() &gt; loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
  
      int topBlockZ = (loc1.getBlockZ() &lt; loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
      int bottomBlockZ = (loc1.getBlockZ() &gt; loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
  
      for(int x = bottomBlockX; x &lt;= topBlockX; x++)
      {
          for(int z = bottomBlockZ; z &lt;= topBlockZ; z++)
          {
              for(int y = bottomBlockY; y &lt;= topBlockY; y++)
              {
                  Block block = loc1.getWorld().getBlockAt(x, y, z);
                  if(block instanceof Beacon || block.getType() == Material.BEACON || block.getType().equals(Material.BEACON)) {
                      // Add location up one block
                      getLogger().info("Block is a Beacon!");
                      spawnLocations.add(block.getLocation().add(new Location(block.getWorld(),0,1,0)));
                  } else {
                      getLogger().info("Block is a " + block.getType().toString() + " block!");
                  }
              }
          }
      }
  }
  </code></pre>