0

Updated to APIv3.2

Commands can now be unregistered. Requires CommandManager as of
2017/05/05 09:44
This commit is contained in:
Pepich 2017-05-05 09:43:31 +02:00
parent 1ee4a123d2
commit 71906eb107
2 changed files with 91 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -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,10 @@ public final class ModuleLoader implements CoreModule
}
Utils.error("Invalid config file! Creating new, blank file!");
}
List<String> autoload = config.getStringList("autoLoad");
if (autoload == null || autoload.isEmpty())
List<String> coremods = config.getStringList("coremods");
if (coremods == null || coremods.isEmpty())
{
config.set("autoLoad", new String[] {"# Add the modules here!"});
config.set("coremods", new String[] {"# Add the coremodules here!"});
Main.plugin.saveConfig();
try
{
@ -113,6 +115,23 @@ public final class ModuleLoader implements CoreModule
e.printStackTrace();
}
}
List<String> autoload = config.getStringList("autoload");
if (autoload == null || autoload.isEmpty())
{
config.set("autoload", new String[] {"# Add the modules here!"});
Main.plugin.saveConfig();
try
{
config.save(configFile);
}
catch (IOException e)
{
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<String> getAllHooks(Module module)
{
ArrayList<String> commands = new ArrayList<String>();
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()
{