summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinenash <minenash@protonmail.com>2019-01-08 09:42:37 -0500
committerGitHub <noreply@github.com>2019-01-08 09:42:37 -0500
commitffac80e347d2f1474c0e60662e3d08cbab6e2978 (patch)
tree66571e33fb539a476ee4b3bea3cccee605b9f7e1
parent82fbf34f3ae12c59d27b516c476e938f33298e2d (diff)
parent259c4b8d376e93087a8fb025fadeea27821c9fab (diff)
Merge pull request #7 from RedstonerServer/descriptor
Added Descriptor Files and redid `/modules [list]`
-rw-r--r--src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd (renamed from src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd)10
-rw-r--r--src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java128
-rw-r--r--src/main/java/com/redstoner/misc/ModuleInfo.java81
-rw-r--r--src/main/java/com/redstoner/modules/ModuleLogger.java3
4 files changed, 159 insertions, 63 deletions
diff --git a/src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd
index eadeeb7..4e06bd0 100644
--- a/src/main/resources/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd
+++ b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.cmd
@@ -9,16 +9,6 @@ command modules {
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;
diff --git a/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java b/src/main/java/com/redstoner/coremods/moduleLoader/ModuleLoader.java
index 062bda2..109fc27 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,24 +28,25 @@ 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.
*
* @author Pepich */
-@Version(major = 4, minor = 0, revision = 1, compatible = 4)
+@Version(major = 5, minor = 0, revision = 0, compatible = 5)
public final class ModuleLoader implements CoreModule
{
private static ModuleLoader instance;
private static final HashMap<Module, Boolean> modules = new HashMap<>();
+ private static HashMap<Module, ModuleInfo> moduleInfos = new HashMap<>();
+ private static HashMap<String, List<Module>> categorizes = new HashMap<>();
private static URL[] urls;
private static URLClassLoader mainLoader;
private static HashMap<Module, URLClassLoader> 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,35 @@ public final class ModuleLoader implements CoreModule
{
try
{
- loggers.put(module, new ModuleLogger(module.getClass().getSimpleName()));
+ InputStream infoFile = null;
+
+ if (VersionHelper.isCompatible(VersionHelper.create(5, 0, 0, 5), module.getClass())) {
+ String basePath = "plugins/ModuleLoader/classes/" + module.getClass().getName().replace(".", "/");
+
+ try {
+ infoFile = new FileInputStream(
+ new File(basePath.substring(0, basePath.lastIndexOf('/')+1) + "module.info"));
+ }
+ catch(Exception e) {
+ infoFile = null;
+ }
+ }
+ ModuleInfo info = new ModuleInfo(infoFile, module);
+
+ moduleInfos.put(module, info);
+
+ String category = info.getCategory();
+ if (!categorizes.containsKey(category))
+ categorizes.put(category, new ArrayList<>(Arrays.asList(module)));
+ else {
+ List<Module> 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 +276,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 +323,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<Module> 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 +539,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 +633,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 +754,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/java/com/redstoner/modules/ModuleLogger.java b/src/main/java/com/redstoner/modules/ModuleLogger.java
index 8141976..11d71b6 100644
--- a/src/main/java/com/redstoner/modules/ModuleLogger.java
+++ b/src/main/java/com/redstoner/modules/ModuleLogger.java
@@ -13,6 +13,7 @@ public class ModuleLogger
{
public static final String PREFIX_WARN = "§8[§eWARN§8]:§7 ";
public static final String PREFIX_ERROR = "§8[§cERROR§8]:§7 ";
+ public static final String PREFIX_INFO = "§8[§fINFO§8]:§7 ";
private String name;
@@ -23,7 +24,7 @@ public class ModuleLogger
public void info(final String message)
{
- Bukkit.getConsoleSender().sendMessage(getPrefix() + ChatAPI.colorify(null, message));
+ Bukkit.getConsoleSender().sendMessage(PREFIX_INFO + getPrefix() + ChatAPI.colorify(null, message));
}
public void warn(final String message)