Some work on putting the components together

This commit is contained in:
Olof Larsson 2012-08-31 14:53:15 +02:00
parent eba582f3f3
commit 6089d5e0ed
6 changed files with 122 additions and 17 deletions

View File

@ -0,0 +1,33 @@
package com.massivecraft.mcore4;
import java.io.File;
import java.util.UUID;
import com.massivecraft.mcore4.util.DiscUtil;
public class Conf
{
public static String dburi = "gson://./mstore";
public static String serverid = UUID.randomUUID().toString();
// -------------------------------------------- //
// Persistance
// -------------------------------------------- //
private static transient File file = new File("plugins/mcore/conf.json");
private static transient Conf i = new Conf();
public static void load()
{
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);
}
}

View File

@ -3,6 +3,7 @@ package com.massivecraft.mcore4;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Random; import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -10,6 +11,9 @@ import com.massivecraft.mcore4.adapter.InventoryAdapter;
import com.massivecraft.mcore4.adapter.ItemStackAdapter; import com.massivecraft.mcore4.adapter.ItemStackAdapter;
import com.massivecraft.mcore4.adapter.MongoURIAdapter; import com.massivecraft.mcore4.adapter.MongoURIAdapter;
import com.massivecraft.mcore4.persist.Persist; import com.massivecraft.mcore4.persist.Persist;
import com.massivecraft.mcore4.store.Coll;
import com.massivecraft.mcore4.store.Db;
import com.massivecraft.mcore4.store.MStore;
import com.massivecraft.mcore4.util.PlayerUtil; import com.massivecraft.mcore4.util.PlayerUtil;
import com.massivecraft.mcore4.xlib.gson.Gson; import com.massivecraft.mcore4.xlib.gson.Gson;
import com.massivecraft.mcore4.xlib.gson.GsonBuilder; import com.massivecraft.mcore4.xlib.gson.GsonBuilder;
@ -35,25 +39,49 @@ public class MCore extends MPlugin
.registerTypeAdapter(Inventory.class, new InventoryAdapter()); .registerTypeAdapter(Inventory.class, new InventoryAdapter());
} }
public static String getServerId() { return Conf.serverid; }
private static Db<?> db;
public static Db<?> getDb() { return db; }
// -------------------------------------------- // // -------------------------------------------- //
// NON STATIC :) // NON STATIC :)
// -------------------------------------------- // // -------------------------------------------- //
InternalListener listener; private Runnable collTickTask = new Runnable()
{
public void run()
{
for (Coll<?, ?> coll : Coll.instances)
{
coll.onTick();
}
}
};
@Override @Override
public void onEnable() public void onEnable()
{ {
// This is safe since all plugins using Persist should bukkit-depend this plugin.
// Note this one must be before preEnable. dooh.
Persist.instances.clear();
Coll.instances.clear();
if ( ! preEnable()) return;
Conf.load();
// Setup the default database
db = MStore.getDb(Conf.dburi);
// Setup PlayerUtil and it's events // Setup PlayerUtil and it's events
new PlayerUtil(this); new PlayerUtil(this);
// This is safe since all plugins using Persist should bukkit-depend this plugin.
Persist.instances.clear();
// Register events // Register events
this.listener = new InternalListener(this); new InternalListener(this);
// Schedule the collection ticker.
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this.collTickTask, 1, 1);
if ( ! preEnable()) return;
this.postEnable(); this.postEnable();
} }

View File

@ -1,5 +1,6 @@
package com.massivecraft.mcore4; package com.massivecraft.mcore4;
import java.util.Iterator;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -10,6 +11,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.massivecraft.mcore4.cmd.Cmd; import com.massivecraft.mcore4.cmd.Cmd;
import com.massivecraft.mcore4.persist.One; import com.massivecraft.mcore4.persist.One;
import com.massivecraft.mcore4.persist.Persist; import com.massivecraft.mcore4.persist.Persist;
import com.massivecraft.mcore4.store.Coll;
import com.massivecraft.mcore4.util.LibLoader; import com.massivecraft.mcore4.util.LibLoader;
import com.massivecraft.mcore4.util.Txt; import com.massivecraft.mcore4.util.Txt;
import com.massivecraft.mcore4.xlib.gson.Gson; import com.massivecraft.mcore4.xlib.gson.Gson;
@ -60,9 +62,21 @@ public abstract class MPlugin extends JavaPlugin implements Listener
public void onDisable() public void onDisable()
{ {
// Collection shutdowns for old system.
this.persist.saveAll(); this.persist.saveAll();
Persist.instances.remove(this.persist); Persist.instances.remove(this.persist);
// Collection shutdowns for new system.
Iterator<Coll<?, ?>> iter = Coll.instances.iterator();
while (iter.hasNext())
{
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();
}
log("Disabled"); log("Disabled");
} }

