ExpDev

JSONSpawn

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