Plugins event handling through IWithEventHandlers.
parent
72d4a3b54e
commit
0088bc59c2
@ -0,0 +1,105 @@
|
|||||||
|
package net.pingex.dcf.modularity.events;
|
||||||
|
|
||||||
|
import net.pingex.dcf.core.GatewayConnectionsManager;
|
||||||
|
import net.pingex.dcf.modularity.PluginWrapper;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import sx.blah.discord.api.IDiscordClient;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all foreign event handlers for D4J.
|
||||||
|
*/
|
||||||
|
public class EventManager
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Singleton instance.
|
||||||
|
*/
|
||||||
|
private static EventManager ourInstance = new EventManager();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger
|
||||||
|
*/
|
||||||
|
private static Logger LOGGER = LogManager.getLogger(EventManager.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current instance
|
||||||
|
* @return The unique instance.
|
||||||
|
*/
|
||||||
|
public static EventManager getInstance()
|
||||||
|
{
|
||||||
|
return ourInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all plugins which implements IWithEvents
|
||||||
|
*/
|
||||||
|
private Set<PluginWrapper> handlers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enforce singleton.
|
||||||
|
*/
|
||||||
|
private EventManager()
|
||||||
|
{
|
||||||
|
handlers = new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that the plugin has event handlers and register it to active handlers pool.
|
||||||
|
* @param plugin Target plugin.
|
||||||
|
*/
|
||||||
|
public void checkAndRegister(PluginWrapper plugin)
|
||||||
|
{
|
||||||
|
LOGGER.trace("Checking {} for event handlers.", plugin.getId());
|
||||||
|
|
||||||
|
if(IWithEventHandlers.class.isAssignableFrom(plugin.getPluginClass()))
|
||||||
|
{
|
||||||
|
LOGGER.debug("Registering event handlers for plugin {}.", plugin.getId());
|
||||||
|
handlers.add(plugin);
|
||||||
|
updateConnectionsHandlers(); // Trigger update
|
||||||
|
}
|
||||||
|
else LOGGER.trace("Plugin {} has no event handlers.", plugin.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregister plugin from active handlers pool.
|
||||||
|
* @param plugin Target plugin.
|
||||||
|
*/
|
||||||
|
public void unregister(PluginWrapper plugin)
|
||||||
|
{
|
||||||
|
if(handlers.contains(plugin))
|
||||||
|
{
|
||||||
|
LOGGER.debug("Unregistering event handlers for plugin {}.", plugin.getId());
|
||||||
|
handlers.remove(plugin);
|
||||||
|
updateConnectionsHandlers(); // Trigger update
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect all event handlers into a single set
|
||||||
|
* @return Final set
|
||||||
|
*/
|
||||||
|
public Set<Class<?>> collectAllHandlers()
|
||||||
|
{
|
||||||
|
Set<Class<?>> toReturn = new HashSet<>();
|
||||||
|
handlers.forEach(pluginWrapper -> toReturn.addAll(((IWithEventHandlers) pluginWrapper.getInstance()).getEventHandlers()));
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delegated method
|
||||||
|
*/
|
||||||
|
public void updateConnectionsHandlers()
|
||||||
|
{
|
||||||
|
GatewayConnectionsManager.getInstance().updateAllListeners(collectAllHandlers());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delegated method
|
||||||
|
*/
|
||||||
|
public void updateConnectionHandlers(IDiscordClient target)
|
||||||
|
{
|
||||||
|
GatewayConnectionsManager.getInstance().updateListeners(target, collectAllHandlers());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package net.pingex.dcf.modularity.events;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface which indicates that the plugin has event handlers.
|
||||||
|
*/
|
||||||
|
public interface IWithEventHandlers
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Gives all event handlers classes for the plugin.
|
||||||
|
* @return All events handlers to submit to D4J.
|
||||||
|
*/
|
||||||
|
Set<Class<?>> getEventHandlers();
|
||||||
|
}
|
Reference in New Issue