diff --git a/build.gradle b/build.gradle index 5a93b68..351220b 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { mainClassName = 'tech.nevets.modbot.CoreBot' group 'tech.nevets' -version '1.0.1' +version '1.1.0' sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 diff --git a/src/main/java/tech/nevets/modbot/CoreBot.java b/src/main/java/tech/nevets/modbot/CoreBot.java index e63b675..ae52a9b 100644 --- a/src/main/java/tech/nevets/modbot/CoreBot.java +++ b/src/main/java/tech/nevets/modbot/CoreBot.java @@ -7,7 +7,7 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.utils.cache.CacheFlag; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import tech.nevets.modbot.api.IBotModule; +import tech.nevets.modbot.api.BotModule; import tech.nevets.modbot.api.Config; import tech.nevets.modbot.util.commands.CoreListener; @@ -19,7 +19,7 @@ public class CoreBot { public static final Config CORE_CONFIG = new Config("core"); public static final List PLUGIN_LISTENERS = new ArrayList<>(); private static String botPrefix; - private static List modules; + private static List modules; private static final Logger LOGGER = LoggerFactory.getLogger(CoreBot.class); public static void main(String[] args) { @@ -45,7 +45,7 @@ public class CoreBot { } coreListener.getCommandRegistry().registerSlashCommands(); - for (IBotModule module : modules) { + for (BotModule module : modules) { if (module.loadCommandRegistry() == null) { LOGGER.warn("Module has no commands to load"); } else { diff --git a/src/main/java/tech/nevets/modbot/ModuleLoader.java b/src/main/java/tech/nevets/modbot/ModuleLoader.java index df50a77..1c4cda2 100644 --- a/src/main/java/tech/nevets/modbot/ModuleLoader.java +++ b/src/main/java/tech/nevets/modbot/ModuleLoader.java @@ -2,6 +2,8 @@ package tech.nevets.modbot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import tech.nevets.modbot.api.BotModule; +import tech.nevets.modbot.api.BotModuleFactory; import tech.nevets.modbot.api.IBotModule; import java.io.File; @@ -45,29 +47,31 @@ public class ModuleLoader { //TODO: make this dynamically wrap all interface modules into abstract modules //TODO: handle both types of modules - public static List loadModules() { + public static List loadModules() { List modulePaths = getModules(); - List modules = new ArrayList<>(); + List modules = new ArrayList<>(); for (Path module : modulePaths) { JarInputStream jarStream; Manifest mf; Attributes attributes; String mainClassName; + String moduleName; try { jarStream = new JarInputStream(new FileInputStream(String.valueOf(module))); mf = jarStream.getManifest(); attributes = mf.getMainAttributes(); mainClassName = attributes.getValue("Main-Class"); + moduleName = attributes.getValue("Module-Name"); } catch (final Exception e) { - LOGGER.error("Error while reading module attributes from module: " + module.toString()); + LOGGER.error("Error while reading module attributes from module: " + module.getFileName()); e.printStackTrace(); continue; } if (mainClassName == null) { - LOGGER.error("Unable to get mainClassName from module: " + module.toString()); + LOGGER.error("Unable to get mainClassName from module: " + moduleName); continue; } @@ -76,7 +80,7 @@ public class ModuleLoader { try { url = new URL[]{file.toURI().toURL()}; } catch (MalformedURLException e) { - LOGGER.error("Error occurred while getting file URL"); + LOGGER.error("Error occurred while getting file URL from module " + moduleName); e.printStackTrace(); continue; } @@ -86,29 +90,36 @@ public class ModuleLoader { try { mainClass = Class.forName(mainClassName, true, urlClassLoader); } catch (ClassNotFoundException e) { - LOGGER.error("Error getting class from classLoader, Class Not Found!"); + LOGGER.error("Error getting class from classLoader, Class Not Found in module " + moduleName + "!"); e.printStackTrace(); continue; } + BotModule newModule; String moduleType = attributes.getValue("Module-Type"); if (moduleType.equalsIgnoreCase("inherit") || moduleType.equalsIgnoreCase("i")) { - + IBotModule iNewModule; + try { + iNewModule = (IBotModule) mainClass.getDeclaredConstructor().newInstance(); + } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { + LOGGER.error("Error while creating instance of module " + moduleName); + e.printStackTrace(); + continue; + } + newModule = BotModuleFactory.fromIBotModule(iNewModule, moduleName); } else if (moduleType.equalsIgnoreCase("extend") || moduleType.equalsIgnoreCase("e")) { - + try { + newModule = (BotModule) mainClass.getDeclaredConstructor(String.class).newInstance(moduleName); + } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { + LOGGER.error("Error while creating instance of module " + moduleName); + e.printStackTrace(); + continue; + } } else { - LOGGER.error("Error loading module: Module " + module.toString() + " has an invalid Module-Type!"); + LOGGER.error("Error loading module: Module " + moduleName + " has an invalid Module-Type!"); continue; } - IBotModule newModule; - try { - newModule = (IBotModule) mainClass.getDeclaredConstructor().newInstance(); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { - LOGGER.error("Error while creating instance of module " + module.toString()); - e.printStackTrace(); - continue; - } newModule.onPreEnable(); modules.add(newModule); @@ -123,7 +134,15 @@ public class ModuleLoader { } } - LOGGER.info("Loaded module: " + modules.toArray().toString()); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < modules.size(); i++) { + sb.append(modules.get(i).getModuleName()); + if (i < modules.size()) { + sb.append(", "); + } + } + + LOGGER.info("Loaded modules: " + sb); return modules; } } diff --git a/src/main/java/tech/nevets/modbot/api/BotModule.java b/src/main/java/tech/nevets/modbot/api/BotModule.java index b70c56a..32b52b5 100644 --- a/src/main/java/tech/nevets/modbot/api/BotModule.java +++ b/src/main/java/tech/nevets/modbot/api/BotModule.java @@ -1,9 +1,6 @@ package tech.nevets.modbot.api; -import net.dv8tion.jda.api.JDA; -import tech.nevets.modbot.api.commands.CommandRegistry; - -abstract class BotModule { +public abstract class BotModule implements IBotModule { private String moduleName; public BotModule(String moduleName) { @@ -13,12 +10,4 @@ abstract class BotModule { public String getModuleName() { return moduleName; } - - void onPreEnable() {} - - abstract CommandRegistry loadCommandRegistry(); - - abstract void onEnable(JDA jda); - - abstract void onDisable(); } diff --git a/src/main/java/tech/nevets/modbot/api/BotModuleFactory.java b/src/main/java/tech/nevets/modbot/api/BotModuleFactory.java new file mode 100644 index 0000000..fb5b789 --- /dev/null +++ b/src/main/java/tech/nevets/modbot/api/BotModuleFactory.java @@ -0,0 +1,27 @@ +package tech.nevets.modbot.api; + +import net.dv8tion.jda.api.JDA; +import tech.nevets.modbot.api.commands.CommandRegistry; + +public class BotModuleFactory { + + public static BotModule fromIBotModule(IBotModule iBotModule, String moduleName) { + + return new BotModule(moduleName) { + @Override + public CommandRegistry loadCommandRegistry() { + return iBotModule.loadCommandRegistry(); + } + + @Override + public void onEnable(JDA jda) { + iBotModule.onEnable(jda); + } + + @Override + public void onDisable() { + iBotModule.onDisable(); + } + }; + } +} diff --git a/src/main/java/tech/nevets/modbot/api/IBotModule.java b/src/main/java/tech/nevets/modbot/api/IBotModule.java index 73678dd..9fb42c0 100644 --- a/src/main/java/tech/nevets/modbot/api/IBotModule.java +++ b/src/main/java/tech/nevets/modbot/api/IBotModule.java @@ -5,12 +5,6 @@ import tech.nevets.modbot.api.commands.CommandRegistry; public interface IBotModule { - String moduleName = ""; - - default void setModuleName(String moduleName) { - this.moduleName = moduleName; - } - default void onPreEnable() {} CommandRegistry loadCommandRegistry(); diff --git a/src/main/java/tech/nevets/modbot/api/commands/test/Icmd.java b/src/main/java/tech/nevets/modbot/api/commands/test/Icmd.java deleted file mode 100644 index 704a5d1..0000000 --- a/src/main/java/tech/nevets/modbot/api/commands/test/Icmd.java +++ /dev/null @@ -1,7 +0,0 @@ -package tech.nevets.modbot.api.commands.test; - -public interface Icmd { - - - -} diff --git a/src/main/java/tech/nevets/modbot/api/commands/test/cmd.java b/src/main/java/tech/nevets/modbot/api/commands/test/cmd.java deleted file mode 100644 index 51f09ed..0000000 --- a/src/main/java/tech/nevets/modbot/api/commands/test/cmd.java +++ /dev/null @@ -1,4 +0,0 @@ -package tech.nevets.modbot.api.commands.test; - -public class cmd { -} diff --git a/src/main/java/tech/nevets/modbot/api/commands/test/iicmd.java b/src/main/java/tech/nevets/modbot/api/commands/test/iicmd.java deleted file mode 100644 index 90fccf3..0000000 --- a/src/main/java/tech/nevets/modbot/api/commands/test/iicmd.java +++ /dev/null @@ -1,15 +0,0 @@ -package tech.nevets.modbot.api.commands.test; - -import net.dv8tion.jda.api.JDA; -import tech.nevets.modbot.api.commands.CommandRegistry; - -public interface iicmd { - - default void onPreEnable() {} - - CommandRegistry loadCommandRegistry(); - - void onEnable(JDA jda); - - void onDisable(); -}