From f0713d781ad98a50f3a0e8fc58517269537a37e2 Mon Sep 17 00:00:00 2001 From: Minenash Date: Fri, 28 Dec 2018 12:53:24 -0500 Subject: Added Descriptor Files and redid `/modules [list]` --- .../coremods/moduleLoader/ModuleLoader.cmd | 24 ++++ .../coremods/moduleLoader/ModuleLoader.java | 124 ++++++++++++--------- src/main/java/com/redstoner/misc/ModuleInfo.java | 81 ++++++++++++++ .../coremods/moduleLoader/ModuleLoader.cmd | 34 ------ 4 files changed, 178 insertions(+), 85 deletions(-) create mode 100644 src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd create mode 100644 src/main/java/com/redstoner/misc/ModuleInfo.java delete mode 100644 src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd diff --git a/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd new file mode 100644 index 0000000..4e06bd0 --- /dev/null +++ b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd @@ -0,0 +1,24 @@ +command modules { + [empty] { + help Lists all modules. Color indicates status: §aENABLED §cDISABLED; + perm moduleloader.modules.list; + run list; + } + list { + help Lists all modules. Color indicates status: §aENABLED §cDISABLED; + perm moduleloader.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 moduleloader.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 moduleloader.modules.admin; + run unload name; + type console; + } +} \ No newline at end of file diff --git a/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java index 062bda2..4281d58 100644 --- a/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java +++ b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java @@ -1,6 +1,7 @@ package com.redstoner.coremods.moduleLoader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -9,6 +10,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -18,6 +20,7 @@ import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; import com.nemez.cmdmgr.Command; import com.nemez.cmdmgr.Command.AsyncType; @@ -25,15 +28,14 @@ import com.nemez.cmdmgr.CommandManager; import com.redstoner.annotations.AutoRegisterListener; import com.redstoner.annotations.Commands; import com.redstoner.annotations.Version; -import com.redstoner.exceptions.MissingVersionException; import com.redstoner.misc.Main; +import com.redstoner.misc.ModuleInfo; import com.redstoner.misc.VersionHelper; import com.redstoner.modules.CoreModule; import com.redstoner.modules.Module; import com.redstoner.modules.ModuleLogger; import net.nemez.chatapi.click.Message; -import org.bukkit.plugin.java.JavaPlugin; /** The module loader, mother of all modules. Responsible for loading and taking care of all modules. * @@ -43,6 +45,8 @@ public final class ModuleLoader implements CoreModule { private static ModuleLoader instance; private static final HashMap modules = new HashMap<>(); + private static HashMap moduleInfos = new HashMap<>(); + private static HashMap> categorizes = new HashMap<>(); private static URL[] urls; private static URLClassLoader mainLoader; private static HashMap loaders = new HashMap<>(); @@ -71,7 +75,9 @@ public final class ModuleLoader implements CoreModule { if (instance == null) instance = new ModuleLoader(); - loggers.put(instance, new ModuleLogger("ModuleLoader")); + ModuleInfo info = new ModuleInfo(ModuleLoader.class.getResourceAsStream("module.info"), instance); + moduleInfos.put(instance, info); + loggers.put(instance, new ModuleLogger(info.getDisplayName())); CommandManager.registerCommand(ModuleLoader.class.getResourceAsStream("ModuleLoader.cmd"), instance, Main.plugin); } @@ -234,7 +240,33 @@ public final class ModuleLoader implements CoreModule { try { - loggers.put(module, new ModuleLogger(module.getClass().getSimpleName())); + String basePath = "plugins/ModuleLoader/classes/" + module.getClass().getName().replace(".", "/"); + + InputStream file; + try { + file = new FileInputStream( + new File(basePath.substring(0, basePath.lastIndexOf('/')+1) + "module.info")); + } + catch(Exception e) { + file = null; + } + + ModuleInfo info = new ModuleInfo(file, module); + + moduleInfos.put(module, info); + + String category = info.getCategory(); + if (!categorizes.containsKey(category)) + categorizes.put(category, new ArrayList<>(Arrays.asList(module))); + else { + List modsInCat = categorizes.get(category); + modsInCat.add(module); + categorizes.put(category, modsInCat); + } + + loggers.put(module, new ModuleLogger(info.getDisplayName())); + + if (module.onEnable()) { modules.put(module, true); @@ -242,9 +274,9 @@ public final class ModuleLoader implements CoreModule module.firstLoad(); else if (!VersionHelper.getVersion(module.getClass()).equals(VersionHelper.getString(oldVersion))) module.migrate(oldVersion); - if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 3), module.getClass())) + if (VersionHelper.isCompatible(VersionHelper.create(5, 0, 0, 3), module.getClass())) module.postEnable(); - if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 4), module.getClass())) + if (VersionHelper.isCompatible(VersionHelper.create(5, 0, 0, 4), module.getClass())) { Commands ann = module.getClass().getAnnotation(Commands.class); if (ann != null) @@ -289,50 +321,31 @@ public final class ModuleLoader implements CoreModule @Command(hook = "list", async = AsyncType.ALWAYS) public boolean listModulesCommand(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]; - m.appendText((ModuleLoader.modules.get(module) ? "§a" : "§c") + classname); - if (i + 1 < modules.length) - m.appendText("§7, "); - } - m.send(); - return true; - } - - /** 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, "); + boolean hasCategorys = hasCategories(); + Message m = new Message(sender, null); + ModuleInfo ml_info = moduleInfos.get(instance); + + m.appendText("§2--=[ ") + .appendTextHover("§2" + ml_info.getDisplayName(), ml_info.getModuleInfoHover()) + .appendText("§2 ]=--\nModules:\n"); + + for (String cat: categorizes.keySet()) { + if (hasCategorys) + m.appendText("\n&7" + cat + ":\n"); + + int curModule = 1; + List mods = categorizes.get(cat); + for (Module mod : mods) { + + ModuleInfo info = moduleInfos.get(mod); + m.appendTextHover((modules.get(mod) ? "§a" : "§c") + info.getDisplayName(), info.getModuleInfoHover()); + + if (curModule != mods.size()) + m.appendText("&7, "); + curModule++; + } + m.appendText("\n"); + } m.send(); return true; @@ -524,6 +537,9 @@ public final class ModuleLoader implements CoreModule disableModule(m); instance.getLogger().info("Disabled module, overriding the implementation"); modules.remove(m); + categorizes.get(moduleInfos.get(m).getCategory()).remove(m); + moduleInfos.remove(m); + try { if (loaders.containsKey(m)) @@ -615,6 +631,8 @@ public final class ModuleLoader implements CoreModule instance.getLogger().info("Attempting to disable module properly:"); disableModule(m); modules.remove(m); + categorizes.get(moduleInfos.get(m).getCategory()).remove(m); + moduleInfos.remove(m); instance.getLogger().info("Disabled module."); return true; } @@ -734,8 +752,12 @@ public final class ModuleLoader implements CoreModule e.printStackTrace(); } } - + public static JavaPlugin getPlugin() { return Main.plugin; } + + public static boolean hasCategories() { + return !(categorizes.size() == 1 && categorizes.containsKey("Other")); + } } diff --git a/src/main/java/com/redstoner/misc/ModuleInfo.java b/src/main/java/com/redstoner/misc/ModuleInfo.java new file mode 100644 index 0000000..e96e813 --- /dev/null +++ b/src/main/java/com/redstoner/misc/ModuleInfo.java @@ -0,0 +1,81 @@ +package com.redstoner.misc; + +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import com.redstoner.coremods.moduleLoader.ModuleLoader; +import com.redstoner.exceptions.MissingVersionException; +import com.redstoner.modules.Module; + +public class ModuleInfo { + + private String simpleName; + private String displayName; + private String category; + private String description; + private String version; + + private String warning; + + public ModuleInfo(InputStream descriptor, Module module) { + try { + InputStreamReader reader = new InputStreamReader(descriptor); + FileConfiguration config = YamlConfiguration.loadConfiguration(reader); + + displayName = config.getString("displayName"); + category = config.getString("category"); + description = config.getString("description"); + } + catch (Exception e) { + warning = "Descriptor file could not be loaded, using the class's name."; + } + + simpleName = module.getClass().getSimpleName(); + + if (displayName == null) + displayName = simpleName; + + if (category == null) + category = "Other"; + + try { + version = VersionHelper.getVersion(module.getClass()); + } catch (MissingVersionException e) {} + } + + public String getSimpleName() { + return simpleName; + } + + public String getDisplayName() { + return displayName; + } + + public String getCategory() { + return category; + } + + public String getDescription() { + return description; + } + + public String getWarning() { + return warning; + } + + public String getVersion() { + return version; + } + + public String getModuleInfoHover() { + return "&8&o" + getSimpleName() + "\n" + + "&r&e" + (getVersion() == null? "&cVersion Missing" : getVersion()) + + "&r&9" + (ModuleLoader.hasCategories()? "\n" + getCategory() : "") + + "&r&7" + (getDescription() == null? "" : "\n\n" + getDescription()); + } + + +} diff --git a/src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd b/src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd deleted file mode 100644 index eadeeb7..0000000 --- a/src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd +++ /dev/null @@ -1,34 +0,0 @@ -command modules { - [empty] { - help Lists all modules. Color indicates status: §aENABLED §cDISABLED; - perm moduleloader.modules.list; - run list; - } - list { - help Lists all modules. Color indicates status: §aENABLED §cDISABLED; - perm moduleloader.modules.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...] { - 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 moduleloader.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 moduleloader.modules.admin; - run unload name; - type console; - } -} \ No newline at end of file -- cgit v1.2.3