From 71906eb107889fd4e2eaa5687ca05560fa5cb192 Mon Sep 17 00:00:00 2001 From: Pepich Date: Fri, 5 May 2017 09:43:31 +0200 Subject: Updated to APIv3.2 Commands can now be unregistered. Requires CommandManager as of 2017/05/05 09:44 --- .../coremods/moduleLoader/ModuleLoader.cmd | 10 ++- .../coremods/moduleLoader/ModuleLoader.java | 85 +++++++++++++++++++++- 2 files changed, 90 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd index 9dd5445..e88cafa 100644 --- a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd +++ b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd @@ -1,13 +1,19 @@ command modules { list { help Lists all modules. Color indicates status: §aENABLED §cDISABLED; - perm jutils.admin; + perm jutils.modules.list; run list; } load [string:name...] { help (Re)-Loads a module. WARNING: Handle with care! This has direct affect on code being executed. This command will temporarily halt the main thread until the class loading operation was completed.; - perm jtuils.admin; + perm jtuils.modules.admin; run load name; type console; } + unload [string:name...] { + help Unloads a module. WARNING: Handle with care! This has direct affect on code being executed. This command will temporarily halt the main thread until the class loading operation was completed.; + perm jutils.modules.admin; + run unload name; + type console; + } } \ No newline at end of file diff --git a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java index 7739db9..71fe3a3 100644 --- a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java +++ b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java @@ -3,9 +3,11 @@ package com.redstoner.coremods.moduleLoader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -35,7 +37,7 @@ import net.minecraft.server.v1_11_R1.MinecraftServer; /** The module loader, mother of all modules. Responsible for loading and taking care of all modules. * * @author Pepich */ -@Version(major = 3, minor = 1, revision = 6, compatible = 2) +@Version(major = 3, minor = 2, revision = 0, compatible = 2) public final class ModuleLoader implements CoreModule { private static ModuleLoader instance; @@ -99,10 +101,24 @@ public final class ModuleLoader implements CoreModule } Utils.error("Invalid config file! Creating new, blank file!"); } - List autoload = config.getStringList("autoLoad"); + List coremods = config.getStringList("coremods"); + if (coremods == null || coremods.isEmpty()) + { + config.set("coremods", new String[] {"# Add the coremodules here!"}); + Main.plugin.saveConfig(); + try + { + config.save(configFile); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + List autoload = config.getStringList("autoload"); if (autoload == null || autoload.isEmpty()) { - config.set("autoLoad", new String[] {"# Add the modules here!"}); + config.set("autoload", new String[] {"# Add the modules here!"}); Main.plugin.saveConfig(); try { @@ -113,6 +129,9 @@ public final class ModuleLoader implements CoreModule e.printStackTrace(); } } + for (String s : coremods) + if (!s.startsWith("#")) + ModuleLoader.addDynamicModule(s); for (String s : autoload) if (!s.startsWith("#")) ModuleLoader.addDynamicModule(s); @@ -300,9 +319,24 @@ public final class ModuleLoader implements CoreModule { HandlerList.unregisterAll((Listener) module); } + String[] commands = getAllHooks(module).toArray(new String[] {}); + CommandManager.unregisterAll(commands); } } + private static ArrayList getAllHooks(Module module) + { + ArrayList commands = new ArrayList(); + for (Method m : module.getClass().getMethods()) + { + Command cmd = m.getDeclaredAnnotation(Command.class); + if (cmd == null) + continue; + commands.add(cmd.hook()); + } + return commands; + } + @Command(hook = "load") public boolean loadModule(CommandSender sender, String name) { @@ -310,6 +344,13 @@ public final class ModuleLoader implements CoreModule return true; } + @Command(hook = "unload") + public boolean unloadModule(CommandSender sender, String name) + { + removeDynamicModule(name); + return true; + } + public static final void addDynamicModule(String name) { Object[] status = getServerStatus(); @@ -451,6 +492,44 @@ public final class ModuleLoader implements CoreModule } } + public static final void removeDynamicModule(String name) + { + Object[] status = getServerStatus(); + for (Module m : modules.keySet()) + { + if (m.getClass().getName().equals(name)) + { + Utils.info( + "Found existing module, attempting unload. WARNING! This operation will halt the main thread until it is completed."); + Utils.info("Current server status:"); + Utils.info("Current system time: " + status[0]); + Utils.info("Current tick: " + status[1]); + Utils.info("Last TPS: " + status[2]); + Utils.info("Entity count: " + status[3]); + Utils.info("Player count: " + status[4]); + Utils.info("Attempting to disable module properly:"); + disableModule(m); + Utils.info("Disabled module, overriding the implementation:"); + Object[] newStatus = getServerStatus(); + Utils.info("Task complete! Took " + ((long) newStatus[0] - (long) status[0]) + "ms to finish!"); + Utils.info("Current server status:"); + Utils.info("Current system time: " + newStatus[0]); + Utils.info("Current tick: " + newStatus[1]); + Utils.info("Last TPS: " + newStatus[2]); + Utils.info("Entity count: " + newStatus[3]); + Utils.info("Player count: " + newStatus[4]); + return; + } + } + if (!name.startsWith("com.redstoner.modules.")) + { + Utils.warn("Couldn't find class definition, suspecting missing path. Autocompleting path, trying again."); + removeDynamicModule("com.redstoner.modules." + name); + } + else + Utils.error("Couldn't find module! Couldn't "); + } + @SuppressWarnings("deprecation") private static final Object[] getServerStatus() { -- cgit v1.2.3