package me.jacobcrofts.simplestructureloader.util;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.json.simple.JSONObject;

public class SavedBlock {

	private final Material type;
	private final byte data;
	private final int relativeX;
	private final int relativeY;
	private final int relativeZ;
	
	@SuppressWarnings("deprecation")
	public SavedBlock(Location baseLocation, Block block) {
		this.type = block.getType();
		this.data = block.getData();
		Location relativeLocation = block.getLocation().subtract(baseLocation);
		this.relativeX = relativeLocation.getBlockX();
		this.relativeY = relativeLocation.getBlockY();
		this.relativeZ = relativeLocation.getBlockZ();
	}
	
	public Material getType() {
		return this.type;
	}
	
	public byte getData() {
		return this.data;
	}
	
	public int getRelativeX() {
		return this.relativeX;
	}
	
	public int getRelativeY() {
		return this.relativeY;
	}
	
	public int getRelativeZ() {
		return this.relativeZ;
	}
	
	@SuppressWarnings("unchecked")
	public JSONObject toJSON() {
		JSONObject json = new JSONObject();
		json.put("type", this.type);
		json.put("data", this.data);
		json.put("relative-x", this.relativeX);
		json.put("relative-y", this.relativeY);
		json.put("relative-z", this.relativeZ);
		return json;
	}
	
}

@@ -718,24 +718,356 @@
 ckZ();%0A%09%7D%0A%09%0A
+%09public SavedBlock(JSONObject savedBlockData) %7B%0A%09%09this.type = (Material) savedBlockData.get(%22type%22);%0A%09%09this.data = (byte) savedBlockData.get(%22data%22);%0A%09%09this.relativeX = (int) savedBlockData.get(%22relative-x%22);%0A%09%09this.relativeY = (int) savedBlockData.get(%22relative-y%22);%0A%09%09this.relativeZ = (int) savedBlockData.get(%22relative-z%22);%0A%09%7D%0A%09%0A
 %09public Mate
@@ -1079,24 +1079,24 @@
 getType() %7B%0A
-
 %09%09return thi
@@ -1410,20 +1410,30 @@
 NObject 
-json
+savedBlockData
  = new J
@@ -1443,28 +1443,38 @@
 Object();%0A%09%09
-json
+savedBlockData
 .put(%22type%22,
@@ -1484,28 +1484,38 @@
 is.type);%0A%09%09
-json
+savedBlockData
 .put(%22data%22,
@@ -1525,28 +1525,38 @@
 is.data);%0A%09%09
-json
+savedBlockData
 .put(%22relati
@@ -1577,28 +1577,38 @@
 lativeX);%0A%09%09
-json
+savedBlockData
 .put(%22relati
@@ -1633,20 +1633,30 @@
 veY);%0A%09%09
-json
+savedBlockData
 .put(%22re
@@ -1692,20 +1692,30 @@
 %09return 
-json
+savedBlockData
 ;%0A%09%7D%0A%09%0A%7D

Add feature: SavedBlock may be initialized with a JSONObject