MassiveCore - Automatic Collection Constructors
This commit is contained in:
parent
319293a5b2
commit
f2ae30319f
@ -4,7 +4,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.massivecraft.massivecore.store.Coll;
|
||||
import com.massivecraft.massivecore.store.MStore;
|
||||
|
||||
public class AspectColl extends Coll<Aspect>
|
||||
{
|
||||
@ -14,10 +13,6 @@ public class AspectColl extends Coll<Aspect>
|
||||
|
||||
private static AspectColl i = new AspectColl();
|
||||
public static AspectColl get() { return i; }
|
||||
private AspectColl()
|
||||
{
|
||||
super("massivecore_aspect", Aspect.class, MStore.getDb("default"), MassiveCore.get());
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STACK TRACEABILITY
|
||||
|
@ -2,7 +2,6 @@ package com.massivecraft.massivecore;
|
||||
|
||||
import com.massivecraft.massivecore.MassiveCore;
|
||||
import com.massivecraft.massivecore.store.Coll;
|
||||
import com.massivecraft.massivecore.store.MStore;
|
||||
|
||||
public class MassiveCoreMConfColl extends Coll<MassiveCoreMConf>
|
||||
{
|
||||
@ -14,7 +13,7 @@ public class MassiveCoreMConfColl extends Coll<MassiveCoreMConf>
|
||||
public static MassiveCoreMConfColl get() { return i; }
|
||||
private MassiveCoreMConfColl()
|
||||
{
|
||||
super("massivecore_mconf", MassiveCoreMConf.class, MStore.getDb(), MassiveCore.get());
|
||||
super("massivecore_mconf");
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.massivecraft.massivecore;
|
||||
|
||||
import com.massivecraft.massivecore.store.Coll;
|
||||
import com.massivecraft.massivecore.store.MStore;
|
||||
|
||||
public class MassiveCoreMSponsorInfoColl extends Coll<MassiveCoreMSponsorInfo>
|
||||
{
|
||||
@ -13,7 +12,7 @@ public class MassiveCoreMSponsorInfoColl extends Coll<MassiveCoreMSponsorInfo>
|
||||
public static MassiveCoreMSponsorInfoColl get() { return i; }
|
||||
private MassiveCoreMSponsorInfoColl()
|
||||
{
|
||||
super("massivecore_msponsorinfo", MassiveCoreMSponsorInfo.class, MStore.getDb(), MassiveCore.get());
|
||||
super("massivecore_msponsorinfo");
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.massivecraft.massivecore;
|
||||
|
||||
import com.massivecraft.massivecore.store.Coll;
|
||||
import com.massivecraft.massivecore.store.MStore;
|
||||
|
||||
public class MultiverseColl extends Coll<Multiverse>
|
||||
{
|
||||
@ -13,7 +12,7 @@ public class MultiverseColl extends Coll<Multiverse>
|
||||
public static MultiverseColl get() { return i; }
|
||||
private MultiverseColl()
|
||||
{
|
||||
super("massivecore_multiverse", Multiverse.class, MStore.getDb("default"), MassiveCore.get());
|
||||
super("massivecore_multiverse");
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.massivecraft.massivecore.store;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -11,6 +13,10 @@ import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.massivecraft.massivecore.MassiveCore;
|
||||
import com.massivecraft.massivecore.MassiveCoreMConf;
|
||||
import com.massivecraft.massivecore.MassivePlugin;
|
||||
@ -85,10 +91,10 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
|
||||
return plugin.getGson();
|
||||
}
|
||||
|
||||
protected Db db;
|
||||
protected final Db db;
|
||||
@Override public Db getDb() { return this.db; }
|
||||
|
||||
protected Object collDriverObject;
|
||||
protected final Object collDriverObject;
|
||||
@Override public Object getCollDriverObject() { return this.collDriverObject; }
|
||||
|
||||
@Override
|
||||
@ -944,7 +950,16 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
|
||||
|
||||
public Coll(String id, Class<E> entityClass, Db db, MassivePlugin plugin)
|
||||
{
|
||||
// Setup the name and the parsed parts
|
||||
// Plugin
|
||||
if (plugin == null) plugin = this.calculatePlugin();
|
||||
this.plugin = plugin;
|
||||
|
||||
// Entity Class
|
||||
if (entityClass == null) entityClass = this.calculateEntityClass();
|
||||
this.entityClass = entityClass;
|
||||
|
||||
// Id
|
||||
if (id == null) id = this.calculateId();
|
||||
this.id = id;
|
||||
String[] idParts = this.id.split("\\@");
|
||||
this.basename = idParts[0];
|
||||
@ -957,26 +972,79 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
|
||||
this.universe = null;
|
||||
}
|
||||
|
||||
// WHAT DO WE HANDLE?
|
||||
this.entityClass = entityClass;
|
||||
|
||||
// SUPPORTING SYSTEM
|
||||
this.plugin = plugin;
|
||||
// Db
|
||||
if (db == null) db = this.calculateDb();
|
||||
this.db = db;
|
||||
this.collDriverObject = db.createCollDriverObject(this);
|
||||
|
||||
// STORAGE
|
||||
// Collections
|
||||
this.id2entity = new ConcurrentHashMap<String, E>();
|
||||
|
||||
// ENTITY DATA
|
||||
this.identifiedModifications = new ConcurrentHashMap<String, Modification>();
|
||||
|
||||
// Tasks
|
||||
this.tickTask = new Runnable()
|
||||
{
|
||||
@Override public void run() { Coll.this.onTick(); }
|
||||
};
|
||||
}
|
||||
|
||||
public Coll(String id)
|
||||
{
|
||||
this(id, null, null, null);
|
||||
}
|
||||
|
||||
public Coll()
|
||||
{
|
||||
this(null, null, null, null);
|
||||
}
|
||||
|
||||
public MassivePlugin calculatePlugin()
|
||||
{
|
||||
// Create
|
||||
int retlength = 0;
|
||||
MassivePlugin ret = null;
|
||||
|
||||
// Fill
|
||||
String me = this.getClass().getName();
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins())
|
||||
{
|
||||
if (!(plugin instanceof MassivePlugin)) continue;
|
||||
MassivePlugin mplugin = (MassivePlugin)plugin;
|
||||
String you = mplugin.getDescription().getMain();
|
||||
|
||||
String prefix = StringUtils.getCommonPrefix(new String[]{me, you});
|
||||
if (prefix == null) continue;
|
||||
int length = prefix.length();
|
||||
if (length <= retlength) continue;
|
||||
|
||||
retlength = length;
|
||||
ret = mplugin;
|
||||
}
|
||||
|
||||
// Return
|
||||
if (ret == null) throw new RuntimeException("plugin could not be calculated");
|
||||
return ret;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<E> calculateEntityClass()
|
||||
{
|
||||
Class<?> clazz = this.getClass();
|
||||
ParameterizedType superType = (ParameterizedType) clazz.getGenericSuperclass();
|
||||
Type[] typeArguments = superType.getActualTypeArguments();
|
||||
return (Class<E>) typeArguments[0];
|
||||
}
|
||||
|
||||
public String calculateId()
|
||||
{
|
||||
return this.getPlugin().getDescription().getName().toLowerCase() + "_" + this.getEntityClass().getSimpleName().toLowerCase();
|
||||
}
|
||||
|
||||
public Db calculateDb()
|
||||
{
|
||||
return MStore.getDb();
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// ACTIVE
|
||||
// -------------------------------------------- //
|
||||
|
@ -21,12 +21,22 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
|
||||
// CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
public SenderColl(String name, Class<E> entityClass, Db db, MassivePlugin plugin)
|
||||
public SenderColl(String id, Class<E> entityClass, Db db, MassivePlugin plugin)
|
||||
{
|
||||
super(name, entityClass, db, plugin);
|
||||
super(id, entityClass, db, plugin);
|
||||
this.setCreative(true);
|
||||
this.setLowercasing(true);
|
||||
}
|
||||
|
||||
public SenderColl(String id)
|
||||
{
|
||||
this(id, null, null, null);
|
||||
}
|
||||
|
||||
public SenderColl()
|
||||
{
|
||||
this(null, null, null, null);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// STACK TRACEABILITY
|
||||
|
Loading…
Reference in New Issue
Block a user