Created abstract class for modules
This commit is contained in:
parent
0478c6df63
commit
30bb267366
@ -8,7 +8,7 @@ plugins {
|
|||||||
mainClassName = 'tech.nevets.modbot.CoreBot'
|
mainClassName = 'tech.nevets.modbot.CoreBot'
|
||||||
|
|
||||||
group 'tech.nevets'
|
group 'tech.nevets'
|
||||||
version '1.0.1'
|
version '1.1.0'
|
||||||
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
@ -7,7 +7,7 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
|||||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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.api.Config;
|
||||||
import tech.nevets.modbot.util.commands.CoreListener;
|
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 Config CORE_CONFIG = new Config("core");
|
||||||
public static final List<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
|
public static final List<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
|
||||||
private static String botPrefix;
|
private static String botPrefix;
|
||||||
private static List<IBotModule> modules;
|
private static List<BotModule> modules;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(CoreBot.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(CoreBot.class);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -45,7 +45,7 @@ public class CoreBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
coreListener.getCommandRegistry().registerSlashCommands();
|
coreListener.getCommandRegistry().registerSlashCommands();
|
||||||
for (IBotModule module : modules) {
|
for (BotModule module : modules) {
|
||||||
if (module.loadCommandRegistry() == null) {
|
if (module.loadCommandRegistry() == null) {
|
||||||
LOGGER.warn("Module has no commands to load");
|
LOGGER.warn("Module has no commands to load");
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,6 +2,8 @@ package tech.nevets.modbot;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import tech.nevets.modbot.api.BotModule;
|
||||||
|
import tech.nevets.modbot.api.BotModuleFactory;
|
||||||
import tech.nevets.modbot.api.IBotModule;
|
import tech.nevets.modbot.api.IBotModule;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -45,29 +47,31 @@ public class ModuleLoader {
|
|||||||
//TODO: make this dynamically wrap all interface modules into abstract modules
|
//TODO: make this dynamically wrap all interface modules into abstract modules
|
||||||
//TODO: handle both types of modules
|
//TODO: handle both types of modules
|
||||||
|
|
||||||
public static List<IBotModule> loadModules() {
|
public static List<BotModule> loadModules() {
|
||||||
List<Path> modulePaths = getModules();
|
List<Path> modulePaths = getModules();
|
||||||
List<IBotModule> modules = new ArrayList<>();
|
List<BotModule> modules = new ArrayList<>();
|
||||||
|
|
||||||
for (Path module : modulePaths) {
|
for (Path module : modulePaths) {
|
||||||
JarInputStream jarStream;
|
JarInputStream jarStream;
|
||||||
Manifest mf;
|
Manifest mf;
|
||||||
Attributes attributes;
|
Attributes attributes;
|
||||||
String mainClassName;
|
String mainClassName;
|
||||||
|
String moduleName;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
|
jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
|
||||||
mf = jarStream.getManifest();
|
mf = jarStream.getManifest();
|
||||||
attributes = mf.getMainAttributes();
|
attributes = mf.getMainAttributes();
|
||||||
mainClassName = attributes.getValue("Main-Class");
|
mainClassName = attributes.getValue("Main-Class");
|
||||||
|
moduleName = attributes.getValue("Module-Name");
|
||||||
} catch (final Exception e) {
|
} 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();
|
e.printStackTrace();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mainClassName == null) {
|
if (mainClassName == null) {
|
||||||
LOGGER.error("Unable to get mainClassName from module: " + module.toString());
|
LOGGER.error("Unable to get mainClassName from module: " + moduleName);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +80,7 @@ public class ModuleLoader {
|
|||||||
try {
|
try {
|
||||||
url = new URL[]{file.toURI().toURL()};
|
url = new URL[]{file.toURI().toURL()};
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
LOGGER.error("Error occurred while getting file URL");
|
LOGGER.error("Error occurred while getting file URL from module " + moduleName);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -86,29 +90,36 @@ public class ModuleLoader {
|
|||||||
try {
|
try {
|
||||||
mainClass = Class.forName(mainClassName, true, urlClassLoader);
|
mainClass = Class.forName(mainClassName, true, urlClassLoader);
|
||||||
} catch (ClassNotFoundException e) {
|
} 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();
|
e.printStackTrace();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BotModule newModule;
|
||||||
String moduleType = attributes.getValue("Module-Type");
|
String moduleType = attributes.getValue("Module-Type");
|
||||||
if (moduleType.equalsIgnoreCase("inherit") || moduleType.equalsIgnoreCase("i")) {
|
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")) {
|
} 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 {
|
} 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;
|
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();
|
newModule.onPreEnable();
|
||||||
modules.add(newModule);
|
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;
|
return modules;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package tech.nevets.modbot.api;
|
package tech.nevets.modbot.api;
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA;
|
public abstract class BotModule implements IBotModule {
|
||||||
import tech.nevets.modbot.api.commands.CommandRegistry;
|
|
||||||
|
|
||||||
abstract class BotModule {
|
|
||||||
private String moduleName;
|
private String moduleName;
|
||||||
|
|
||||||
public BotModule(String moduleName) {
|
public BotModule(String moduleName) {
|
||||||
@ -13,12 +10,4 @@ abstract class BotModule {
|
|||||||
public String getModuleName() {
|
public String getModuleName() {
|
||||||
return moduleName;
|
return moduleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onPreEnable() {}
|
|
||||||
|
|
||||||
abstract CommandRegistry loadCommandRegistry();
|
|
||||||
|
|
||||||
abstract void onEnable(JDA jda);
|
|
||||||
|
|
||||||
abstract void onDisable();
|
|
||||||
}
|
}
|
||||||
|
27
src/main/java/tech/nevets/modbot/api/BotModuleFactory.java
Normal file
27
src/main/java/tech/nevets/modbot/api/BotModuleFactory.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -5,12 +5,6 @@ import tech.nevets.modbot.api.commands.CommandRegistry;
|
|||||||
|
|
||||||
public interface IBotModule {
|
public interface IBotModule {
|
||||||
|
|
||||||
String moduleName = "";
|
|
||||||
|
|
||||||
default void setModuleName(String moduleName) {
|
|
||||||
this.moduleName = moduleName;
|
|
||||||
}
|
|
||||||
|
|
||||||
default void onPreEnable() {}
|
default void onPreEnable() {}
|
||||||
|
|
||||||
CommandRegistry loadCommandRegistry();
|
CommandRegistry loadCommandRegistry();
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package tech.nevets.modbot.api.commands.test;
|
|
||||||
|
|
||||||
public interface Icmd {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package tech.nevets.modbot.api.commands.test;
|
|
||||||
|
|
||||||
public class cmd {
|
|
||||||
}
|
|
@ -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();
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user