2019-08-26 09:43:53 +03:00
|
|
|
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;
|
2019-08-26 10:42:49 +03:00
|
|
|
protected final LauncherModuleInfo moduleInfo;
|
2019-08-26 09:43:53 +03:00
|
|
|
protected ModulesConfigManager modulesConfigManager;
|
2019-08-26 12:27:30 +03:00
|
|
|
protected InitPhase initPhase = InitPhase.CREATED;
|
2019-08-26 10:42:49 +03:00
|
|
|
|
|
|
|
protected LauncherModule() {
|
|
|
|
moduleInfo = new LauncherModuleInfo("UnknownModule");
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
public LauncherModuleInfo getModuleInfo() {
|
|
|
|
return moduleInfo;
|
|
|
|
}
|
|
|
|
|
2019-08-26 09:43:53 +03:00
|
|
|
public enum InitPhase
|
|
|
|
{
|
|
|
|
CREATED,
|
|
|
|
INIT,
|
|
|
|
FINISH
|
|
|
|
}
|
|
|
|
@FunctionalInterface
|
|
|
|
public interface EventHandler<T extends Event>
|
|
|
|
{
|
2019-08-26 10:42:49 +03:00
|
|
|
void event(T e) throws Exception;
|
2019-08-26 09:43:53 +03:00
|
|
|
}
|
2019-08-26 10:42:49 +03:00
|
|
|
public static class Event
|
2019-08-26 09:43:53 +03:00
|
|
|
{
|
2019-08-26 10:42:49 +03:00
|
|
|
public boolean isCancel() {
|
|
|
|
return cancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Event cancel() {
|
|
|
|
this.cancel = true;
|
|
|
|
return this;
|
|
|
|
}
|
2019-08-26 09:43:53 +03:00
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
protected boolean cancel = false;
|
2019-08-26 09:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public InitPhase getInitPhase() {
|
|
|
|
return initPhase;
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
public LauncherModule setInitPhase(InitPhase initPhase) {
|
|
|
|
this.initPhase = initPhase;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setContext(LauncherModulesContext context)
|
2019-08-26 09:43:53 +03:00
|
|
|
{
|
|
|
|
if(this.context != null) throw new IllegalStateException("Module already set context");
|
|
|
|
this.context = context;
|
|
|
|
this.modulesManager = context.getModulesManager();
|
|
|
|
this.modulesConfigManager = context.getModulesConfigManager();
|
2019-08-26 12:27:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void preInit() {
|
|
|
|
//NOP
|
2019-08-26 09:43:53 +03:00
|
|
|
}
|
2019-08-26 10:42:49 +03:00
|
|
|
|
|
|
|
public abstract void init();
|
2019-08-26 09:43:53 +03:00
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
|
|
|
|
protected <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
|
2019-08-26 09:43:53 +03:00
|
|
|
{
|
|
|
|
eventMap.put(tClass, handle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:42:49 +03:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-08-26 12:27:30 +03:00
|
|
|
public final <T extends Event> void callEvent(T event) throws Exception
|
2019-08-26 09:43:53 +03:00
|
|
|
{
|
|
|
|
Class<? extends Event> tClass = event.getClass();
|
|
|
|
for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet())
|
|
|
|
{
|
|
|
|
|
|
|
|
if(e.getKey().isAssignableFrom(tClass))
|
|
|
|
{
|
2019-08-26 10:42:49 +03:00
|
|
|
e.getValue().event(event);
|
|
|
|
if(event.isCancel()) return;
|
2019-08-26 09:43:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|