package Example;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.FileUtils;
import org.bukkit.Location;
import com.google.gson.Gson;
public class Saves {
private Map<String, Save> save = new HashMap<String, Save>();
private Main plugin;
Saves(Main plugin) {
this.plugin = plugin;
}
public Save getChunk(Location loc) {
int ChunkX = loc.getBlockX() >> 4;
int ChunkZ = loc.getBlockZ() >> 4;
String s = ChunkX + "." + ChunkZ + ".json";
if (save.containsKey(s)) {
return save.get(s);
} else {
String str = "{}";
Gson gson = new Gson();
try {
File file = new File(this.plugin.getDataFolder(), s);
if (!file.exists()) {
FileUtils.writeStringToFile(file, new Save().toString(), Charset.defaultCharset());
}
str = FileUtils.readFileToString(file, Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
save.put(s, gson.fromJson(str, Save.class));
return save.get(s);
}
}
public void save() {
for (Entry<String, Save> tmp : save.entrySet()) {
File file = new File(this.plugin.getDataFolder(), tmp.getKey());
try {
FileUtils.writeStringToFile(file, tmp.getValue().toString(), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}