Improvements to the simple config. Added an init method to the store coll.

This commit is contained in:
Olof Larsson 2012-08-31 16:16:24 +02:00
parent 7e0305aec0
commit f706ea7ac2
4 changed files with 32 additions and 10 deletions

View File

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

View File

@ -504,7 +504,7 @@ public class Coll<E, L> implements CollInterface<E, L>
@Override public Thread examineThread() { return this.examineThread; }
// -------------------------------------------- //
// CONSTRUCTORS
// CONSTRUCT
// -------------------------------------------- //
public Coll(Db<?> db, MPlugin mplugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative)
@ -533,15 +533,19 @@ public class Coll<E, L> implements CollInterface<E, L>
{
@Override public void run() { me.onTick(); }
};
this.examineThread = new ExamineThread<E, L>(this);
this.examineThread.start();
this.syncAll();
instances.add(this);
}
public Coll(MPlugin mplugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative)
{
this(MCore.getDb(), mplugin, idStrategyName, name, entityClass, idClass, creative);
}
@Override
public void init()
{
this.syncAll();
this.examineThread = new ExamineThread<E, L>(this);
this.examineThread.start();
instances.add(this);
}
}

View File

@ -133,5 +133,11 @@ public interface CollInterface<E, L>
public Thread examineThread();
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public void init();
}

View File

@ -46,6 +46,7 @@ public abstract class Entity<E extends Entity<E, L>, L>
public L getId()
{
if (this.getColl() == null) return null;
return this.getColl().id(getThis());
}