From 7e0305aec08002028b17fc9c3fa2fcb3f6031af2 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 31 Aug 2012 15:30:20 +0200 Subject: [PATCH] A bugfix and a simple config class. --- src/com/massivecraft/mcore4/Conf.java | 28 +++------- src/com/massivecraft/mcore4/MCore.java | 12 +++- src/com/massivecraft/mcore4/MPlugin.java | 7 +-- src/com/massivecraft/mcore4/SimpleConfig.java | 55 +++++++++++++++++++ 4 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 src/com/massivecraft/mcore4/SimpleConfig.java diff --git a/src/com/massivecraft/mcore4/Conf.java b/src/com/massivecraft/mcore4/Conf.java index ab287935..6de4a8ec 100644 --- a/src/com/massivecraft/mcore4/Conf.java +++ b/src/com/massivecraft/mcore4/Conf.java @@ -3,31 +3,21 @@ package com.massivecraft.mcore4; import java.io.File; import java.util.UUID; -import com.massivecraft.mcore4.util.DiscUtil; - -public class Conf +public class Conf extends SimpleConfig { + // -------------------------------------------- // + // CONTENT + // -------------------------------------------- // + public static String dburi = "gson://./mstore"; public static String serverid = UUID.randomUUID().toString(); // -------------------------------------------- // - // Persistance + // META // -------------------------------------------- // - private static transient File file = new File("plugins/mcore/conf.json"); - private static transient Conf i = new Conf(); - public static void load() + public static transient Conf i = new Conf(); + private Conf() { - if (file.isFile()) - { - String content = DiscUtil.readCatch(file); - MCore.gson.fromJson(content, Conf.class); - } - save(); - } - - public static void save() - { - String content = MCore.gson.toJson(i, i.getClass()); - DiscUtil.writeCatch(file, content); + super(MCore.p, new File("plugins/mcore/conf.json")); } } diff --git a/src/com/massivecraft/mcore4/MCore.java b/src/com/massivecraft/mcore4/MCore.java index ce168e63..2f5bdfbf 100644 --- a/src/com/massivecraft/mcore4/MCore.java +++ b/src/com/massivecraft/mcore4/MCore.java @@ -43,6 +43,16 @@ public class MCore extends MPlugin private static Db db; public static Db getDb() { return db; } + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + protected static MCore p; + public MCore() + { + p = this; + } + // -------------------------------------------- // // NON STATIC :) // -------------------------------------------- // @@ -68,7 +78,7 @@ public class MCore extends MPlugin if ( ! preEnable()) return; - Conf.load(); + Conf.i.load(); // Setup the default database db = MStore.getDb(Conf.dburi); diff --git a/src/com/massivecraft/mcore4/MPlugin.java b/src/com/massivecraft/mcore4/MPlugin.java index 010ccd0f..e2134d1f 100644 --- a/src/com/massivecraft/mcore4/MPlugin.java +++ b/src/com/massivecraft/mcore4/MPlugin.java @@ -1,6 +1,5 @@ package com.massivecraft.mcore4; -import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; @@ -67,14 +66,12 @@ public abstract class MPlugin extends JavaPlugin implements Listener Persist.instances.remove(this.persist); // Collection shutdowns for new system. - Iterator> iter = Coll.instances.iterator(); - while (iter.hasNext()) + for (Coll coll : Coll.instances) { - Coll coll = iter.next(); if (coll.mplugin() != this) continue; coll.examineThread().interrupt(); coll.syncAll(); // TODO: Save outwards only? We may want to avoid loads at this stage... - iter.remove(); + Coll.instances.remove(coll); } log("Disabled"); diff --git a/src/com/massivecraft/mcore4/SimpleConfig.java b/src/com/massivecraft/mcore4/SimpleConfig.java new file mode 100644 index 00000000..0600c16b --- /dev/null +++ b/src/com/massivecraft/mcore4/SimpleConfig.java @@ -0,0 +1,55 @@ +package com.massivecraft.mcore4; + +import java.io.File; + +import com.massivecraft.mcore4.store.accessor.Accessor; +import com.massivecraft.mcore4.util.DiscUtil; + +public class SimpleConfig +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + protected transient MPlugin mplugin; + protected MPlugin mplugin() { return this.mplugin; } + + protected transient File file = new File("plugins/mcore/conf.json"); + protected File file() { return this.file; } + + public SimpleConfig(MPlugin mplugin, File file) + { + this.mplugin = mplugin; + this.file = file; + } + + public SimpleConfig(MPlugin mplugin, String confname) + { + this(mplugin, new File(mplugin.getDataFolder(), confname+".json")); + } + + public SimpleConfig(MPlugin mplugin) + { + this(mplugin, "conf"); + } + + // -------------------------------------------- // + // IO + // -------------------------------------------- // + + 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); + } + save(); + } + + public void save() + { + String content = this.mplugin().gson.toJson(this); + DiscUtil.writeCatch(file, content); + } +}