A bugfix and a simple config class.

This commit is contained in:
Olof Larsson 2012-08-31 15:30:20 +02:00
parent 6089d5e0ed
commit 7e0305aec0
4 changed files with 77 additions and 25 deletions

View File

@ -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"));
}
}

View File

@ -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);

View File

@ -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<Coll<?, ?>> 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");

View File

@ -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);
}
}