From f706ea7ac2341ab6ae6f5bf34d58204e8d5db64a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 31 Aug 2012 16:16:24 +0200 Subject: [PATCH] Improvements to the simple config. Added an init method to the store coll. --- src/com/massivecraft/mcore4/SimpleConfig.java | 19 +++++++++++++++---- src/com/massivecraft/mcore4/store/Coll.java | 16 ++++++++++------ .../mcore4/store/CollInterface.java | 6 ++++++ src/com/massivecraft/mcore4/store/Entity.java | 1 + 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/com/massivecraft/mcore4/SimpleConfig.java b/src/com/massivecraft/mcore4/SimpleConfig.java index 0600c16b..e0790d07 100644 --- a/src/com/massivecraft/mcore4/SimpleConfig.java +++ b/src/com/massivecraft/mcore4/SimpleConfig.java @@ -13,7 +13,7 @@ public class SimpleConfig protected transient MPlugin mplugin; protected MPlugin mplugin() { return this.mplugin; } - protected transient File file = new File("plugins/mcore/conf.json"); + protected transient File file; protected File file() { return this.file; } public SimpleConfig(MPlugin mplugin, File file) @@ -36,20 +36,31 @@ public class SimpleConfig // IO // -------------------------------------------- // + protected static boolean contentRequestsDefaults(String content) + { + if (content == null) return false; + char c = content.charAt(0); + return c == 'd' || c == 'D'; + } + public void load() { if (this.file().isFile()) { String content = DiscUtil.readCatch(this.file()); - SimpleConfig loaded = this.mplugin().gson.fromJson(content, this.getClass()); - Accessor.get(this.getClass()).copy(loaded, this); + if (contentRequestsDefaults(content)) return; + Object createdByGson = this.mplugin().gson.fromJson(content, this.getClass()); + Accessor.get(this.getClass()).copy(createdByGson, this); } save(); } public void save() { - String content = this.mplugin().gson.toJson(this); + String content = DiscUtil.readCatch(this.file()); + if (contentRequestsDefaults(content)) return; + content = this.mplugin().gson.toJson(this); DiscUtil.writeCatch(file, content); } } + diff --git a/src/com/massivecraft/mcore4/store/Coll.java b/src/com/massivecraft/mcore4/store/Coll.java index ea4fc024..e1103877 100644 --- a/src/com/massivecraft/mcore4/store/Coll.java +++ b/src/com/massivecraft/mcore4/store/Coll.java @@ -504,7 +504,7 @@ public class Coll implements CollInterface @Override public Thread examineThread() { return this.examineThread; } // -------------------------------------------- // - // CONSTRUCTORS + // CONSTRUCT // -------------------------------------------- // public Coll(Db db, MPlugin mplugin, String idStrategyName, String name, Class entityClass, Class idClass, boolean creative) @@ -533,15 +533,19 @@ public class Coll implements CollInterface { @Override public void run() { me.onTick(); } }; - - this.examineThread = new ExamineThread(this); - this.examineThread.start(); - this.syncAll(); - instances.add(this); } public Coll(MPlugin mplugin, String idStrategyName, String name, Class entityClass, Class idClass, boolean creative) { this(MCore.getDb(), mplugin, idStrategyName, name, entityClass, idClass, creative); } + + @Override + public void init() + { + this.syncAll(); + this.examineThread = new ExamineThread(this); + this.examineThread.start(); + instances.add(this); + } } diff --git a/src/com/massivecraft/mcore4/store/CollInterface.java b/src/com/massivecraft/mcore4/store/CollInterface.java index 49536206..c5b2bae9 100644 --- a/src/com/massivecraft/mcore4/store/CollInterface.java +++ b/src/com/massivecraft/mcore4/store/CollInterface.java @@ -133,5 +133,11 @@ public interface CollInterface public Thread examineThread(); + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public void init(); + } \ No newline at end of file diff --git a/src/com/massivecraft/mcore4/store/Entity.java b/src/com/massivecraft/mcore4/store/Entity.java index 0ccfbb7a..69f1e1f0 100644 --- a/src/com/massivecraft/mcore4/store/Entity.java +++ b/src/com/massivecraft/mcore4/store/Entity.java @@ -46,6 +46,7 @@ public abstract class Entity, L> public L getId() { + if (this.getColl() == null) return null; return this.getColl().id(getThis()); }