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
|
public interface Active
|
||||||
{
|
{
|
||||||
|
// Boolean
|
||||||
public boolean isActive();
|
public boolean isActive();
|
||||||
public void setActive(boolean active);
|
public void setActive(boolean active);
|
||||||
|
|
||||||
|
// Plugin
|
||||||
public MassivePlugin setActivePlugin(MassivePlugin plugin);
|
public MassivePlugin setActivePlugin(MassivePlugin plugin);
|
||||||
public MassivePlugin getActivePlugin();
|
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
|
@Override
|
||||||
public MassivePlugin setActivePlugin(MassivePlugin activePlugin)
|
public MassivePlugin setActivePlugin(MassivePlugin plugin)
|
||||||
{
|
{
|
||||||
this.setPluginSoft(activePlugin);
|
this.setPluginSoft(plugin);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,6 +86,13 @@ public abstract class Engine implements Active, Listener, Runnable
|
|||||||
return this.getPlugin();
|
return this.getPlugin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setActive(MassivePlugin plugin)
|
||||||
|
{
|
||||||
|
this.setActivePlugin(plugin);
|
||||||
|
this.setActive(plugin != null);
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// ACTIVE > EVENTS
|
// ACTIVE > EVENTS
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
@ -205,62 +205,59 @@ public class MassiveCore extends MassivePlugin
|
|||||||
// Activate
|
// Activate
|
||||||
this.activate(
|
this.activate(
|
||||||
// Coll
|
// Coll
|
||||||
MultiverseColl.get(),
|
MultiverseColl.class,
|
||||||
AspectColl.get(),
|
AspectColl.class,
|
||||||
MassiveCoreMConfColl.get(),
|
MassiveCoreMConfColl.class,
|
||||||
MassiveCoreMSponsorInfoColl.get(),
|
MassiveCoreMSponsorInfoColl.class,
|
||||||
|
|
||||||
// Engine
|
// Engine
|
||||||
EngineMassiveCoreChestGui.get(),
|
EngineMassiveCoreChestGui.class,
|
||||||
EngineMassiveCoreCollTick.get(),
|
EngineMassiveCoreCollTick.class,
|
||||||
EngineMassiveCoreCommandRegistration.get(),
|
EngineMassiveCoreCommandRegistration.class,
|
||||||
EngineMassiveCoreDatabase.get(),
|
EngineMassiveCoreDatabase.class,
|
||||||
EngineMassiveCoreDestination.get(),
|
EngineMassiveCoreDestination.class,
|
||||||
EngineMassiveCoreGank.get(),
|
EngineMassiveCoreGank.class,
|
||||||
EngineMassiveCoreMain.get(),
|
EngineMassiveCoreMain.class,
|
||||||
EngineMassiveCorePlayerLeave.get(),
|
EngineMassiveCorePlayerLeave.class,
|
||||||
EngineMassiveCorePlayerState.get(),
|
EngineMassiveCorePlayerState.class,
|
||||||
EngineMassiveCorePlayerUpdate.get(),
|
EngineMassiveCorePlayerUpdate.class,
|
||||||
EngineMassiveCoreScheduledTeleport.get(),
|
EngineMassiveCoreScheduledTeleport.class,
|
||||||
EngineMassiveCoreTeleportMixinCause.get(),
|
EngineMassiveCoreTeleportMixinCause.class,
|
||||||
EngineMassiveCoreVariable.get(),
|
EngineMassiveCoreVariable.class,
|
||||||
EngineMassiveCoreWorldNameSet.get(),
|
EngineMassiveCoreWorldNameSet.class,
|
||||||
EngineMassiveCoreSponsor.get(),
|
EngineMassiveCoreSponsor.class,
|
||||||
|
|
||||||
// Util
|
// Util
|
||||||
PlayerUtil.get(),
|
PlayerUtil.class,
|
||||||
|
|
||||||
// Integration
|
// Integration
|
||||||
IntegrationVault.get()
|
IntegrationVault.class,
|
||||||
);
|
|
||||||
|
|
||||||
// NOTE: Some commands rely on the MConf being loaded at construction.
|
|
||||||
this.activate(
|
|
||||||
// Command
|
// Command
|
||||||
CmdMassiveCore.get(),
|
CmdMassiveCore.class,
|
||||||
CmdMassiveCoreUsys.get(),
|
CmdMassiveCoreUsys.class,
|
||||||
CmdMassiveCoreStore.get(),
|
CmdMassiveCoreStore.class,
|
||||||
CmdMassiveCoreBuffer.get(),
|
CmdMassiveCoreBuffer.class,
|
||||||
CmdMassiveCoreCmdurl.get(),
|
CmdMassiveCoreCmdurl.class,
|
||||||
CmdMassiveCoreClick.get(),
|
CmdMassiveCoreClick.class,
|
||||||
|
|
||||||
// Mixin
|
// Mixin
|
||||||
MixinEvent.get(), // NOTE: Should be first
|
MixinEvent.class, // NOTE: Should be first
|
||||||
MixinActionbar.get(),
|
MixinActionbar.class,
|
||||||
MixinActual.get(),
|
MixinActual.class,
|
||||||
MixinCommand.get(),
|
MixinCommand.class,
|
||||||
MixinDisplayName.get(),
|
MixinDisplayName.class,
|
||||||
MixinGamemode.get(),
|
MixinGamemode.class,
|
||||||
MixinInventory.get(),
|
MixinInventory.class,
|
||||||
MixinKick.get(),
|
MixinKick.class,
|
||||||
MixinMessage.get(),
|
MixinMessage.class,
|
||||||
MixinModification.get(),
|
MixinModification.class,
|
||||||
MixinPlayed.get(),
|
MixinPlayed.class,
|
||||||
MixinSenderPs.get(),
|
MixinSenderPs.class,
|
||||||
MixinTeleport.get(),
|
MixinTeleport.class,
|
||||||
MixinTitle.get(),
|
MixinTitle.class,
|
||||||
MixinVisibility.get(),
|
MixinVisibility.class,
|
||||||
MixinWorld.get()
|
MixinWorld.class
|
||||||
);
|
);
|
||||||
|
|
||||||
// Start the examine threads
|
// Start the examine threads
|
||||||
|
@ -12,6 +12,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
|
|
||||||
import com.massivecraft.massivecore.command.MassiveCommand;
|
import com.massivecraft.massivecore.command.MassiveCommand;
|
||||||
import com.massivecraft.massivecore.store.Coll;
|
import com.massivecraft.massivecore.store.Coll;
|
||||||
|
import com.massivecraft.massivecore.util.ReflectionUtil;
|
||||||
import com.massivecraft.massivecore.util.Txt;
|
import com.massivecraft.massivecore.util.Txt;
|
||||||
import com.massivecraft.massivecore.xlib.gson.Gson;
|
import com.massivecraft.massivecore.xlib.gson.Gson;
|
||||||
import com.massivecraft.massivecore.xlib.gson.GsonBuilder;
|
import com.massivecraft.massivecore.xlib.gson.GsonBuilder;
|
||||||
@ -170,15 +171,37 @@ public abstract class MassivePlugin extends JavaPlugin implements Listener, Name
|
|||||||
Bukkit.getPluginManager().disablePlugin(this);
|
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 active = asActive(object);
|
||||||
active.setActive(true);
|
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
|
// LOGGING
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
@ -85,6 +85,7 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
|
|||||||
// NOTE: Not Implemented
|
// NOTE: Not Implemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public MassivePlugin setActivePlugin(MassivePlugin activePlugin)
|
public MassivePlugin setActivePlugin(MassivePlugin activePlugin)
|
||||||
{
|
{
|
||||||
if (activePlugin == null)
|
if (activePlugin == null)
|
||||||
@ -97,11 +98,19 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public MassivePlugin getActivePlugin()
|
public MassivePlugin getActivePlugin()
|
||||||
{
|
{
|
||||||
return registry.get(this);
|
return registry.get(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setActive(MassivePlugin plugin)
|
||||||
|
{
|
||||||
|
this.setActivePlugin(plugin);
|
||||||
|
this.setActive(plugin != null);
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// PLUGIN IDENTIFIABLE COMMAND
|
// PLUGIN IDENTIFIABLE COMMAND
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
@ -1034,6 +1034,12 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
|
|||||||
return name2instance.containsKey(this.getName());
|
return name2instance.containsKey(this.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setActive(MassivePlugin plugin)
|
||||||
|
{
|
||||||
|
this.setActivePlugin(plugin);
|
||||||
|
this.setActive(plugin != null);
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// NAME UTILITIES
|
// NAME UTILITIES
|
||||||
|
@ -73,6 +73,13 @@ public abstract class Colls<C extends Coll<E>, E extends Entity<E>> implements A
|
|||||||
return this.plugin;
|
return this.plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setActive(MassivePlugin plugin)
|
||||||
|
{
|
||||||
|
this.setActivePlugin(plugin);
|
||||||
|
this.setActive(plugin != null);
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// UTIL
|
// UTIL
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
@ -122,6 +122,19 @@ public class ReflectionUtil
|
|||||||
return (T) invokeMethod(method, target, EMPTY_ARRAY_OF_OBJECT);
|
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
|
// FIELD > GET
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
Loading…
Reference in New Issue
Block a user