Automatic activation of engines, colls, commands and others

This commit is contained in:
Magnus Ulf Jørgensen 2017-03-10 16:23:26 +01:00
parent d3fc83386d
commit fe81648eb7
46 changed files with 632 additions and 459 deletions

View File

@ -2,7 +2,7 @@ package com.massivecraft.massivecore;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCore;
import com.massivecraft.massivecore.cmd.CmdMassiveCore;
import com.massivecraft.massivecore.command.requirement.Requirement;
import com.massivecraft.massivecore.command.requirement.RequirementAbstract;
import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer;

View File

@ -28,70 +28,18 @@ import com.massivecraft.massivecore.collections.MassiveTreeMap;
import com.massivecraft.massivecore.collections.MassiveTreeMapDef;
import com.massivecraft.massivecore.collections.MassiveTreeSet;
import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCore;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCoreBuffer;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCoreClick;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCoreCmdurl;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCoreStore;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCoreUsys;
import com.massivecraft.massivecore.command.type.RegistryType;
import com.massivecraft.massivecore.engine.EngineMassiveCoreChestGui;
import com.massivecraft.massivecore.engine.EngineMassiveCoreCollTick;
import com.massivecraft.massivecore.engine.EngineMassiveCoreCommandRegistration;
import com.massivecraft.massivecore.engine.EngineMassiveCoreCommandSet;
import com.massivecraft.massivecore.engine.EngineMassiveCoreDatabase;
import com.massivecraft.massivecore.engine.EngineMassiveCoreDestination;
import com.massivecraft.massivecore.engine.EngineMassiveCoreGank;
import com.massivecraft.massivecore.engine.EngineMassiveCoreLorePriority;
import com.massivecraft.massivecore.engine.EngineMassiveCoreMain;
import com.massivecraft.massivecore.engine.EngineMassiveCorePlayerLeave;
import com.massivecraft.massivecore.engine.EngineMassiveCorePlayerState;
import com.massivecraft.massivecore.engine.EngineMassiveCorePlayerUpdate;
import com.massivecraft.massivecore.engine.EngineMassiveCoreScheduledTeleport;
import com.massivecraft.massivecore.engine.EngineMassiveCoreSponsor;
import com.massivecraft.massivecore.engine.EngineMassiveCoreTeleportMixinCause;
import com.massivecraft.massivecore.engine.EngineMassiveCoreVariable;
import com.massivecraft.massivecore.engine.EngineMassiveCoreWorldNameSet;
import com.massivecraft.massivecore.integration.liability.IntegrationLiabilityAreaEffectCloud;
import com.massivecraft.massivecore.integration.vault.IntegrationVault;
import com.massivecraft.massivecore.item.DataBannerPattern;
import com.massivecraft.massivecore.item.WriterItemStack;
import com.massivecraft.massivecore.mixin.MixinActionbar;
import com.massivecraft.massivecore.mixin.MixinActual;
import com.massivecraft.massivecore.mixin.MixinCommand;
import com.massivecraft.massivecore.mixin.MixinDisplayName;
import com.massivecraft.massivecore.mixin.MixinEvent;
import com.massivecraft.massivecore.mixin.MixinGamemode;
import com.massivecraft.massivecore.mixin.MixinInventory;
import com.massivecraft.massivecore.mixin.MixinKick;
import com.massivecraft.massivecore.mixin.MixinLog;
import com.massivecraft.massivecore.mixin.MixinMassiveCraftPremium;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.mixin.MixinModification;
import com.massivecraft.massivecore.mixin.MixinPlayed;
import com.massivecraft.massivecore.mixin.MixinSenderPs;
import com.massivecraft.massivecore.mixin.MixinTeleport;
import com.massivecraft.massivecore.mixin.MixinTitle;
import com.massivecraft.massivecore.mixin.MixinVisibility;
import com.massivecraft.massivecore.mixin.MixinWorld;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.mson.MsonEvent;
import com.massivecraft.massivecore.nms.NmsBasics;
import com.massivecraft.massivecore.nms.NmsBoard;
import com.massivecraft.massivecore.nms.NmsChat;
import com.massivecraft.massivecore.nms.NmsEntityDamageEvent;
import com.massivecraft.massivecore.nms.NmsEntityGet;
import com.massivecraft.massivecore.nms.NmsItemStackCreate;
import com.massivecraft.massivecore.nms.NmsItemStackCreate17R4P;
import com.massivecraft.massivecore.nms.NmsItemStackTooltip;
import com.massivecraft.massivecore.nms.NmsPermissions;
import com.massivecraft.massivecore.nms.NmsPlayerInventoryCreate;
import com.massivecraft.massivecore.nms.NmsSkullMeta;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.ps.PSAdapter;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.ModificationPollerLocal;
import com.massivecraft.massivecore.store.ModificationPollerRemote;
import com.massivecraft.massivecore.test.TestTypeEnchantment;
import com.massivecraft.massivecore.util.BoardUtil;
import com.massivecraft.massivecore.util.ContainerUtil;
import com.massivecraft.massivecore.util.EventUtil;
@ -123,6 +71,7 @@ import org.bukkit.inventory.PlayerInventory;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
@ -277,85 +226,17 @@ public class MassiveCore extends MassivePlugin
RegistryType.registerAll();
// Activate
this.activate(
// Coll
MultiverseColl.class,
AspectColl.class,
MassiveCoreMConfColl.class,
MassiveCoreMSponsorInfoColl.class,
this.activateAuto();
// Nms
NmsBasics.class,
NmsBoard.class,
NmsChat.class,
NmsEntityDamageEvent.class,
NmsEntityGet.class,
NmsItemStackCreate.class,
NmsItemStackTooltip.class,
NmsPermissions.class,
NmsPlayerInventoryCreate.class,
NmsSkullMeta.class,
// These must be activated after nms
this.activate(
// Writer,
WriterItemStack.class,
// Engine
EngineMassiveCoreChestGui.class,
EngineMassiveCoreCollTick.class,
EngineMassiveCoreCommandRegistration.class,
EngineMassiveCoreCommandSet.class,
EngineMassiveCoreDatabase.class,
EngineMassiveCoreDestination.class,
EngineMassiveCoreGank.class,
EngineMassiveCoreLorePriority.class,
EngineMassiveCoreMain.class,
EngineMassiveCorePlayerLeave.class,
EngineMassiveCorePlayerState.class,
EngineMassiveCorePlayerUpdate.class,
EngineMassiveCoreScheduledTeleport.class,
EngineMassiveCoreTeleportMixinCause.class,
EngineMassiveCoreVariable.class,
EngineMassiveCoreWorldNameSet.class,
EngineMassiveCoreSponsor.class,
// Util
PlayerUtil.class,
BoardUtil.class,
// Integration
IntegrationVault.class,
IntegrationLiabilityAreaEffectCloud.class,
// Command
CmdMassiveCore.class,
CmdMassiveCoreUsys.class,
CmdMassiveCoreStore.class,
CmdMassiveCoreBuffer.class,
CmdMassiveCoreCmdurl.class,
CmdMassiveCoreClick.class,
// Mixin
MixinEvent.class, // NOTE: Should be first
MixinActionbar.class,
MixinActual.class,
MixinCommand.class,
MixinDisplayName.class,
MixinGamemode.class,
MixinInventory.class,
MixinKick.class,
MixinLog.class,
MixinMassiveCraftPremium.class,
MixinMessage.class,
MixinModification.class,
MixinPlayed.class,
MixinSenderPs.class,
MixinTeleport.class,
MixinTitle.class,
MixinVisibility.class,
MixinWorld.class,
// Test
TestTypeEnchantment.class
BoardUtil.class
);
// Start the examine threads
@ -368,6 +249,25 @@ public class MassiveCore extends MassivePlugin
Bukkit.getScheduler().scheduleSyncDelayedTask(this, MassiveCoreTaskDeleteFiles.get());
}
@Override
public List<Class<?>> getClassesActiveColls()
{
List<Class<?>> ret = this.getClassesActive(null, Coll.class);
return ret;
}
@Override
public List<Class<?>> getClassesActiveMixins()
{
List<Class<?>> ret = super.getClassesActiveMixins();
ret.remove(MixinEvent.class);
ret.add(0, MixinEvent.class);
return ret;
}
// -------------------------------------------- //
// DISABLE
// -------------------------------------------- //

View File

@ -143,6 +143,7 @@ public class MassiveCoreMConf extends Entity<MassiveCoreMConf>
// -------------------------------------------- //
public boolean debugWriters = false;
public boolean debugActives = false;
public boolean testsEnabled = false;
// -------------------------------------------- //

View File

@ -2,7 +2,13 @@ package com.massivecraft.massivecore;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateAnd;
import com.massivecraft.massivecore.predicate.PredicateIsClassSingleton;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.migrator.MigratorRoot;
import com.massivecraft.massivecore.test.Test;
import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.Gson;
@ -14,6 +20,8 @@ import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -201,21 +209,56 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
public void activate(Object... objects)
{
for (Object object : objects)
this.activateOne(objects);
}
// We must get one active at a time because initialisation of some
// can only happen after others have been initialised.
public void activateOne(Object object)
{
boolean debug = MassiveCoreMConf.get() != null && MassiveCoreMConf.get().debugActives;
// Try collection
if (object instanceof Iterable)
{
Active active = asActive(object);
if (active == null) continue;
active.setActive(this);
Iterable<?> elements = (Iterable) object;
for (Object element : elements)
{
this.activateOne(element);
}
return;
}
// Try array
if (object instanceof Object[])
{
Object[] array = (Object[]) object;
activateOne(Arrays.asList(array));
return;
}
Active active = asActive(object);
if (active == null) return;
if (active.isActive())
{
if (debug) log(Txt.parse("<h>%s <b>is already active.", active.getClass().getSimpleName()));
}
active.setActive(this);
if (debug) log(Txt.parse("<i>Activating <h>%s<i>.", active.getClass().getSimpleName()));
}
private static Active asActive(Object object)
{
// Active already
if (object instanceof Active)
{
return (Active)object;
return (Active) object;
}
// Try string as class name
if (object instanceof String)
{
String string = (String)object;
@ -230,6 +273,7 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
}
}
// Try class
if (object instanceof Class<?>)
{
Class<?> clazz = (Class<?>)object;
@ -242,6 +286,7 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
return active;
}
// No success
throw new IllegalArgumentException("Neither Active nor Class: " + object);
}
@ -267,9 +312,134 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
}
}
// -------------------------------------------- //
// ACTIVATE AUTO
// -------------------------------------------- //
public void activateAuto()
{
// And activate them
this.activate(getClassesActive());
}
public List<Class<?>> getClassesActive()
{
List<Class<?>> ret = new MassiveList<>();
// Fill with all kinds of Actives
ret.addAll(this.getClassesActiveMigrators());
ret.addAll(this.getClassesActiveColls());
ret.addAll(this.getClassesActiveNms());
ret.addAll(this.getClassesActiveCommands());
ret.addAll(this.getClassesActiveEngines());
ret.addAll(this.getClassesActiveIntegrations());
ret.addAll(this.getClassesActiveTasks());
ret.addAll(this.getClassesActiveMixins());
ret.addAll(this.getClassesActiveTests());
return ret;
}
public List<Class<?>> getClassesActiveColls()
{
return getClassesActive("entity", Coll.class);
}
public List<Class<?>> getClassesActiveNms()
{
return getClassesActive("nms", Mixin.class, new Predicate<Class<?>>()
{
@Override
public boolean apply(Class<?> clazz)
{
try
{
ReflectionUtil.getField(clazz, "d");
return true;
}
catch (Exception ex)
{
return false;
}
}
}
);
}
public List<Class<?>> getClassesActiveCommands()
{
return getClassesActive("cmd", MassiveCommand.class);
}
public List<Class<?>> getClassesActiveEngines()
{
return getClassesActive(Engine.class);
}
public List<Class<?>> getClassesActiveIntegrations()
{
return getClassesActive(Integration.class);
}
public List<Class<?>> getClassesActiveTasks()
{
return getClassesActive("task", ModuloRepeatTask.class);
}
public List<Class<?>> getClassesActiveMixins()
{
return getClassesActive(Mixin.class);
}
public List<Class<?>> getClassesActiveTests()
{
return getClassesActive(Test.class);
}
public List<Class<?>> getClassesActiveMigrators()
{
return getClassesActive("entity.migrator", MigratorRoot.class);
}
public List<Class<?>> getClassesActive(Class<? extends Active> superClass, Predicate<Class<?>>... predicates)
{
return getClassesActive(superClass.getSimpleName().toLowerCase(), superClass);
}
@SuppressWarnings("unchecked")
public List<Class<?>> getClassesActive(String packageName, final Class<?> superClass, Predicate<Class<?>>... predicates)
{
if (!Active.class.isAssignableFrom(superClass)) throw new IllegalArgumentException(superClass.getName() + " is not insatnce of Active.");
packageName = packageName == null ? "" : "." + packageName;
packageName = this.getClass().getPackage().getName() + packageName;
Predicate predicateCombined = PredicateAnd.get(predicates);
Predicate<Class<?>> predicateNotAbstract = new Predicate<Class<?>>()
{
@Override
public boolean apply(Class<?> type)
{
return !Modifier.isAbstract(type.getModifiers());
}
};
Predicate<Class<?>> predicateSubclass = new Predicate<Class<?>>()
{
@Override
public boolean apply(Class<?> type)
{
return superClass.isAssignableFrom(type);
}
};
Predicate<Class<?>> predicateSingleton = PredicateIsClassSingleton.get();
return ReflectionUtil.getPackageClasses(packageName, this.getClassLoader(), true, predicateCombined, predicateNotAbstract, predicateSubclass, predicateSingleton);
}
// -------------------------------------------- //
// LOGGING
// -------------------------------------------- //
private String logPrefixColored = null;
private String logPrefixPlain = null;
public void log(Object... msg)

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCoreMConf;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeString;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.engine.EngineMassiveCoreVariable;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.engine.EngineMassiveCoreVariable;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeString;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeInteger;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;
@ -125,3 +125,4 @@ public class CmdMassiveCoreCmdurl extends MassiveCoreCommand
}
}

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.SoundEffect;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.ConfServer;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.MassiveCorePerm;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCoreMConf;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.ConfServer;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.store.TypeColl;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCoreMConf;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
public class CmdMassiveCoreUsysAspect extends MassiveCoreCommand
{

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.Aspect;
import com.massivecraft.massivecore.AspectColl;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.Aspect;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.Aspect;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
public class CmdMassiveCoreUsysMultiverse extends MassiveCoreCommand
{

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.Multiverse;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.MultiverseColl;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.Aspect;
import com.massivecraft.massivecore.MassiveCore;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
public class CmdMassiveCoreUsysUniverse extends MassiveCoreCommand
{

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.Multiverse;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.Multiverse;

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.command.massivecore;
package com.massivecraft.massivecore.cmd;
import com.massivecraft.massivecore.MassiveCorePerm;
import com.massivecraft.massivecore.command.MassiveCommand;

View File

@ -40,6 +40,8 @@ public class CommandEditSingleton<O> extends CommandEditProperties<O, O>
@SuppressWarnings("unchecked")
private static <O> Type<O> getType(O object)
{
if (object == null) throw new NullPointerException("object");
// Get the return value
Type<O> ret = TypeSingleton.get(object);

View File

@ -93,7 +93,8 @@ public class Mixin extends Engine
}
catch (Throwable t)
{
throw ReflectionUtil.asRuntimeException(t);
t.printStackTrace();
//throw ReflectionUtil.asRuntimeException(t);
}
}

View File

@ -0,0 +1,24 @@
package com.massivecraft.massivecore.predicate;
import com.massivecraft.massivecore.util.ReflectionUtil;
public class PredicateIsClassSingleton implements Predicate<Class<?>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PredicateIsClassSingleton i = new PredicateIsClassSingleton();
public static PredicateIsClassSingleton get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(Class clazz)
{
return ReflectionUtil.isSingleton(clazz);
}
}

View File

@ -978,4 +978,3 @@ public final class PS implements Serializable, Comparable<PS>
}
}

View File

@ -9,6 +9,7 @@ import com.massivecraft.massivecore.util.Txt;
import java.util.List;
public abstract class Test extends Engine
{
// -------------------------------------------- //

View File

@ -1,8 +1,14 @@
package com.massivecraft.massivecore.util;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateAnd;
import org.bukkit.Bukkit;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
@ -12,6 +18,7 @@ import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ReflectionUtil
@ -244,6 +251,19 @@ public class ReflectionUtil
return fallback;
}
public static boolean isSingleton(Class<?> clazz)
{
try
{
Method get = getMethod(clazz, "get");
return true;
}
catch (Exception ex)
{
return false;
}
}
// -------------------------------------------- //
// ANNOTATION
// -------------------------------------------- //
@ -423,6 +443,60 @@ public class ReflectionUtil
});
}
// -------------------------------------------- //
// GET PACKAGE CLASSES
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public static List<Class<?>> getPackageClasses(String packageName, ClassLoader classLoader, boolean recursive, Predicate<Class<?>>... predicates)
{
// Create ret
List<Class<?>> ret = new MassiveList<>();
try
{
// Get info
ClassPath classPath = ClassPath.from(classLoader);
Predicate<Class<?>> predicateCombined = PredicateAnd.get(predicates);
Collection<ClassInfo> classes = recursive ? classPath.getTopLevelClassesRecursive(packageName) : classPath.getTopLevelClasses(packageName);
for (ClassInfo classInfo : classes)
{
// Get name of class
String className = classInfo.getName();
// Avoid versions created at runtime
// Apparently it found a "EngineMassiveCoreCollTick 3" which we don't want
if (className.contains(" ")) continue;
// Try and load it
Class<?> clazz;
try
{
clazz = classInfo.load();
}
catch (NoClassDefFoundError ex)
{
// This thing couldn't be loaded. Probably has to do with integrations.
// Just skip it
continue;
}
// And it must not be ignored
if (!predicateCombined.apply(clazz)) continue;
ret.add(clazz);
}
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
return ret;
}
// -------------------------------------------- //
// AS RUNTIME EXCEPTION
// -------------------------------------------- //

View File

@ -2,7 +2,7 @@ package com.massivecraft.massivecore.util;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.massivecore.CmdMassiveCore;
import com.massivecraft.massivecore.cmd.CmdMassiveCore;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.mson.MsonEvent;
import com.massivecraft.massivecore.predicate.Predicate;