mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-16 20:19:13 +03:00
79 lines
2.1 KiB
Java
79 lines
2.1 KiB
Java
package pro.gravit.launcher.modules;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public abstract class LauncherModule {
|
|
private LauncherModulesContext context;
|
|
|
|
private Map<Class<? extends Event>, EventHandler> eventMap = new HashMap<>();
|
|
protected LauncherModulesManager modulesManager;
|
|
protected final LauncherModuleInfo moduleInfo;
|
|
protected ModulesConfigManager modulesConfigManager;
|
|
InitPhase initPhase = InitPhase.CREATED;
|
|
|
|
protected LauncherModule() {
|
|
moduleInfo = new LauncherModuleInfo("UnknownModule");
|
|
}
|
|
|
|
public enum InitPhase
|
|
{
|
|
CREATED,
|
|
INIT,
|
|
FINISH
|
|
}
|
|
@FunctionalInterface
|
|
public interface EventHandler<T extends Event>
|
|
{
|
|
void event(T e) throws Exception;
|
|
}
|
|
public static class Event
|
|
{
|
|
public boolean isCancel() {
|
|
return cancel;
|
|
}
|
|
|
|
public Event cancel() {
|
|
this.cancel = true;
|
|
return this;
|
|
}
|
|
|
|
protected boolean cancel;
|
|
}
|
|
|
|
public InitPhase getInitPhase() {
|
|
return initPhase;
|
|
}
|
|
|
|
Map<Class<? extends Event>, EventHandler> 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();
|
|
return eventMap;
|
|
}
|
|
|
|
public abstract void init();
|
|
|
|
<T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
|
|
{
|
|
eventMap.put(tClass, handle);
|
|
return true;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
<T extends Event> void callEvent(T event) throws Exception
|
|
{
|
|
Class<? extends Event> tClass = event.getClass();
|
|
for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet())
|
|
{
|
|
|
|
if(e.getKey().isAssignableFrom(tClass))
|
|
{
|
|
e.getValue().event(event);
|
|
if(event.isCancel()) return;
|
|
}
|
|
}
|
|
}
|
|
}
|