Created abstract class for modules
This commit is contained in:
parent
0478c6df63
commit
30bb267366
@ -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
|
||||
|
@ -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<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
|
||||
private static String botPrefix;
|
||||
private static List<IBotModule> modules;
|
||||
private static List<BotModule> 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 {
|
||||
|
@ -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<IBotModule> loadModules() {
|
||||
public static List<BotModule> loadModules() {
|
||||
List<Path> modulePaths = getModules();
|
||||
List<IBotModule> modules = new ArrayList<>();
|
||||
List<BotModule> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
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 {
|
||||
|
||||
String moduleName = "";
|
||||
|
||||
default void setModuleName(String moduleName) {
|
||||
this.moduleName = moduleName;
|
||||
}
|
||||
|
||||
default void onPreEnable() {}
|
||||
|
||||
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