Advertisement
ExpDev

JSONRandomPoints

May 7th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. package me.expdev.gkitpvp.persist.json;
  2.  
  3. /*
  4.  * Project created by ExpDev
  5.  */
  6.  
  7. import com.google.gson.Gson;
  8. import com.google.gson.reflect.TypeToken;
  9. import me.expdev.gkitpvp.GKitPvPPlugin;
  10. import me.expdev.gkitpvp.GLocation;
  11. import me.expdev.gkitpvp.utils.DiscUtil;
  12.  
  13. import java.io.File;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16.  
  17. public class JSONRandomPoints extends JSONPoints {
  18.  
  19.     // Info on how to persist
  20.     private Gson gson;
  21.     private File file;
  22.  
  23.     public JSONRandomPoints() {
  24.         file = new File(GKitPvPPlugin.p.getDataFolder(), "points.json");
  25.         gson = GKitPvPPlugin.gson;
  26.     }
  27.  
  28.     public Gson getGson() {
  29.         return gson;
  30.     }
  31.  
  32.     public void setGson(Gson gson) {
  33.         this.gson = gson;
  34.     }
  35.  
  36.     public void forceSave() {
  37.         forceSave(true);
  38.     }
  39.  
  40.     public void forceSave(boolean sync) {
  41.         saveCore(file, this.points, sync);
  42.     }
  43.  
  44.     private boolean saveCore(File target, Map<String, GLocation> data, boolean sync) {
  45.         return DiscUtil.writeCatch(target, this.gson.toJson(data), sync);
  46.     }
  47.  
  48.     public void load() {
  49.         Map<String, GLocation> points = this.loadCore();
  50.         if (points == null) {
  51.             return;
  52.         }
  53.  
  54.         this.points.clear();
  55.         this.points.putAll(points);
  56.         GKitPvPPlugin.p.log("Loaded " + points.size() + " points");
  57.  
  58.  
  59.     }
  60.  
  61.     private Map<String, GLocation> loadCore() {
  62.         if (!this.file.exists()) {
  63.             return new HashMap<String, GLocation>();
  64.         }
  65.  
  66.         String content = DiscUtil.readCatch(this.file);
  67.         if (content == null) {
  68.             return null;
  69.         }
  70.  
  71.         Map<String, GLocation> data = this.gson.fromJson(
  72.                 content,
  73.                 new TypeToken<Map<String, GLocation>>() {
  74.                 }.getType());
  75.  
  76.         saveCore(this.file, data, true); // Update the flatfile
  77.  
  78.         return data;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement