MassiveCore - Lazy Active by Reflection. Solves unwanted early instance creation.
This commit is contained in:
parent
2087b10c79
commit
a4acf15094
@ -2,10 +2,17 @@ package com.massivecraft.massivecore;
|
||||
|
||||
public interface Active
|
||||
{
|
||||
// Boolean
|
||||
public boolean isActive();
|
||||
public void setActive(boolean active);
|
||||
|
||||
// Plugin
|
||||
public MassivePlugin setActivePlugin(MassivePlugin plugin);
|
||||
public MassivePlugin getActivePlugin();
|
||||
|
||||
// Combined Setter
|
||||
// Plugin is set first.
|
||||
// Boolean by null state.
|
||||
public void setActive(MassivePlugin plugin);
|
||||
|
||||
}
|
||||
|
@ -74,9 +74,9 @@ public abstract class Engine implements Active, Listener, Runnable
|
||||
}
|
||||
|
||||
@Override
|
||||
public MassivePlugin setActivePlugin(MassivePlugin activePlugin)
|
||||
public MassivePlugin setActivePlugin(MassivePlugin plugin)
|
||||
{
|
||||
this.setPluginSoft(activePlugin);
|
||||
this.setPluginSoft(plugin);
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -86,6 +86,13 @@ public abstract class Engine implements Active, Listener, Runnable
|
||||
return this.getPlugin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActive(MassivePlugin plugin)
|
||||
{
|
||||
this.setActivePlugin(plugin);
|
||||
this.setActive(plugin != null);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// ACTIVE > EVENTS
|
||||
// -------------------------------------------- //
|
||||
|
@ -205,62 +205,59 @@ public class MassiveCore extends MassivePlugin
|
||||
// Activate
|
||||
this.activate(
|
||||
// Coll
|
||||
MultiverseColl.get(),
|
||||
AspectColl.get(),
|
||||
MassiveCoreMConfColl.get(),
|
||||
MassiveCoreMSponsorInfoColl.get(),
|
||||
MultiverseColl.class,
|
||||
AspectColl.class,
|
||||
MassiveCoreMConfColl.class,
|
||||
MassiveCoreMSponsorInfoColl.class,
|
||||
|
||||
// Engine
|
||||
EngineMassiveCoreChestGui.get(),
|
||||
EngineMassiveCoreCollTick.get(),
|
||||
EngineMassiveCoreCommandRegistration.get(),
|
||||
EngineMassiveCoreDatabase.get(),
|
||||
EngineMassiveCoreDestination.get(),
|
||||
EngineMassiveCoreGank.get(),
|
||||
EngineMassiveCoreMain.get(),
|
||||
EngineMassiveCorePlayerLeave.get(),
|
||||
EngineMassiveCorePlayerState.get(),
|
||||
EngineMassiveCorePlayerUpdate.get(),
|
||||
EngineMassiveCoreScheduledTeleport.get(),
|
||||
EngineMassiveCoreTeleportMixinCause.get(),
|
||||
EngineMassiveCoreVariable.get(),
|
||||
EngineMassiveCoreWorldNameSet.get(),
|
||||
EngineMassiveCoreSponsor.get(),
|
||||
EngineMassiveCoreChestGui.class,
|
||||
EngineMassiveCoreCollTick.class,
|
||||
EngineMassiveCoreCommandRegistration.class,
|
||||
EngineMassiveCoreDatabase.class,
|
||||
EngineMassiveCoreDestination.class,
|
||||
EngineMassiveCoreGank.class,
|
||||
EngineMassiveCoreMain.class,
|
||||
EngineMassiveCorePlayerLeave.class,
|
||||
EngineMassiveCorePlayerState.class,
|
||||
EngineMassiveCorePlayerUpdate.class,
|
||||
EngineMassiveCoreScheduledTeleport.class,
|
||||
EngineMassiveCoreTeleportMixinCause.class,
|
||||
EngineMassiveCoreVariable.class,
|
||||
EngineMassiveCoreWorldNameSet.class,
|
||||
EngineMassiveCoreSponsor.class,
|
||||
|
||||
// Util
|
||||
PlayerUtil.get(),
|
||||
PlayerUtil.class,
|
||||
|
||||
// Integration
|
||||
IntegrationVault.get()
|
||||
);
|
||||
IntegrationVault.class,
|
||||
|
||||
// NOTE: Some commands rely on the MConf being loaded at construction.
|
||||
this.activate(
|
||||
// Command
|
||||
CmdMassiveCore.get(),
|
||||
CmdMassiveCoreUsys.get(),
|
||||
CmdMassiveCoreStore.get(),
|
||||
CmdMassiveCoreBuffer.get(),
|
||||
CmdMassiveCoreCmdurl.get(),
|
||||
CmdMassiveCoreClick.get(),
|
||||
CmdMassiveCore.class,
|
||||
CmdMassiveCoreUsys.class,
|
||||
CmdMassiveCoreStore.class,
|
||||
CmdMassiveCoreBuffer.class,
|
||||
CmdMassiveCoreCmdurl.class,
|
||||
CmdMassiveCoreClick.class,
|
||||
|
||||
// Mixin
|
||||
MixinEvent.get(), // NOTE: Should be first
|
||||
MixinActionbar.get(),
|
||||
MixinActual.get(),
|
||||
MixinCommand.get(),
|
||||
MixinDisplayName.get(),
|
||||
MixinGamemode.get(),
|
||||
MixinInventory.get(),
|
||||
MixinKick.get(),
|
||||
MixinMessage.get(),
|
||||
MixinModification.get(),
|
||||
MixinPlayed.get(),
|
||||
MixinSenderPs.get(),
|
||||
MixinTeleport.get(),
|
||||
MixinTitle.get(),
|
||||
MixinVisibility.get(),
|
||||
MixinWorld.get()
|
||||
MixinEvent.class, // NOTE: Should be first
|
||||
MixinActionbar.class,
|
||||
MixinActual.class,
|
||||
MixinCommand.class,
|
||||
MixinDisplayName.class,
|
||||
MixinGamemode.class,
|
||||
MixinInventory.class,
|
||||
MixinKick.class,
|
||||
MixinMessage.class,
|
||||
MixinModification.class,
|
||||
MixinPlayed.class,
|
||||
MixinSenderPs.class,
|
||||
MixinTeleport.class,
|
||||
MixinTitle.class,
|
||||
MixinVisibility.class,
|
||||
MixinWorld.class
|
||||
);
|
||||
|
||||
// Start the examine threads
|
||||
|
@ -12,6 +12,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.massivecraft.massivecore.command.MassiveCommand;
|
||||
import com.massivecraft.massivecore.store.Coll;
|
||||
import com.massivecraft.massivecore.util.ReflectionUtil;
|
||||
import com.massivecraft.massivecore.util.Txt;
|
||||
import com.massivecraft.massivecore.xlib.gson.Gson;
|
||||
import com.massivecraft.massivecore.xlib.gson.GsonBuilder;
|
||||
@ -170,15 +171,37 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
}
|
||||
|
||||
public void activate(Active... actives)
|
||||
public void activate(Object... objects)
|
||||
{
|
||||
for (Active active : actives)
|
||||
for (Object object : objects)
|
||||
{
|
||||
active.setActivePlugin(this);
|
||||
active.setActive(true);
|
||||
Active active = asActive(object);
|
||||
active.setActive(this);
|
||||
}
|
||||
}
|
||||
|
||||
private static Active asActive(Object object)
|
||||
{
|
||||
if (object instanceof Active)
|
||||
{
|
||||
return (Active)object;
|
||||
}
|
||||
|
||||
if (object instanceof Class<?>)
|
||||
{
|
||||
Class<?> clazz = (Class<?>)object;
|
||||
if ( ! Active.class.isAssignableFrom(clazz)) throw new IllegalArgumentException("Not Active Class: " + (clazz == null ? "NULL" : clazz));
|
||||
|
||||
Object instance = ReflectionUtil.getSingletonInstance(clazz);
|
||||
if ( ! (instance instanceof Active)) throw new IllegalArgumentException("Not Active Instance: " + (instance == null ? "NULL" : instance) + " for object: " + (object == null ? "NULL" : object));
|
||||
|
||||
Active active = (Active)instance;
|
||||
return active;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Neither Active nor Class: " + object);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// LOGGING
|
||||
// -------------------------------------------- //
|
||||
|
@ -85,6 +85,7 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
|
||||
// NOTE: Not Implemented
|
||||
}
|
||||
|
||||
@Override
|
||||
public MassivePlugin setActivePlugin(MassivePlugin activePlugin)
|
||||
{
|
||||
if (activePlugin == null)
|
||||
@ -97,11 +98,19 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MassivePlugin getActivePlugin()
|
||||
{
|
||||
return registry.get(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActive(MassivePlugin plugin)
|
||||
{
|
||||
this.setActivePlugin(plugin);
|
||||
this.setActive(plugin != null);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// PLUGIN IDENTIFIABLE COMMAND
|
||||
// -------------------------------------------- //
|
||||
|
@ -1034,6 +1034,12 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
|
||||
return name2instance.containsKey(this.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActive(MassivePlugin plugin)
|
||||
{
|
||||
this.setActivePlugin(plugin);
|
||||
this.setActive(plugin != null);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// NAME UTILITIES
|
||||
|
@ -73,6 +73,13 @@ public abstract class Colls<C extends Coll<E>, E extends Entity<E>> implements A
|
||||
return this.plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActive(MassivePlugin plugin)
|
||||
{
|
||||
this.setActivePlugin(plugin);
|
||||
this.setActive(plugin != null);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// UTIL
|
||||
// -------------------------------------------- //
|
||||
|
@ -122,6 +122,19 @@ public class ReflectionUtil
|
||||
return (T) invokeMethod(method, target, EMPTY_ARRAY_OF_OBJECT);
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// SINGLETON INSTANCE
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static <T> T getSingletonInstance(Class<T> clazz)
|
||||
{
|
||||
Method get = getMethod(clazz, "get");
|
||||
T ret = invokeMethod(get, null);
|
||||
if (ret == null) throw new NullPointerException("Singleton instance was null for: " + clazz);
|
||||
if ( ! clazz.isAssignableFrom(ret.getClass())) throw new IllegalStateException("Singleton instance was not of same or subclass for: " + clazz);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// FIELD > GET
|
||||
// -------------------------------------------- //
|
||||
|
Loading…
Reference in New Issue
Block a user