Some work on putting the components together
This commit is contained in:
parent
eba582f3f3
commit
6089d5e0ed
33
src/com/massivecraft/mcore4/Conf.java
Normal file
33
src/com/massivecraft/mcore4/Conf.java
Normal 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);
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.massivecraft.mcore4;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
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.MongoURIAdapter;
|
||||
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.xlib.gson.Gson;
|
||||
import com.massivecraft.mcore4.xlib.gson.GsonBuilder;
|
||||
@ -35,25 +39,49 @@ public class MCore extends MPlugin
|
||||
.registerTypeAdapter(Inventory.class, new InventoryAdapter());
|
||||
}
|
||||
|
||||
public static String getServerId() { return Conf.serverid; }
|
||||
private static Db<?> db;
|
||||
public static Db<?> getDb() { return db; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// NON STATIC :)
|
||||
// -------------------------------------------- //
|
||||
|
||||
InternalListener listener;
|
||||
private Runnable collTickTask = new Runnable()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
for (Coll<?, ?> coll : Coll.instances)
|
||||
{
|
||||
coll.onTick();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
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
|
||||
new PlayerUtil(this);
|
||||
|
||||
// This is safe since all plugins using Persist should bukkit-depend this plugin.
|
||||
Persist.instances.clear();
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.massivecraft.mcore4;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.logging.Level;
|
||||
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.persist.One;
|
||||
import com.massivecraft.mcore4.persist.Persist;
|
||||
import com.massivecraft.mcore4.store.Coll;
|
||||
import com.massivecraft.mcore4.util.LibLoader;
|
||||
import com.massivecraft.mcore4.util.Txt;
|
||||
import com.massivecraft.mcore4.xlib.gson.Gson;
|
||||
@ -60,9 +62,21 @@ public abstract class MPlugin extends JavaPlugin implements Listener
|
||||
|
||||
public void onDisable()
|
||||
{
|
||||
// Collection shutdowns for old system.
|
||||
this.persist.saveAll();
|
||||
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");
|
||||
}
|
||||
|
||||
|
@ -4,11 +4,14 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import com.massivecraft.mcore4.MCore;
|
||||
import com.massivecraft.mcore4.MPlugin;
|
||||
import com.massivecraft.mcore4.Predictate;
|
||||
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>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// GLOBAL REGISTRY
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static List<Coll<?, ?>> instances = new CopyOnWriteArrayList<Coll<?, ?>>();
|
||||
|
||||
// -------------------------------------------- //
|
||||
// WHAT DO WE HANDLE?
|
||||
// -------------------------------------------- //
|
||||
@ -498,7 +507,7 @@ public class Coll<E, L> implements CollInterface<E, L>
|
||||
// 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.entityClass = entityClass;
|
||||
@ -526,5 +535,13 @@ public class Coll<E, L> implements CollInterface<E, L>
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.massivecraft.mcore4.MCore;
|
||||
|
||||
|
||||
public class MStore
|
||||
{
|
||||
@ -17,6 +19,7 @@ public class MStore
|
||||
}
|
||||
public static Driver<?> getDriver(String id)
|
||||
{
|
||||
|
||||
return drivers.get(id);
|
||||
}
|
||||
|
||||
@ -36,6 +39,7 @@ public class MStore
|
||||
public static Db<?> getDb(URI uri)
|
||||
{
|
||||
String scheme = uri.getScheme();
|
||||
if (scheme.equalsIgnoreCase("default")) return MCore.getDb();
|
||||
Driver<?> driver = getDriver(scheme);
|
||||
if (driver == null) return null;
|
||||
return driver.db(uri.toString());
|
||||
|
@ -1,18 +1,27 @@
|
||||
package com.massivecraft.mcore4.store;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
|
||||
import com.massivecraft.mcore4.MCore;
|
||||
import com.massivecraft.mcore4.MPlugin;
|
||||
import com.massivecraft.mcore4.Predictate;
|
||||
|
||||
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
|
||||
@ -27,14 +36,14 @@ public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
|
||||
|
||||
public Collection<E> getAllOnline()
|
||||
{
|
||||
// TODO: Reverse the order since we have fewer players online than offline?
|
||||
return this.getAll(new Predictate<E>()
|
||||
List<E> ret = new ArrayList<E>();
|
||||
for(Player player : Bukkit.getOnlinePlayers())
|
||||
{
|
||||
public boolean apply(E entity)
|
||||
{
|
||||
return entity.isOnline();
|
||||
}
|
||||
});
|
||||
E entity = this.get(player.getName());
|
||||
if (entity == null) continue;
|
||||
ret.add(entity);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Collection<E> getAllOffline()
|
||||
|
Loading…
Reference in New Issue
Block a user