package pro.gravit.launcher.modules; import java.util.HashMap; import java.util.Map; public abstract class LauncherModule { private LauncherModulesContext context; private Map, EventHandler> eventMap = new HashMap<>(); protected LauncherModulesManager modulesManager; protected final LauncherModuleInfo moduleInfo; protected ModulesConfigManager modulesConfigManager; protected InitStatus initStatus = InitStatus.CREATED; protected LauncherModule() { moduleInfo = new LauncherModuleInfo("UnknownModule"); } protected LauncherModule(LauncherModuleInfo info) { moduleInfo = info; } public LauncherModuleInfo getModuleInfo() { return moduleInfo; } /** * Module initialization status at the current time * CREATED - Module status immediately after loading * INIT - The state of the module during the execution of the method init() * FINISH - Status of the module after initialization */ public enum InitStatus { CREATED, INIT, FINISH } @FunctionalInterface public interface EventHandler { void event(T e); } public static class Event { public boolean isCancel() { return cancel; } public Event cancel() { this.cancel = true; return this; } protected boolean cancel = false; } public InitStatus getInitStatus() { return initStatus; } public LauncherModule setInitStatus(InitStatus initStatus) { this.initStatus = initStatus; return this; } /** * The internal method used by the ModuleManager * DO NOT TOUCH * @param context Private context */ public void setContext(LauncherModulesContext context) { if(this.context != null) throw new IllegalStateException("Module already set context"); this.context = context; this.modulesManager = context.getModulesManager(); this.modulesConfigManager = context.getModulesConfigManager(); } /** * This method is called before initializing all modules and resolving dependencies. * You can: * Use to Module Manager * Add custom modules not described in the manifest * Change information about your module or modules you control * You can not: * Use your dependencies * Use API Launcher, LaunchServer, ServerWrapper * Change the names of any modules */ public void preInit() { //NOP } /** * Basic module initialization method * You can: * Subscribe to events * Use your dependencies * Use provided initContext * Receive modules and access the module’s internal methods * You can not: * Modify module description, dependencies * Add modules * Read configuration * @param initContext null on module initialization during boot or startup * Not null during module initialization while running */ public abstract void init(LauncherInitContext initContext); /** * Registers an event handler for the current module * @param handle your event handler * @param tClass event class * @param event type * @return true if adding a handler was successful */ protected boolean registerEvent(EventHandler handle, Class tClass) { eventMap.put(tClass, handle); return true; } /** * Call the handler of the current module * @param event event handled * @param event type */ @SuppressWarnings("unchecked") public final void callEvent(T event) { Class tClass = event.getClass(); for(Map.Entry, EventHandler> e : eventMap.entrySet()) { if(e.getKey().isAssignableFrom(tClass)) { e.getValue().event(event); if(event.isCancel()) return; } } } }