From 0478c6df6321d62f65ec262c6b5008758bb01c2d Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Tue, 13 Sep 2022 14:27:13 -0400 Subject: [PATCH] All in a days work mate --- .idea/.name | 1 + .idea/inspectionProfiles/Project_Default.xml | 8 ++++++ src/main/java/tech/nevets/modbot/CoreBot.java | 6 ++--- .../java/tech/nevets/modbot/ModuleLoader.java | 26 ++++++++++++++----- .../tech/nevets/modbot/api/BotModule.java | 20 +++++++++----- .../tech/nevets/modbot/api/IBotModule.java | 21 +++++++++++++++ .../nevets/modbot/api/commands/test/Icmd.java | 7 +++++ .../nevets/modbot/api/commands/test/cmd.java | 4 +++ .../modbot/api/commands/test/iicmd.java | 15 +++++++++++ 9 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 src/main/java/tech/nevets/modbot/api/IBotModule.java create mode 100644 src/main/java/tech/nevets/modbot/api/commands/test/Icmd.java create mode 100644 src/main/java/tech/nevets/modbot/api/commands/test/cmd.java create mode 100644 src/main/java/tech/nevets/modbot/api/commands/test/iicmd.java diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..6ff5b75 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +ModBot \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..3153295 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/src/main/java/tech/nevets/modbot/CoreBot.java b/src/main/java/tech/nevets/modbot/CoreBot.java index ae52a9b..e63b675 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.BotModule; +import tech.nevets.modbot.api.IBotModule; 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 (BotModule module : modules) { + for (IBotModule 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 d58d41c..df50a77 100644 --- a/src/main/java/tech/nevets/modbot/ModuleLoader.java +++ b/src/main/java/tech/nevets/modbot/ModuleLoader.java @@ -2,7 +2,7 @@ package tech.nevets.modbot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import tech.nevets.modbot.api.BotModule; +import tech.nevets.modbot.api.IBotModule; import java.io.File; import java.io.FileInputStream; @@ -42,9 +42,12 @@ public class ModuleLoader { return modules; } - public static List loadModules() { + //TODO: make this dynamically wrap all interface modules into abstract modules + //TODO: handle both types of modules + + public static List loadModules() { List modulePaths = getModules(); - List modules = new ArrayList<>(); + List modules = new ArrayList<>(); for (Path module : modulePaths) { JarInputStream jarStream; @@ -87,9 +90,20 @@ public class ModuleLoader { e.printStackTrace(); continue; } - BotModule newModule; + + String moduleType = attributes.getValue("Module-Type"); + if (moduleType.equalsIgnoreCase("inherit") || moduleType.equalsIgnoreCase("i")) { + + } else if (moduleType.equalsIgnoreCase("extend") || moduleType.equalsIgnoreCase("e")) { + + } else { + LOGGER.error("Error loading module: Module " + module.toString() + " has an invalid Module-Type!"); + continue; + } + + IBotModule newModule; try { - newModule = (BotModule) mainClass.getDeclaredConstructor().newInstance(); + newModule = (IBotModule) mainClass.getDeclaredConstructor().newInstance(); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { LOGGER.error("Error while creating instance of module " + module.toString()); e.printStackTrace(); @@ -109,7 +123,7 @@ public class ModuleLoader { } } - LOGGER.debug("Loaded module: " + modules.toArray().toString()); + LOGGER.info("Loaded module: " + modules.toArray().toString()); 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 35eb365..b70c56a 100644 --- a/src/main/java/tech/nevets/modbot/api/BotModule.java +++ b/src/main/java/tech/nevets/modbot/api/BotModule.java @@ -3,16 +3,22 @@ package tech.nevets.modbot.api; import net.dv8tion.jda.api.JDA; import tech.nevets.modbot.api.commands.CommandRegistry; -import java.util.HashMap; -import java.util.Map; +abstract class BotModule { + private String moduleName; -public interface BotModule { + public BotModule(String moduleName) { + this.moduleName = moduleName; + } - default void onPreEnable() {} + public String getModuleName() { + return moduleName; + } - CommandRegistry loadCommandRegistry(); + void onPreEnable() {} - void onEnable(JDA jda); + abstract CommandRegistry loadCommandRegistry(); - void onDisable(); + abstract void onEnable(JDA jda); + + abstract void onDisable(); } diff --git a/src/main/java/tech/nevets/modbot/api/IBotModule.java b/src/main/java/tech/nevets/modbot/api/IBotModule.java new file mode 100644 index 0000000..73678dd --- /dev/null +++ b/src/main/java/tech/nevets/modbot/api/IBotModule.java @@ -0,0 +1,21 @@ +package tech.nevets.modbot.api; + +import net.dv8tion.jda.api.JDA; +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(); + + void onEnable(JDA jda); + + void onDisable(); +} 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 new file mode 100644 index 0000000..704a5d1 --- /dev/null +++ b/src/main/java/tech/nevets/modbot/api/commands/test/Icmd.java @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000..51f09ed --- /dev/null +++ b/src/main/java/tech/nevets/modbot/api/commands/test/cmd.java @@ -0,0 +1,4 @@ +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 new file mode 100644 index 0000000..90fccf3 --- /dev/null +++ b/src/main/java/tech/nevets/modbot/api/commands/test/iicmd.java @@ -0,0 +1,15 @@ +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(); +}