package me.jacobcrofts.simplestructureloader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public final class StructureAPI {
	
	private StructureAPI() {}
	
	public static void writeToFile(String filename, Selection selection) throws IOException {
		
		File structures = new File("plugins/structures");
		if (!(structures.exists())) {			
			structures.mkdirs();
		}
		
		FileWriter writer = new FileWriter("plugins/structures/" + filename + ".json");
		writer.write(selection.toJSON().toJSONString());
		writer.close();
		
	}
	
	public static JSONArray readFromFile(String path) throws FileNotFoundException, IOException, ParseException {
		return (JSONArray) new JSONParser().parse(new FileReader(path));
	}
	
	@SuppressWarnings("deprecation")
	public static void placeStructure(Selection selection, Location baseLocation) {
		for (SavedBlock block : selection.getSavedBlocks()) {
			Block realBlock = baseLocation.add(block.getRelativeX(), block.getRelativeY(), block.getRelativeZ()).getBlock();
			realBlock.setType(block.getType());
			realBlock.setData(block.getData());
		}
	}
	
}

@@ -1116,17 +1116,22 @@
 edBlock 
-b
+savedB
 lock : s
@@ -1197,13 +1197,26 @@
 ion.
-add(b
+clone().add(savedB
 lock
@@ -1228,25 +1228,30 @@
 elativeX(), 
-b
+savedB
 lock.getRela
@@ -1259,17 +1259,22 @@
 iveY(), 
-b
+savedB
 lock.get
@@ -1319,17 +1319,22 @@
 setType(
-b
+savedB
 lock.get
@@ -1363,17 +1363,22 @@
 setData(
-b
+savedB
 lock.get

Fix bug: pasting structure destructively adds to location