From f2ae30319fb48441c02de677ab573ba12504302a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 23 Jun 2016 11:55:13 +0200 Subject: [PATCH] MassiveCore - Automatic Collection Constructors --- .../massivecraft/massivecore/AspectColl.java | 5 -- .../massivecore/MassiveCoreMConfColl.java | 3 +- .../MassiveCoreMSponsorInfoColl.java | 3 +- .../massivecore/MultiverseColl.java | 3 +- .../massivecraft/massivecore/store/Coll.java | 90 ++++++++++++++++--- .../massivecore/store/SenderColl.java | 14 ++- 6 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/com/massivecraft/massivecore/AspectColl.java b/src/com/massivecraft/massivecore/AspectColl.java index 82adcfbc..6ae5bcd3 100644 --- a/src/com/massivecraft/massivecore/AspectColl.java +++ b/src/com/massivecraft/massivecore/AspectColl.java @@ -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 { @@ -14,10 +13,6 @@ public class AspectColl extends Coll 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 diff --git a/src/com/massivecraft/massivecore/MassiveCoreMConfColl.java b/src/com/massivecraft/massivecore/MassiveCoreMConfColl.java index 9de52459..d8c5bacc 100644 --- a/src/com/massivecraft/massivecore/MassiveCoreMConfColl.java +++ b/src/com/massivecraft/massivecore/MassiveCoreMConfColl.java @@ -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 { @@ -14,7 +13,7 @@ public class MassiveCoreMConfColl extends Coll public static MassiveCoreMConfColl get() { return i; } private MassiveCoreMConfColl() { - super("massivecore_mconf", MassiveCoreMConf.class, MStore.getDb(), MassiveCore.get()); + super("massivecore_mconf"); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/MassiveCoreMSponsorInfoColl.java b/src/com/massivecraft/massivecore/MassiveCoreMSponsorInfoColl.java index 82768b7c..6685d830 100644 --- a/src/com/massivecraft/massivecore/MassiveCoreMSponsorInfoColl.java +++ b/src/com/massivecraft/massivecore/MassiveCoreMSponsorInfoColl.java @@ -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 { @@ -13,7 +12,7 @@ public class MassiveCoreMSponsorInfoColl extends Coll public static MassiveCoreMSponsorInfoColl get() { return i; } private MassiveCoreMSponsorInfoColl() { - super("massivecore_msponsorinfo", MassiveCoreMSponsorInfo.class, MStore.getDb(), MassiveCore.get()); + super("massivecore_msponsorinfo"); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/MultiverseColl.java b/src/com/massivecraft/massivecore/MultiverseColl.java index 37f9437f..8363a235 100644 --- a/src/com/massivecraft/massivecore/MultiverseColl.java +++ b/src/com/massivecraft/massivecore/MultiverseColl.java @@ -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 { @@ -13,7 +12,7 @@ public class MultiverseColl extends Coll public static MultiverseColl get() { return i; } private MultiverseColl() { - super("massivecore_multiverse", Multiverse.class, MStore.getDb("default"), MassiveCore.get()); + super("massivecore_multiverse"); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/store/Coll.java b/src/com/massivecraft/massivecore/store/Coll.java index 0c9868df..68acd534 100644 --- a/src/com/massivecraft/massivecore/store/Coll.java +++ b/src/com/massivecraft/massivecore/store/Coll.java @@ -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> extends CollAbstract 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> extends CollAbstract public Coll(String id, Class 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> extends CollAbstract 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(); - - // ENTITY DATA this.identifiedModifications = new ConcurrentHashMap(); + // 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 calculateEntityClass() + { + Class clazz = this.getClass(); + ParameterizedType superType = (ParameterizedType) clazz.getGenericSuperclass(); + Type[] typeArguments = superType.getActualTypeArguments(); + return (Class) typeArguments[0]; + } + + public String calculateId() + { + return this.getPlugin().getDescription().getName().toLowerCase() + "_" + this.getEntityClass().getSimpleName().toLowerCase(); + } + + public Db calculateDb() + { + return MStore.getDb(); + } + // -------------------------------------------- // // ACTIVE // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/store/SenderColl.java b/src/com/massivecraft/massivecore/store/SenderColl.java index 31a0eac0..3160195c 100644 --- a/src/com/massivecraft/massivecore/store/SenderColl.java +++ b/src/com/massivecraft/massivecore/store/SenderColl.java @@ -21,12 +21,22 @@ public class SenderColl> extends Coll implements Se // CONSTRUCT // -------------------------------------------- // - public SenderColl(String name, Class entityClass, Db db, MassivePlugin plugin) + public SenderColl(String id, Class 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