123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package me.km.api;
- import java.lang.reflect.InvocationTargetException;
- import java.util.HashMap;
- import me.km.PackageHelper;
- import me.km.databank.SimpleDataBank;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraftforge.common.MinecraftForge;
- import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
- import net.minecraftforge.fml.common.eventhandler.EventBus;
- public class Module extends MessageSender
- {
- private final HashMap<String, ModuleCommand> commands;
- private final String mname;
- private final HashMap<Class, ModuleListener> events;
- private SimpleDataBank bank;
-
- public Module(String mname, String prefix, TextFormatting color)
- {
- super(prefix, color);
- commands = new HashMap<>();
- this.mname = mname;
- events = new HashMap<>();
- bank = null;
-
- super.sendToConsole("Das Modul " + color + mname + TextFormatting.RESET + " wurde geladen.");
- }
-
- //--------------------------------------------------------------------------
- // Allgemeine Rückgaben
- //--------------------------------------------------------------------------
-
- public String getName()
- {
- return mname;
- }
-
- //--------------------------------------------------------------------------
- // DataBank
- //--------------------------------------------------------------------------
-
- public void setDataBank(SimpleDataBank bank)
- {
- this.bank = bank;
- this.sendToConsole("Die Datenbank wurde geladen.");
- }
-
- @SuppressWarnings("unchecked")
- public <T extends SimpleDataBank> T getDataBank(Class<T> c)
- {
- return (T) bank;
- }
-
- //--------------------------------------------------------------------------
- // Command-Methoden
- //--------------------------------------------------------------------------
-
- public void registerCommand(FMLServerStartingEvent e, ModuleCommand command)
- {
- e.registerServerCommand(command);
- if(commands.put(command.getName(), command) != null)
- {
- this.sendToConsole("Der Command '" + command.getName() + "' wurde ein weiteres Mal geladen.");
- }
- }
-
- @SuppressWarnings("unchecked")
- public void registerCommands(FMLServerStartingEvent e, String packageName)
- {
- PackageHelper.getClasses(packageName).stream().filter(c -> ModuleCommand.class.isAssignableFrom(c)).forEach((Class c) ->
- {
- try
- {
- ModuleCommand command = (ModuleCommand) c.getDeclaredConstructor(Module.class).newInstance(this);
- registerCommand(e, command);
- }
- catch(ClassCastException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
- {
- this.sendToConsole("Die Klasse '" + c + "' wurde nicht geladen.");
- }
- });
- this.sendToConsole("Alle Commands wurden geladen.");
- }
-
- public ModuleCommand getCommand(String name)
- {
- return commands.get(name);
- }
-
- @SuppressWarnings("unchecked")
- public <T extends ModuleCommand> T getCommand(Class<T> c)
- {
- return (T) commands.get(c.getSimpleName().substring(7).toLowerCase());
- }
-
- //--------------------------------------------------------------------------
- // Event-Methoden
- //--------------------------------------------------------------------------
-
- @SuppressWarnings("unchecked")
- public void registerEvents(String packageName)
- {
- EventBus bus = MinecraftForge.EVENT_BUS;
- PackageHelper.getClasses(packageName).stream().filter(c -> ModuleListener.class.isAssignableFrom(c)).forEach((Class c) ->
- {
- try
- {
- ModuleListener l = (ModuleListener) c.getDeclaredConstructor(Module.class).newInstance(this);
- events.put(c, l);
- bus.register(l);
- }
- catch(NoSuchMethodException | ClassCastException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
- {
- this.sendToConsole("Fehler bei " + c);
- this.sendToConsole(ex.toString());
- }
- });
- this.sendToConsole("Alle Events wurden geladen.");
- }
-
- @SuppressWarnings("unchecked")
- public <T extends ModuleListener> T getEvent(Class<T> c)
- {
- return (T) events.get(c);
- }
- }
|