View File

@ -4,11 +4,14 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import com.massivecraft.mcore4.MCore;
import com.massivecraft.mcore4.MPlugin; import com.massivecraft.mcore4.MPlugin;
import com.massivecraft.mcore4.Predictate; import com.massivecraft.mcore4.Predictate;
import com.massivecraft.mcore4.store.idstrategy.IdStrategy; import com.massivecraft.mcore4.store.idstrategy.IdStrategy;
@ -16,6 +19,12 @@ import com.massivecraft.mcore4.store.storeadapter.StoreAdapter;
public class Coll<E, L> implements CollInterface<E, L> public class Coll<E, L> implements CollInterface<E, L>
{ {
// -------------------------------------------- //
// GLOBAL REGISTRY
// -------------------------------------------- //
public static List<Coll<?, ?>> instances = new CopyOnWriteArrayList<Coll<?, ?>>();
// -------------------------------------------- // // -------------------------------------------- //
// WHAT DO WE HANDLE? // WHAT DO WE HANDLE?
// -------------------------------------------- // // -------------------------------------------- //
@ -498,7 +507,7 @@ public class Coll<E, L> implements CollInterface<E, L>
// CONSTRUCTORS // CONSTRUCTORS
// -------------------------------------------- // // -------------------------------------------- //
public Coll(MPlugin mplugin, Db<?> db, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative) public Coll(Db<?> db, MPlugin mplugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative)
{ {
this.name = name; this.name = name;
this.entityClass = entityClass; this.entityClass = entityClass;
@ -526,5 +535,13 @@ public class Coll<E, L> implements CollInterface<E, L>
}; };
this.examineThread = new ExamineThread<E, L>(this); 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);
} }
} }

View File

@ -5,6 +5,8 @@ import java.net.URISyntaxException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.massivecraft.mcore4.MCore;
public class MStore public class MStore
{ {
@ -17,6 +19,7 @@ public class MStore
} }
public static Driver<?> getDriver(String id) public static Driver<?> getDriver(String id)
{ {
return drivers.get(id); return drivers.get(id);
} }
@ -36,6 +39,7 @@ public class MStore
public static Db<?> getDb(URI uri) public static Db<?> getDb(URI uri)
{ {
String scheme = uri.getScheme(); String scheme = uri.getScheme();
if (scheme.equalsIgnoreCase("default")) return MCore.getDb();
Driver<?> driver = getDriver(scheme); Driver<?> driver = getDriver(scheme);
if (driver == null) return null; if (driver == null) return null;
return driver.db(uri.toString()); return driver.db(uri.toString());

View File

@ -1,18 +1,27 @@
package com.massivecraft.mcore4.store; package com.massivecraft.mcore4.store;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import com.massivecraft.mcore4.MCore;
import com.massivecraft.mcore4.MPlugin; import com.massivecraft.mcore4.MPlugin;
import com.massivecraft.mcore4.Predictate; import com.massivecraft.mcore4.Predictate;
public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String> public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
{ {
public PlayerColl(MPlugin mplugin, Db<?> db, String name, Class<E> entityClass) public PlayerColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass)
{ {
super(mplugin, db, "ai", name, entityClass, String.class, true); super(db, mplugin, "ai", name, entityClass, String.class, true);
}
public PlayerColl(MPlugin mplugin, String name, Class<E> entityClass)
{
super(MCore.getDb(), mplugin, "ai", name, entityClass, String.class, true);
} }
@Override @Override
@ -27,14 +36,14 @@ public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
public Collection<E> getAllOnline() public Collection<E> getAllOnline()
{ {
// TODO: Reverse the order since we have fewer players online than offline? List<E> ret = new ArrayList<E>();
return this.getAll(new Predictate<E>() for(Player player : Bukkit.getOnlinePlayers())
{ {
public boolean apply(E entity) E entity = this.get(player.getName());
{ if (entity == null) continue;
return entity.isOnline(); ret.add(entity);
} }
}); return ret;
} }
public Collection<E> getAllOffline() public Collection<E> getAllOffline()