Module.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package me.km.api;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.util.HashMap;
  4. import me.km.PackageHelper;
  5. import me.km.databank.SimpleDataBank;
  6. import net.minecraft.util.text.TextFormatting;
  7. import net.minecraftforge.common.MinecraftForge;
  8. import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
  9. import net.minecraftforge.fml.common.eventhandler.EventBus;
  10. public class Module extends MessageSender
  11. {
  12. private final HashMap<String, ModuleCommand> commands;
  13. private final String mname;
  14. private final HashMap<Class, ModuleListener> events;
  15. private SimpleDataBank bank;
  16. public Module(String mname, String prefix, TextFormatting color)
  17. {
  18. super(prefix, color);
  19. commands = new HashMap<>();
  20. this.mname = mname;
  21. events = new HashMap<>();
  22. bank = null;
  23. super.sendToConsole("Das Modul " + color + mname + TextFormatting.RESET + " wurde geladen.");
  24. }
  25. //--------------------------------------------------------------------------
  26. // Allgemeine Rückgaben
  27. //--------------------------------------------------------------------------
  28. public String getName()
  29. {
  30. return mname;
  31. }
  32. //--------------------------------------------------------------------------
  33. // DataBank
  34. //--------------------------------------------------------------------------
  35. public void setDataBank(SimpleDataBank bank)
  36. {
  37. this.bank = bank;
  38. this.sendToConsole("Die Datenbank wurde geladen.");
  39. }
  40. @SuppressWarnings("unchecked")
  41. public <T extends SimpleDataBank> T getDataBank(Class<T> c)
  42. {
  43. return (T) bank;
  44. }
  45. //--------------------------------------------------------------------------
  46. // Command-Methoden
  47. //--------------------------------------------------------------------------
  48. public void registerCommand(FMLServerStartingEvent e, ModuleCommand command)
  49. {
  50. e.registerServerCommand(command);
  51. if(commands.put(command.getName(), command) != null)
  52. {
  53. this.sendToConsole("Der Command '" + command.getName() + "' wurde ein weiteres Mal geladen.");
  54. }
  55. }
  56. @SuppressWarnings("unchecked")
  57. public void registerCommands(FMLServerStartingEvent e, String packageName)
  58. {
  59. PackageHelper.getClasses(packageName).stream().filter(c -> ModuleCommand.class.isAssignableFrom(c)).forEach((Class c) ->
  60. {
  61. try
  62. {
  63. ModuleCommand command = (ModuleCommand) c.getDeclaredConstructor(Module.class).newInstance(this);
  64. registerCommand(e, command);
  65. }
  66. catch(ClassCastException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
  67. {
  68. this.sendToConsole("Die Klasse '" + c + "' wurde nicht geladen.");
  69. }
  70. });
  71. this.sendToConsole("Alle Commands wurden geladen.");
  72. }
  73. public ModuleCommand getCommand(String name)
  74. {
  75. return commands.get(name);
  76. }
  77. @SuppressWarnings("unchecked")
  78. public <T extends ModuleCommand> T getCommand(Class<T> c)
  79. {
  80. return (T) commands.get(c.getSimpleName().substring(7).toLowerCase());
  81. }
  82. //--------------------------------------------------------------------------
  83. // Event-Methoden
  84. //--------------------------------------------------------------------------
  85. @SuppressWarnings("unchecked")
  86. public void registerEvents(String packageName)
  87. {
  88. EventBus bus = MinecraftForge.EVENT_BUS;
  89. PackageHelper.getClasses(packageName).stream().filter(c -> ModuleListener.class.isAssignableFrom(c)).forEach((Class c) ->
  90. {
  91. try
  92. {
  93. ModuleListener l = (ModuleListener) c.getDeclaredConstructor(Module.class).newInstance(this);
  94. events.put(c, l);
  95. bus.register(l);
  96. }
  97. catch(NoSuchMethodException | ClassCastException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
  98. {
  99. this.sendToConsole("Fehler bei " + c);
  100. this.sendToConsole(ex.toString());
  101. }
  102. });
  103. this.sendToConsole("Alle Events wurden geladen.");
  104. }
  105. @SuppressWarnings("unchecked")
  106. public <T extends ModuleListener> T getEvent(Class<T> c)
  107. {
  108. return (T) events.get(c);
  109. }
  110. }