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 commands; private final String mname; private final HashMap 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 getDataBank(Class 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 getCommand(Class 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 getEvent(Class c) { return (T) events.get(c); } }