MassiveCore - Automatic Collection Constructors

This commit is contained in:
Olof Larsson 2016-06-23 11:55:13 +02:00
parent 319293a5b2
commit f2ae30319f
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
6 changed files with 94 additions and 24 deletions

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.massivecraft.massivecore.store.Coll; import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.MStore;
public class AspectColl extends Coll<Aspect> public class AspectColl extends Coll<Aspect>
{ {
@ -14,10 +13,6 @@ public class AspectColl extends Coll<Aspect>
private static AspectColl i = new AspectColl(); private static AspectColl i = new AspectColl();
public static AspectColl get() { return i; } public static AspectColl get() { return i; }
private AspectColl()
{
super("massivecore_aspect", Aspect.class, MStore.getDb("default"), MassiveCore.get());
}
// -------------------------------------------- // // -------------------------------------------- //
// STACK TRACEABILITY // STACK TRACEABILITY

View File

@ -2,7 +2,6 @@ package com.massivecraft.massivecore;
import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.store.Coll; import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.MStore;
public class MassiveCoreMConfColl extends Coll<MassiveCoreMConf> public class MassiveCoreMConfColl extends Coll<MassiveCoreMConf>
{ {
@ -14,7 +13,7 @@ public class MassiveCoreMConfColl extends Coll<MassiveCoreMConf>
public static MassiveCoreMConfColl get() { return i; } public static MassiveCoreMConfColl get() { return i; }
private MassiveCoreMConfColl() private MassiveCoreMConfColl()
{ {
super("massivecore_mconf", MassiveCoreMConf.class, MStore.getDb(), MassiveCore.get()); super("massivecore_mconf");
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,7 +1,6 @@
package com.massivecraft.massivecore; package com.massivecraft.massivecore;
import com.massivecraft.massivecore.store.Coll; import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.MStore;
public class MassiveCoreMSponsorInfoColl extends Coll<MassiveCoreMSponsorInfo> public class MassiveCoreMSponsorInfoColl extends Coll<MassiveCoreMSponsorInfo>
{ {
@ -13,7 +12,7 @@ public class MassiveCoreMSponsorInfoColl extends Coll<MassiveCoreMSponsorInfo>
public static MassiveCoreMSponsorInfoColl get() { return i; } public static MassiveCoreMSponsorInfoColl get() { return i; }
private MassiveCoreMSponsorInfoColl() private MassiveCoreMSponsorInfoColl()
{ {
super("massivecore_msponsorinfo", MassiveCoreMSponsorInfo.class, MStore.getDb(), MassiveCore.get()); super("massivecore_msponsorinfo");
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,7 +1,6 @@
package com.massivecraft.massivecore; package com.massivecraft.massivecore;
import com.massivecraft.massivecore.store.Coll; import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.MStore;
public class MultiverseColl extends Coll<Multiverse> public class MultiverseColl extends Coll<Multiverse>
{ {
@ -13,7 +12,7 @@ public class MultiverseColl extends Coll<Multiverse>
public static MultiverseColl get() { return i; } public static MultiverseColl get() { return i; }
private MultiverseColl() private MultiverseColl()
{ {
super("massivecore_multiverse", Multiverse.class, MStore.getDb("default"), MassiveCore.get()); super("massivecore_multiverse");
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,5 +1,7 @@
package com.massivecraft.massivecore.store; package com.massivecraft.massivecore.store;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -11,6 +13,10 @@ import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap; 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.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf; import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.MassivePlugin; import com.massivecraft.massivecore.MassivePlugin;
@ -85,10 +91,10 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
return plugin.getGson(); return plugin.getGson();
} }
protected Db db; protected final Db db;
@Override public Db getDb() { return this.db; } @Override public Db getDb() { return this.db; }
protected Object collDriverObject; protected final Object collDriverObject;
@Override public Object getCollDriverObject() { return this.collDriverObject; } @Override public Object getCollDriverObject() { return this.collDriverObject; }
@Override @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) 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; this.id = id;
String[] idParts = this.id.split("\\@"); String[] idParts = this.id.split("\\@");
this.basename = idParts[0]; this.basename = idParts[0];
@ -957,26 +972,79 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
this.universe = null; this.universe = null;
} }
// WHAT DO WE HANDLE? // Db
this.entityClass = entityClass; if (db == null) db = this.calculateDb();
// SUPPORTING SYSTEM
this.plugin = plugin;
this.db = db; this.db = db;
this.collDriverObject = db.createCollDriverObject(this); this.collDriverObject = db.createCollDriverObject(this);
// STORAGE // Collections
this.id2entity = new ConcurrentHashMap<String, E>(); this.id2entity = new ConcurrentHashMap<String, E>();
// ENTITY DATA
this.identifiedModifications = new ConcurrentHashMap<String, Modification>(); this.identifiedModifications = new ConcurrentHashMap<String, Modification>();
// Tasks
this.tickTask = new Runnable() this.tickTask = new Runnable()
{ {
@Override public void run() { Coll.this.onTick(); } @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 // ACTIVE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -21,12 +21,22 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
// CONSTRUCT // 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.setCreative(true);
this.setLowercasing(true); this.setLowercasing(true);
} }
public SenderColl(String id)
{
this(id, null, null, null);
}
public SenderColl()
{
this(null, null, null, null);
}
// -------------------------------------------- // // -------------------------------------------- //
// STACK TRACEABILITY // STACK TRACEABILITY