Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.expdev.gkitpvp.persist.json;
- /*
- * Project created by ExpDev
- */
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import me.expdev.gkitpvp.GKitPvPPlugin;
- import me.expdev.gkitpvp.GLocation;
- import me.expdev.gkitpvp.utils.DiscUtil;
- import java.io.File;
- import java.util.HashMap;
- import java.util.Map;
- public class JSONRandomPoints extends JSONPoints {
- // Info on how to persist
- private Gson gson;
- private File file;
- public JSONRandomPoints() {
- file = new File(GKitPvPPlugin.p.getDataFolder(), "points.json");
- gson = GKitPvPPlugin.gson;
- }
- public Gson getGson() {
- return gson;
- }
- public void setGson(Gson gson) {
- this.gson = gson;
- }
- public void forceSave() {
- forceSave(true);
- }
- public void forceSave(boolean sync) {
- saveCore(file, this.points, sync);
- }
- private boolean saveCore(File target, Map<String, GLocation> data, boolean sync) {
- return DiscUtil.writeCatch(target, this.gson.toJson(data), sync);
- }
- public void load() {
- Map<String, GLocation> points = this.loadCore();
- if (points == null) {
- return;
- }
- this.points.clear();
- this.points.putAll(points);
- GKitPvPPlugin.p.log("Loaded " + points.size() + " points");
- }
- private Map<String, GLocation> loadCore() {
- if (!this.file.exists()) {
- return new HashMap<String, GLocation>();
- }
- String content = DiscUtil.readCatch(this.file);
- if (content == null) {
- return null;
- }
- Map<String, GLocation> data = this.gson.fromJson(
- content,
- new TypeToken<Map<String, GLocation>>() {
- }.getType());
- saveCore(this.file, data, true); // Update the flatfile
- return data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement