0

APIv4, support for spigot 1.12 (and up) #6

Merged
Pepich merged 13 commits from APIv4_spigot_1.12 into master 2017-10-31 15:53:31 +00:00
10 changed files with 443 additions and 310 deletions
Showing only changes of commit 5ccfe4b121 - Show all commits

View File

@ -1,11 +1,14 @@
package com.redstoner.annotations; package com.redstoner.annotations;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import com.redstoner.misc.CommandHolderType; import com.redstoner.misc.CommandHolderType;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Commands public @interface Commands
{ {
CommandHolderType value(); CommandHolderType value();

View File

@ -1,18 +1,33 @@
command modules { command modules {
[empty] {
help Lists all modules. Color indicates status: §aENABLED §cDISABLED;
perm moduleloader.modules.list;
run list;
}
list { list {
help Lists all modules. Color indicates status: §aENABLED §cDISABLED; help Lists all modules. Color indicates status: §aENABLED §cDISABLED;
perm jutils.modules.list; perm moduleloader.modules.list;
run list; run list;
} }
-v {
help Lists all modules. Color indicates status: §aENABLED §cDISABLED;
perm moduleloader.modules.list;
run listv;
}
list -v {
help Lists all modules. Color indicates status: §aENABLED §cDISABLED;
perm moduleloader.modules.list;
run listv;
}
load [string:name...] { 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.; 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.modules.admin; perm moduleloader.modules.admin;
run load name; run load name;
type console; type console;
} }
unload [string:name...] { 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.; 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; perm moduleloader.modules.admin;
run unload name; run unload name;
type console; type console;
} }

View File

@ -25,16 +25,19 @@ import com.nemez.cmdmgr.CommandManager;
import com.redstoner.annotations.AutoRegisterListener; import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Commands; import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version; import com.redstoner.annotations.Version;
import com.redstoner.exceptions.MissingVersionException;
import com.redstoner.misc.Main; import com.redstoner.misc.Main;
import com.redstoner.misc.Utils;
import com.redstoner.misc.VersionHelper; import com.redstoner.misc.VersionHelper;
import com.redstoner.modules.CoreModule; import com.redstoner.modules.CoreModule;
import com.redstoner.modules.Module; import com.redstoner.modules.Module;
import com.redstoner.modules.ModuleLogger;
import net.nemez.chatapi.click.Message;
/** The module loader, mother of all modules. Responsible for loading and taking care of all modules. /** The module loader, mother of all modules. Responsible for loading and taking care of all modules.
* *
* @author Pepich */ * @author Pepich */
@Version(major = 4, minor = 0, revision = 0, compatible = 2) @Version(major = 4, minor = 0, revision = 0, compatible = 4)
public final class ModuleLoader implements CoreModule public final class ModuleLoader implements CoreModule
{ {
private static ModuleLoader instance; private static ModuleLoader instance;
@ -45,6 +48,7 @@ public final class ModuleLoader implements CoreModule
private static File configFile; private static File configFile;
private static FileConfiguration config; private static FileConfiguration config;
private static boolean debugMode = false; private static boolean debugMode = false;
private static HashMap<Module, ModuleLogger> loggers = new HashMap<Module, ModuleLogger>();
private ModuleLoader() private ModuleLoader()
{ {
@ -66,6 +70,7 @@ public final class ModuleLoader implements CoreModule
{ {
if (instance == null) if (instance == null)
instance = new ModuleLoader(); instance = new ModuleLoader();
loggers.put(instance, new ModuleLogger("ModuleLoader"));
CommandManager.registerCommand(ModuleLoader.class.getResourceAsStream("ModuleLoader.cmd"), instance, CommandManager.registerCommand(ModuleLoader.class.getResourceAsStream("ModuleLoader.cmd"), instance,
Main.plugin); Main.plugin);
} }
@ -98,7 +103,7 @@ public final class ModuleLoader implements CoreModule
{ {
e1.printStackTrace(); e1.printStackTrace();
} }
Utils.error("Invalid config file! Creating new, blank file!"); instance.getLogger().error("Invalid config file! Creating new, blank file!");
} }
List<String> coremods = config.getStringList("coremods"); List<String> coremods = config.getStringList("coremods");
if (coremods == null || coremods.isEmpty()) if (coremods == null || coremods.isEmpty())
@ -143,23 +148,21 @@ public final class ModuleLoader implements CoreModule
debugMode = config.getBoolean("debugMode"); debugMode = config.getBoolean("debugMode");
for (String s : coremods) for (String s : coremods)
if (!s.startsWith("#")) if (!s.startsWith("#"))
ModuleLoader.addDynamicModule(s); if (!ModuleLoader.addDynamicModule(s))
enableModules(); {
instance.getLogger().error("Couldn't autocomplete path for module name: " + s
+ "! If you're on a case sensitive filesystem, please take note that case correction does not work. Make sure that the classname has proper capitalisation.");
}
for (String s : autoload) for (String s : autoload)
if (!s.startsWith("#")) if (!s.startsWith("#"))
ModuleLoader.addDynamicModule(s); if (!ModuleLoader.addDynamicModule(s))
enableModules(); {
} instance.getLogger().error("Couldn't autocomplete path for module name: " + s
+ "! If you're on a case sensitive filesystem, please take note that case correction does not work. Make sure that the classname has proper capitalisation.");
/** Call this to enable all not-yet enabled modules that are known to the loader. */
public static final void enableModules()
{
for (Module module : modules.keySet())
{
if (modules.get(module))
continue;
enableLoadedModule(module);
} }
updateConfig();
} }
/** This method enables a specific module. If no module with that name is known to the loader yet it will be added to the list.</br> /** This method enables a specific module. If no module with that name is known to the loader yet it will be added to the list.</br>
@ -176,7 +179,7 @@ public final class ModuleLoader implements CoreModule
{ {
if (modules.get(module)) if (modules.get(module))
{ {
Utils.info("Module was already enabled! Ignoring module.!"); instance.getLogger().info("Module was already enabled! Ignoring module.!");
return true; return true;
} }
if (module.onEnable()) if (module.onEnable())
@ -186,14 +189,14 @@ public final class ModuleLoader implements CoreModule
{ {
Bukkit.getPluginManager().registerEvents((Listener) module, Main.plugin); Bukkit.getPluginManager().registerEvents((Listener) module, Main.plugin);
} }
Utils.info("Enabled module " + module.getClass().getName()); instance.getLogger().info("Enabled module " + module.getClass().getName());
Utils.info("Loaded module " + module.getClass().getName()); instance.getLogger().info("Loaded module " + module.getClass().getName());
modules.put(module, true); modules.put(module, true);
return true; return true;
} }
else else
{ {
Utils.error("Failed to enable module " + module.getClass().getName()); instance.getLogger().error("Failed to enable module " + module.getClass().getName());
return false; return false;
} }
} }
@ -208,33 +211,36 @@ public final class ModuleLoader implements CoreModule
{ {
Bukkit.getPluginManager().registerEvents((Listener) m, Main.plugin); Bukkit.getPluginManager().registerEvents((Listener) m, Main.plugin);
} }
Utils.info("Loaded and enabled module " + m.getClass().getName()); instance.getLogger().info("Loaded and enabled module " + m.getClass().getName());
Utils.info("Loaded module " + m.getClass().getName()); instance.getLogger().info("Loaded module " + m.getClass().getName());
return true; return true;
} }
else else
{ {
Utils.error("Failed to enable module " + m.getClass().getName()); instance.getLogger().error("Failed to enable module " + m.getClass().getName());
return false; return false;
} }
} }
catch (InstantiationException | IllegalAccessException e) catch (InstantiationException | IllegalAccessException e)
{ {
Utils.error("Could not add " + clazz.getName() + " to the list, constructor not accessible."); instance.getLogger()
.error("Could not add " + clazz.getName() + " to the list, constructor not accessible.");
return false; return false;
} }
} }
@SuppressWarnings("deprecation") private static final void enableLoadedModule(Module module, Version oldVersion)
private static final void enableLoadedModule(Module module)
{ {
try try
{ {
loggers.put(module, new ModuleLogger(module.getClass().getSimpleName()));
if (module.onEnable()) if (module.onEnable())
{ {
modules.put(module, true); modules.put(module, true);
if (VersionHelper.isCompatible(VersionHelper.create(2, 0, 0, -1), module.getClass())) if (oldVersion.toString().equals("0.0.0.0"))
CommandManager.registerCommand(module.getCommandString(), module, Main.plugin); module.firstLoad();
else if (!VersionHelper.getVersion(module.getClass()).equals(oldVersion))
module.migrate(oldVersion);
if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 3), module.getClass())) if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 3), module.getClass()))
{ {
module.postEnable(); module.postEnable();
@ -247,7 +253,8 @@ public final class ModuleLoader implements CoreModule
switch (ann.value()) switch (ann.value())
{ {
case File: case File:
File f = new File(module.getClass().getName() + ".cmd"); File f = new File("plugins/ModuleLoader/classes/"
+ module.getClass().getName().replace(".", "/") + ".cmd");
CommandManager.registerCommand(f, module, Main.plugin); CommandManager.registerCommand(f, module, Main.plugin);
break; break;
case Stream: case Stream:
@ -262,18 +269,16 @@ public final class ModuleLoader implements CoreModule
} }
} }
} }
Utils.info("Loaded module " + module.getClass().getName()); instance.getLogger().info("Loaded module " + module.getClass().getName());
if (module.getClass().isAnnotationPresent(AutoRegisterListener.class) && (module instanceof Listener)) if (module.getClass().isAnnotationPresent(AutoRegisterListener.class) && (module instanceof Listener))
{
Bukkit.getPluginManager().registerEvents((Listener) module, Main.plugin); Bukkit.getPluginManager().registerEvents((Listener) module, Main.plugin);
} }
}
else else
Utils.error("Failed to load module " + module.getClass().getName()); instance.getLogger().error("Failed to load module " + module.getClass().getName());
} }
catch (Exception e) catch (Exception e)
{ {
Utils.error("Failed to load module " + module.getClass().getName()); instance.getLogger().error("Failed to load module " + module.getClass().getName());
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -285,19 +290,52 @@ public final class ModuleLoader implements CoreModule
@Command(hook = "list", async = AsyncType.ALWAYS) @Command(hook = "list", async = AsyncType.ALWAYS)
public boolean listModulesCommand(CommandSender sender) public boolean listModulesCommand(CommandSender sender)
{ {
Utils.sendModuleHeader(sender); Message m = new Message(sender, null);
StringBuilder sb = new StringBuilder("Modules:\n"); m.appendText(getLogger().getHeader());
for (Module module : modules.keySet()) m.appendText("§2Modules:\n&e");
Module[] modules = ModuleLoader.modules.keySet().toArray(new Module[] {});
for (int i = 0; i < modules.length; i++)
{ {
Module module = modules[i];
String[] classPath = module.getClass().getName().split("\\."); String[] classPath = module.getClass().getName().split("\\.");
String classname = classPath[classPath.length - 1]; String classname = classPath[classPath.length - 1];
sb.append(modules.get(module) ? "&a" : "&c"); m.appendText((ModuleLoader.modules.get(module) ? "§a" : "§c") + classname);
sb.append(classname); if (i + 1 < modules.length)
sb.append(", "); m.appendText("§7, ");
} }
sb.delete(sb.length() - 2, sb.length()); m.send();
Utils.sendMessage(sender, " §e", sb.toString(), '&'); return true;
Utils.sendMessage(sender, " §7", "For more detailed information, consult the debugger."); }
/** This method lists all modules to the specified CommandSender. The modules will be color coded correspondingly to their enabled status.
*
* @param sender The person to send the info to, usually the issuer of the command or the console sender.
* @return true. */
@Command(hook = "listv", async = AsyncType.ALWAYS)
public boolean listModulesCommandVersion(CommandSender sender)
{
Message m = new Message(sender, null);
m.appendText(getLogger().getHeader());
m.appendText("§2Modules:\n&e");
Module[] modules = ModuleLoader.modules.keySet().toArray(new Module[] {});
for (int i = 0; i < modules.length; i++)
{
Module module = modules[i];
String[] classPath = module.getClass().getName().split("\\.");
String classname = classPath[classPath.length - 1];
try
{
m.appendText((ModuleLoader.modules.get(module) ? "§a" : "§c") + classname + "§e("
+ VersionHelper.getVersion(module.getClass()) + ")");
}
catch (MissingVersionException e)
{
m.appendText((ModuleLoader.modules.get(module) ? "§a" : "§c") + classname + "§c" + "(Unknown Version)");
}
if (i + 1 < modules.length)
m.appendText("§7, ");
}
m.send();
return true; return true;
} }
@ -351,40 +389,55 @@ public final class ModuleLoader implements CoreModule
@Command(hook = "load") @Command(hook = "load")
public boolean loadModule(CommandSender sender, String name) public boolean loadModule(CommandSender sender, String name)
{ {
addDynamicModule(name); if (!addDynamicModule(name))
{
instance.getLogger().message(sender, true, "Couldn't autocomplete path for module name: " + name
+ "! If you're on a case sensitive filesystem, please take note that case correction does not work. Make sure that the classname has proper capitalisation.");
}
updateConfig();
return true; return true;
} }
@Command(hook = "unload") @Command(hook = "unload")
public boolean unloadModule(CommandSender sender, String name) public boolean unloadModule(CommandSender sender, String name)
{ {
removeDynamicModule(name); if (!removeDynamicModule(name))
instance.getLogger().error("Couldn't find module! Couldn't disable nonexisting module!");
return true; return true;
} }
public static final void addDynamicModule(String name) public static final boolean addDynamicModule(String raw_name)
{ {
String[] raw = raw_name.split(" ");
String name = raw[0];
Version oldVersion;
if (raw.length > 1)
oldVersion = VersionHelper.getVersion(raw[1]);
else
oldVersion = VersionHelper.create(0, 0, 0, 0);
for (Module m : modules.keySet()) for (Module m : modules.keySet())
{ {
if (m.getClass().getName().equals(name)) if (m.getClass().getName().equals(name))
{ {
Utils.info( instance.getLogger().info(
"Found existing module, attempting override. WARNING! This operation will halt the main thread until it is completed."); "Found existing module, attempting override. WARNING! This operation will halt the main thread until it is completed.");
Utils.info("Attempting to load new class definition before disabling and removing the old module"); instance.getLogger()
.info("Attempting to load new class definition before disabling and removing the old module");
boolean differs = false; boolean differs = false;
Utils.info("Old class definition: Class@" + m.getClass().hashCode()); instance.getLogger().info("Old class definition: Class@" + m.getClass().hashCode());
ClassLoader delegateParent = mainLoader.getParent(); ClassLoader delegateParent = mainLoader.getParent();
Class<?> newClass = null; Class<?> newClass = null;
URLClassLoader cl = new URLClassLoader(urls, delegateParent); URLClassLoader cl = new URLClassLoader(urls, delegateParent);
try try
{ {
newClass = cl.loadClass(m.getClass().getName()); newClass = cl.loadClass(m.getClass().getName());
Utils.info("Found new class definition: Class@" + newClass.hashCode()); instance.getLogger().info("Found new class definition: Class@" + newClass.hashCode());
differs = m.getClass() != newClass; differs = m.getClass() != newClass;
} }
catch (ClassNotFoundException e) catch (ClassNotFoundException e)
{ {
Utils.error("Could not find a class definition, aborting now!"); instance.getLogger().error("Could not find a class definition, aborting now!");
e.printStackTrace(); e.printStackTrace();
try try
{ {
@ -394,15 +447,15 @@ public final class ModuleLoader implements CoreModule
{ {
e1.printStackTrace(); e1.printStackTrace();
} }
return; return false;
} }
if (!differs) if (!differs)
{ {
if (!debugMode) if (!debugMode)
{ {
Utils.warn( instance.getLogger().warn(
"New class definition equals old definition, are you sure you did everything right?"); "New class definition equals old definition, are you sure you did everything right?");
Utils.info("Aborting now..."); instance.getLogger().info("Aborting now...");
try try
{ {
cl.close(); cl.close();
@ -411,14 +464,14 @@ public final class ModuleLoader implements CoreModule
{ {
e.printStackTrace(); e.printStackTrace();
} }
return; return false;
} }
else else
Utils.warn( instance.getLogger().warn(
"New class definition equals old definition, but debugMode is enabled. Loading anyways."); "New class definition equals old definition, but debugMode is enabled. Loading anyways.");
} }
else else
Utils.info("Found new class definition, attempting to instantiate:"); instance.getLogger().info("Found new class definition, attempting to instantiate:");
Module module = null; Module module = null;
try try
{ {
@ -426,7 +479,7 @@ public final class ModuleLoader implements CoreModule
} }
catch (InstantiationException | IllegalAccessException e) catch (InstantiationException | IllegalAccessException e)
{ {
Utils.error("Could not instantiate the module, aborting!"); instance.getLogger().error("Could not instantiate the module, aborting!");
e.printStackTrace(); e.printStackTrace();
try try
{ {
@ -436,18 +489,19 @@ public final class ModuleLoader implements CoreModule
{ {
e1.printStackTrace(); e1.printStackTrace();
} }
return; return false;
} }
Utils.info("Instantiated new class definition, checking versions"); instance.getLogger().info("Instantiated new class definition, checking versions");
Version oldVersion = m.getClass().getAnnotation(Version.class); oldVersion = m.getClass().getAnnotation(Version.class);
Utils.info("Current version: " + VersionHelper.getString(oldVersion)); instance.getLogger().info("Current version: " + VersionHelper.getString(oldVersion));
Version newVersion = module.getClass().getAnnotation(Version.class); Version newVersion = module.getClass().getAnnotation(Version.class);
Utils.info("Version of remote class: " + VersionHelper.getString(newVersion)); instance.getLogger().info("Version of remote class: " + VersionHelper.getString(newVersion));
if (oldVersion.equals(newVersion)) if (oldVersion.equals(newVersion))
{ {
if (!debugMode) if (!debugMode)
{ {
Utils.error("Detected equal module versions, " + (debugMode instance.getLogger()
.error("Detected equal module versions, " + (debugMode
? " aborting now... Set debugMode to true in your config if you want to continue!" ? " aborting now... Set debugMode to true in your config if you want to continue!"
: " continueing anyways.")); : " continueing anyways."));
if (!debugMode) if (!debugMode)
@ -460,16 +514,17 @@ public final class ModuleLoader implements CoreModule
{ {
e.printStackTrace(); e.printStackTrace();
} }
return; return false;
} }
} }
else else
Utils.warn("New version equals old version, but debugMode is enabled. Loading anyways."); instance.getLogger()
.warn("New version equals old version, but debugMode is enabled. Loading anyways.");
} }
else else
Utils.info("Versions differ, disabling old module"); instance.getLogger().info("Versions differ, disabling old module");
disableModule(m); disableModule(m);
Utils.info("Disabled module, overriding the implementation"); instance.getLogger().info("Disabled module, overriding the implementation");
modules.remove(m); modules.remove(m);
try try
{ {
@ -482,81 +537,115 @@ public final class ModuleLoader implements CoreModule
} }
modules.put(module, false); modules.put(module, false);
loaders.put(module, cl); loaders.put(module, cl);
Utils.info("Successfully updated class definition. Enabling new implementation:"); instance.getLogger().info("Successfully updated class definition. Enabling new implementation:");
enableLoadedModule(module); enableLoadedModule(module, oldVersion);
return; return true;
} }
} }
ClassLoader delegateParent = mainLoader.getParent();
URLClassLoader cl = new URLClassLoader(urls, delegateParent);
try try
{ {
Class<?> clazz = mainLoader.loadClass(name); Class<?> clazz = cl.loadClass(name);
Module module = (Module) clazz.newInstance(); Module module = (Module) clazz.newInstance();
modules.put(module, false); modules.put(module, false);
enableLoadedModule(module); loaders.put(module, cl);
enableLoadedModule(module, oldVersion);
return true;
} }
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) catch (NoClassDefFoundError | ClassNotFoundException | InstantiationException | IllegalAccessException e)
{ {
try
{
cl.close();
}
catch (IOException e1)
{}
if (e instanceof NoClassDefFoundError)
{
NoClassDefFoundError exception = (NoClassDefFoundError) e;
String[] exMessage = exception.getMessage().split(" ");
String moduleName = exMessage[exMessage.length - 1]
.substring(0, exMessage[exMessage.length - 1].length()
- (exMessage[exMessage.length - 1].endsWith(")") ? 1 : 0))
.replace("/", ".");
if (!moduleName.equalsIgnoreCase(name))
{
instance.getLogger()
.error("Class &e" + moduleName + "&r couldn't be found! Suspecting a missing dependency!");
return false;
}
else
instance.getLogger().warn(
"Couldn't find class definition, attempting to get proper classname from thrown Exception.");
if (addDynamicModule(moduleName))
return true;
}
if (name.endsWith(".class")) if (name.endsWith(".class"))
{ {
Utils.warn( instance.getLogger().warn(
"Couldn't find class definition, but path ends with .class -> Attempting again with removed file suffix."); "Couldn't find class definition, but path ends with .class -> Attempting again with removed file suffix.");
addDynamicModule(name.replaceAll(".class$", "")); if (addDynamicModule(name.replaceAll(".class$", "")))
return true;
} }
if (!name.contains(".")) if (!name.contains("."))
{ {
Utils.warn( instance.getLogger().warn(
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of path by adding a packet name and trying again."); "Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of path by adding a package name and trying again.");
addDynamicModule(name.toLowerCase() + "." + name); if (addDynamicModule(name.toLowerCase() + "." + name))
return true;
} }
if (!name.startsWith("com.redstoner.modules.")) if (!name.startsWith("com.redstoner.modules.") && name.contains("."))
{ {
Utils.warn( instance.getLogger().warn(
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of packet name and trying again."); "Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of package name and trying again.");
addDynamicModule("com.redstoner.modules." + name); if (addDynamicModule("com.redstoner.modules." + name))
return true;
} }
else
e.printStackTrace();
} }
return false;
} }
public static final void removeDynamicModule(String name) public static final boolean removeDynamicModule(String name)
{ {
for (Module m : modules.keySet()) for (Module m : modules.keySet())
{ {
if (m.getClass().getName().equals(name)) if (m.getClass().getName().equals(name))
{ {
Utils.info( instance.getLogger().info(
"Found existing module, attempting unload. WARNING! This operation will halt the main thread until it is completed."); "Found existing module, attempting unload. WARNING! This operation will halt the main thread until it is completed.");
Utils.info("Attempting to disable module properly:"); instance.getLogger().info("Attempting to disable module properly:");
disableModule(m); disableModule(m);
modules.remove(m); modules.remove(m);
Utils.info("Disabled module."); instance.getLogger().info("Disabled module.");
return; return true;
} }
} }
if (!name.startsWith("com.redstoner.modules.")) if (!name.startsWith("com.redstoner.modules."))
{ {
if (name.endsWith(".class")) if (name.endsWith(".class"))
{ {
Utils.warn( instance.getLogger().warn(
"Couldn't find class definition, but path ends with .class -> Attempting again with removed file suffix."); "Couldn't find class definition, but path ends with .class -> Attempting again with removed file suffix.");
addDynamicModule(name.replaceAll(".class$", "")); if (removeDynamicModule(name.replaceAll(".class$", "")))
return true;
} }
if (!name.contains(".")) if (!name.contains("."))
{ {
Utils.warn( instance.getLogger().warn(
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of path by adding a packet name and trying again."); "Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of path by adding a package name and trying again.");
addDynamicModule(name.toLowerCase() + "." + name); if (removeDynamicModule(name.toLowerCase() + "." + name))
return true;
} }
if (!name.startsWith("com.redstoner.modules.")) if (!name.startsWith("com.redstoner.modules."))
{ {
Utils.warn( instance.getLogger().warn(
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of packet name and trying again."); "Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of package name and trying again.");
addDynamicModule("com.redstoner.modules." + name); if (removeDynamicModule("com.redstoner.modules." + name))
return true;
} }
} }
else return false;
Utils.error("Couldn't find module! Couldn't disable nonexisting module!");
} }
/** Finds a module by name for other modules to reference it. /** Finds a module by name for other modules to reference it.
@ -566,7 +655,7 @@ public final class ModuleLoader implements CoreModule
public static Module getModule(String name) public static Module getModule(String name)
{ {
for (Module m : modules.keySet()) for (Module m : modules.keySet())
if (m.getClass().getSimpleName().equals(name) || m.getClass().getName().equals(name)) if (m.getClass().getSimpleName().equalsIgnoreCase(name) || m.getClass().getName().equalsIgnoreCase(name))
return m; return m;
return null; return null;
} }
@ -582,4 +671,69 @@ public final class ModuleLoader implements CoreModule
return true; return true;
return false; return false;
} }
public static ModuleLogger getModuleLogger(Module module)
{
return loggers.get(module);
}
public static void updateConfig()
{
List<String> coremods = config.getStringList("coremods");
ArrayList<String> new_coremods = new ArrayList<String>();
List<String> autoload = config.getStringList("autoload");
ArrayList<String> new_autoload = new ArrayList<String>();
for (String s : coremods)
{
if (s.startsWith("#"))
{
new_coremods.add(s);
}
else
{
s = s.split(" ")[0];
try
{
new_coremods.add(getModule(s).getClass().getName() + " "
+ VersionHelper.getVersion(getModule(s).getClass()));
}
catch (Exception e)
{
new_coremods.add(s + " " + VersionHelper.getString(VersionHelper.create(0, 0, 0, 0)));
}
}
}
for (String s : autoload)
{
if (s.startsWith("#"))
{
new_autoload.add(s);
}
else
{
s = s.split(" ")[0];
try
{
new_autoload.add(getModule(s).getClass().getName() + " "
+ VersionHelper.getVersion(getModule(s).getClass()));
}
catch (Exception e)
{
new_autoload.add(s + " " + VersionHelper.getString(VersionHelper.create(0, 0, 0, 0)));
}
}
}
config.set("coremods", new_coremods);
config.set("autoload", new_autoload);
try
{
config.save(configFile);
}
catch (IOException e)
{
e.printStackTrace();
}
}
} }

View File

@ -8,6 +8,6 @@ public enum CommandHolderType
{ {
Stream, Stream,
File, File,
@Deprecated String, String,
None None
} }

View File

@ -54,6 +54,19 @@ public class JsonManager
{ {
@Override @Override
public void run() public void run()
{
saveSync(object, destination);
}
});
t.start();
}
/** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br>
* Note that this operation will be run on the same thread that you are calling it from!
*
* @param object the JSONObject to save.
* @param destination the file to write to. */
public static void saveSync(JSONObject object, File destination)
{ {
if (destination.exists()) if (destination.exists())
destination.delete(); destination.delete();
@ -70,9 +83,6 @@ public class JsonManager
catch (IOException e) catch (IOException e)
{} {}
} }
});
t.start();
}
/** Loads a JSONArray from a file. /** Loads a JSONArray from a file.
* *
@ -105,6 +115,19 @@ public class JsonManager
{ {
@Override @Override
public void run() public void run()
{
saveSync(array, destination);
}
});
t.start();
}
/** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br>
* Note that this operation will be run on the same thread that you are calling it from!
*
* @param object the JSONArray to save.
* @param destination the file to write to. */
public static void saveSync(JSONArray array, File destination)
{ {
if (destination.exists()) if (destination.exists())
destination.delete(); destination.delete();
@ -121,7 +144,4 @@ public class JsonManager
catch (IOException e) catch (IOException e)
{} {}
} }
});
t.start();
}
} }

View File

@ -6,6 +6,8 @@ import com.redstoner.annotations.Version;
import com.redstoner.coremods.moduleLoader.ModuleLoader; import com.redstoner.coremods.moduleLoader.ModuleLoader;
import com.redstoner.misc.mysql.MysqlHandler; import com.redstoner.misc.mysql.MysqlHandler;
import net.nemez.chatapi.ChatAPI;
/** Main class. Duh. /** Main class. Duh.
* *
* @author Pepich */ * @author Pepich */
@ -18,13 +20,12 @@ public class Main extends JavaPlugin
public void onEnable() public void onEnable()
{ {
plugin = this; plugin = this;
ChatAPI.initialize(this);
// Configger.init(); // Configger.init();
MysqlHandler.init(); MysqlHandler.init();
ModuleLoader.init(); ModuleLoader.init();
// Load modules from config // Load modules from config
ModuleLoader.loadFromConfig(); ModuleLoader.loadFromConfig();
// And enable them
ModuleLoader.enableModules();
} }
@Override @Override

View File

@ -11,6 +11,8 @@ import org.bukkit.entity.Player;
import com.redstoner.annotations.Version; import com.redstoner.annotations.Version;
import net.nemez.chatapi.ChatAPI;
/** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more. /** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more.
* *
* @author Pepich */ * @author Pepich */
@ -24,90 +26,6 @@ public final class Utils
private Utils() private Utils()
{} {}
/** This will send a message to the specified recipient. It will generate the module prefix.
*
* @param recipient Whom to sent the message to.
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
public static void sendMessage(CommandSender recipient, String message)
{
sendMessage(recipient, null, message);
}
/** This will send a message to the specified recipient. It will generate the module prefix. Also, this will be logged to console as a warning.
*
* @param recipient Whom to sent the message to.
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
public static void sendErrorMessage(CommandSender recipient, String message)
{
sendErrorMessage(recipient, null, message);
}
/** This will send a message to the specified recipient. It will generate the module prefix if you want it to.
*
* @param recipient Whom to sent the message to.
* @param prefix The prefix for the message. If null, the default prefix will be used: &8[&2MODULE&8]
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
public static void sendMessage(CommandSender recipient, String prefix, String message)
{
if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: ";
recipient.sendMessage(prefix + "§7" + message);
}
/** This will send a message to the specified recipient. It will generate the module prefix if you want it to. Also, this will be logged to console as a warning.
*
* @param recipient Whom to sent the message to.
* @param prefix The prefix for the message. If null, the default prefix will be used: &8[&cMODULE&8]
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
public static void sendErrorMessage(CommandSender recipient, String prefix, String message)
{
if (prefix == null)
prefix = "§8[§c" + getCaller() + "§8]: ";
recipient.sendMessage(prefix + "§7" + message);
}
/** Invokes sendMessage. This method will additionally translate alternate color codes for you.
*
* @param recipient Whom to sent the message to.
* @param prefix The prefix for the message. If null, the default prefix will be used: &8[&cMODULE&8]
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise.
* @param alternateColorCode The alternate color code indicator to use. If set to '&' then "&7" would be translated to "§7". Works with any char. */
public static void sendMessage(CommandSender recipient, String prefix, String message, char alternateColorCode)
{
if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: ";
sendMessage(recipient, colorify(prefix, alternateColorCode), colorify(message, alternateColorCode));
}
/** Invokes sendErrorMessage. This method will additionally translate alternate color codes for you.
*
* @param recipient Whom to sent the message to.
* @param prefix The prefix for the message. If null, the default prefix will be used: &8[&cMODULE&8]
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise.
* @param alternateColorCode The alternate color code indicator to use. If set to '&' then "&7" would be translated to "§7". Works with any char. */
public static void sendErrorMessage(CommandSender recipient, String prefix, String message, char alternateColorCode)
{
if (prefix == null)
prefix = "§8[§c" + getCaller() + "§8]: ";
sendErrorMessage(recipient, colorify(prefix, '&'), colorify(message, '&'));
}
/** This method broadcasts a message to all players (and console) that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
* This will not be logged to console except when you return true in the filter.
*
* @param message the message to be sent around
* @param filter the BroadcastFilter to be applied.</br>
* Write a class implementing the interface and pass it to this method, the "sendTo()" method will be called for each recipient.
* @param alternateColorCode The alternate color code indicator to use. If set to '&' then "&7" would be translated to "§7". Works with any char.
* @return the amount of people that received the message. */
public static int broadcast(String prefix, String message, BroadcastFilter filter, char alternateColorCode)
{
if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: ";
message = colorify(message, alternateColorCode);
return broadcast(prefix, message, filter);
}
/** This method broadcasts a message to all players and console that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br> /** This method broadcasts a message to all players and console that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
* If you want to, you can set a message that will be logged to console. Set to null to not log anything.</br> * If you want to, you can set a message that will be logged to console. Set to null to not log anything.</br>
* You can still allow console in the filter to log the original message. * You can still allow console in the filter to log the original message.
@ -120,6 +38,7 @@ public final class Utils
* @return the amount of people that received the message. */ * @return the amount of people that received the message. */
public static int broadcast(String prefix, String message, BroadcastFilter filter) public static int broadcast(String prefix, String message, BroadcastFilter filter)
{ {
message = ChatAPI.colorify(null, message);
if (prefix == null) if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: "; prefix = "§8[§2" + getCaller() + "§8]: ";
if (filter == null) if (filter == null)
@ -147,45 +66,6 @@ public final class Utils
} }
} }
/** Deprecated. Use Utils.info(message) instead.
*
* @param message The message to be put into console. Prefixes are automatically generated. */
@Deprecated
public static void log(String message)
{
info(message);
}
/** Prints an info message into console. Supports "&" color codes.
*
* @param message The message to be put into console. Prefixes are automatically generated. Color defaults to grey. */
public static void info(String message)
{
String classname = getCaller();
String prefix = "§8[§2" + classname + "§8]: ";
Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
}
/** Prints a warning message into console. Supports "&" color codes.
*
* @param message The message to be put into console. Prefixes are automatically generated. Color defaults to grey. */
public static void warn(String message)
{
String classname = getCaller();
String prefix = "§e[WARN]: §8[§e" + classname + "§8]: ";
Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
}
/** Used to make an error output to console. Supports "&" color codes.
*
* @param message The message to be put into console. Prefixes are automatically generated. Color defaults to red. */
public static void error(String message)
{
String classname = getCaller();
String prefix = "§c[ERROR]: §8[§c" + classname + "§8]: ";
Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
}
/** This method will find the next parent caller and return their class name, omitting package names. /** This method will find the next parent caller and return their class name, omitting package names.
* *
* @return the Name of the calling class. */ * @return the Name of the calling class. */
@ -218,25 +98,6 @@ public final class Utils
return classname; return classname;
} }
/** Displays the module header to the recipient.</br>
* Format: &2--=[ %MODULE% ]=--
*
* @param recipient Whom to display the header to. */
public static void sendModuleHeader(CommandSender recipient)
{
sendModuleHeader(recipient, getCaller());
}
/** Displays the module header to the recipient.</br>
* Format: &2--=[ %HEADER% ]=--
*
* @param recipient Whom to display the header to.
* @param header The module name. */
public static void sendModuleHeader(CommandSender recipient, String header)
{
recipient.sendMessage("§2--=[ " + header + " ]=--");
}
/** Provides a uniform way of getting the date for all modules. /** Provides a uniform way of getting the date for all modules.
* *
* @return The current date in the format "[dd-mm-yyyy hh:mm:ss]" */ * @return The current date in the format "[dd-mm-yyyy hh:mm:ss]" */
@ -271,29 +132,4 @@ public final class Utils
id = "CONSOLE"; id = "CONSOLE";
return id; return id;
} }
/** This method "colorifies" a message.
*
* @param message the message to be colored.
* @return the colorified message. */
public static String colorify(String message, char alternateColorcode)
{
return colorify(message, Bukkit.getConsoleSender(), alternateColorcode);
}
/** This method "colorifies" a message using proper permissions.
*
* @param message the message to be colored.
* @param sender the @CommandSender whose permissions shall be applied.
* @return the colorified message. */
public static String colorify(String message, CommandSender sender, char alternateColorcode)
{
if (sender.hasPermission("essentials.chat.color"))
message = message.replaceAll(alternateColorcode + "([0-9a-fA-FrR])", "§$1");
if (sender.hasPermission("essentials.chat.format"))
message = message.replaceAll(alternateColorcode + "(l-oL-OrR)", "§$1");
if (sender.hasPermission("essentials.chat.magic"))
message = message.replaceAll(alternateColorcode + "([kKrR])", "§$1");
return message.replace(alternateColorcode + "§", alternateColorcode + "");
}
} }

View File

@ -97,6 +97,15 @@ public final class VersionHelper
return ver.major() + "." + ver.minor() + "." + ver.revision() + "." + ver.compatible(); return ver.major() + "." + ver.minor() + "." + ver.revision() + "." + ver.compatible();
} }
public static Version getVersion(String ver)
{
String[] raw = ver.split("\\.");
if (raw.length != 4)
return null;
return VersionHelper.create(Integer.parseInt(raw[0]), Integer.parseInt(raw[1]), Integer.parseInt(raw[2]),
Integer.parseInt(raw[3]));
}
/** This method creates a new Version to use for compatibility checks. /** This method creates a new Version to use for compatibility checks.
* *
* @param major The major version * @param major The major version

View File

@ -1,11 +1,12 @@
package com.redstoner.modules; package com.redstoner.modules;
import com.redstoner.annotations.Version; import com.redstoner.annotations.Version;
import com.redstoner.coremods.moduleLoader.ModuleLoader;
/** Interface for the Module class. Modules must always have an empty constructor to be invoked by the ModuleLoader. /** Interface for the Module class. Modules must always have an empty constructor to be invoked by the ModuleLoader.
* *
* @author Pepich */ * @author Pepich */
@Version(major = 3, minor = 0, revision = 0, compatible = 2) @Version(major = 4, minor = 0, revision = 0, compatible = 0)
public interface Module public interface Module
{ {
/** Will be called when the module gets enabled. */ /** Will be called when the module gets enabled. */
@ -23,13 +24,31 @@ public interface Module
public default void onDisable() public default void onDisable()
{} {}
/** Gets called on registration of the module. /** Gets called on registration of the module, when this option is selected for command registration
* THIS WAS ONLY KEPT FOR COMPATIBILITY REASONS. Please register commands yourself instead using the "postEnable" method.
* *
* @return The String used for the CommandManager to register the commands. */ * @return The String used for the CommandManager to register the commands. */
@Deprecated
public default String getCommandString() public default String getCommandString()
{ {
return null; return null;
} }
public default ModuleLogger getLogger()
{
return ModuleLoader.getModuleLogger(this);
}
/** This method gets run the very first time a module gets loaded. You can use this to set up file structures or background data. */
public default void firstLoad()
{}
/** This method gets run every time a module gets loaded and its version has changed.
*
* @param old The version of the previous module. */
public default void migrate(Version old)
{}
default void setPrefix(final String name)
{
getLogger().setName(name);
}
} }

View File

@ -0,0 +1,76 @@
package com.redstoner.modules;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import com.redstoner.annotations.Version;
import net.nemez.chatapi.ChatAPI;
import net.nemez.chatapi.click.Message;
@Version(major = 4, minor = 0, revision = 0, compatible = -1)
public class ModuleLogger
{
public static final String PREFIX_WARN = "§8[§eWARN§8]:§7 ";
public static final String PREFIX_ERROR = "§8[§cERROR§8]:§7 ";
private String name;
public ModuleLogger(final String name)
{
this.name = name;
}
public void info(final String message)
{
Bukkit.getConsoleSender().sendMessage(getPrefix() + ChatAPI.colorify(null, message));
}
public void warn(final String message)
{
Bukkit.getConsoleSender().sendMessage(PREFIX_WARN + getPrefix() + ChatAPI.colorify(null, message));
}
public void error(final String message)
{
Bukkit.getConsoleSender().sendMessage(PREFIX_ERROR + getPrefix() + ChatAPI.colorify(null, message));
}
public void message(final CommandSender recipient, final String... message)
{
message(recipient, false, message);
}
public void message(final CommandSender recipient, final boolean error, final String... message)
{
Message m = new Message(recipient, null);
if (message.length == 1)
m.appendText(getPrefix(error) + message[0]);
else
{
m.appendText(getHeader());
m.appendText("&7" + String.join("\n&7", message));
}
m.send();
}
public String getPrefix()
{
return getPrefix(false);
}
public String getPrefix(final boolean error)
{
return "§8[§" + (error ? 'c' : '2') + name + "§8]§7 ";
}
public String getHeader()
{
return "§2--=[ " + name + " ]=--\n";
}
protected final void setName(final String name)
{
this.name = name;
}
}