0

Added Descriptor Files and redid /modules [list] #7

Merged
Minenash merged 4 commits from descriptor into gradle 2019-01-08 14:42:38 +00:00
3 changed files with 153 additions and 60 deletions
Showing only changes of commit f0713d781a - Show all commits

View File

@ -9,16 +9,6 @@ command modules {
perm moduleloader.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 moduleloader.modules.admin; perm moduleloader.modules.admin;

View File

@ -1,6 +1,7 @@
package com.redstoner.coremods.moduleLoader; package com.redstoner.coremods.moduleLoader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -9,6 +10,7 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -18,6 +20,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.Command; import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType; import com.nemez.cmdmgr.Command.AsyncType;
@ -25,15 +28,14 @@ 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.ModuleInfo;
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 com.redstoner.modules.ModuleLogger;
import net.nemez.chatapi.click.Message; 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. /** 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 ModuleLoader instance;
private static final HashMap<Module, Boolean> modules = new HashMap<>(); 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 URL[] urls;
private static URLClassLoader mainLoader; private static URLClassLoader mainLoader;
private static HashMap<Module, URLClassLoader> loaders = new HashMap<>(); private static HashMap<Module, URLClassLoader> loaders = new HashMap<>();
@ -71,7 +75,9 @@ public final class ModuleLoader implements CoreModule
{ {
if (instance == null) if (instance == null)
instance = new ModuleLoader(); 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, CommandManager.registerCommand(ModuleLoader.class.getResourceAsStream("ModuleLoader.cmd"), instance,
Main.plugin); Main.plugin);
} }
@ -234,7 +240,33 @@ public final class ModuleLoader implements CoreModule
{ {
try 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<Module> modsInCat = categorizes.get(category);
modsInCat.add(module);
categorizes.put(category, modsInCat);
}
loggers.put(module, new ModuleLogger(info.getDisplayName()));
if (module.onEnable()) if (module.onEnable())
{ {
modules.put(module, true); modules.put(module, true);
@ -242,9 +274,9 @@ public final class ModuleLoader implements CoreModule
module.firstLoad(); module.firstLoad();
else if (!VersionHelper.getVersion(module.getClass()).equals(VersionHelper.getString(oldVersion))) else if (!VersionHelper.getVersion(module.getClass()).equals(VersionHelper.getString(oldVersion)))
module.migrate(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(); 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); Commands ann = module.getClass().getAnnotation(Commands.class);
if (ann != null) if (ann != null)
@ -289,50 +321,31 @@ 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)
{ {
Message m = new Message(sender, null); boolean hasCategorys = hasCategories();
m.appendText(getLogger().getHeader()); Message m = new Message(sender, null);
m.appendText("§2Modules:\n&e"); ModuleInfo ml_info = moduleInfos.get(instance);
Module[] modules = ModuleLoader.modules.keySet().toArray(new Module[] {});
for (int i = 0; i < modules.length; i++) m.appendText("§2--=[ ")
{ .appendTextHover("§2" + ml_info.getDisplayName(), ml_info.getModuleInfoHover())
Module module = modules[i]; .appendText("§2 ]=--\nModules:\n");
String[] classPath = module.getClass().getName().split("\\.");
String classname = classPath[classPath.length - 1]; for (String cat: categorizes.keySet()) {
m.appendText((ModuleLoader.modules.get(module) ? "§a" : "§c") + classname); if (hasCategorys)
if (i + 1 < modules.length) m.appendText("\n&7" + cat + ":\n");
m.appendText("§7, ");
} int curModule = 1;
m.send(); List<Module> mods = categorizes.get(cat);
return true; for (Module mod : mods) {
}
ModuleInfo info = moduleInfos.get(mod);
/** This method lists all modules to the specified CommandSender. The modules will be color coded correspondingly to their enabled status. m.appendTextHover((modules.get(mod) ? "§a" : "§c") + info.getDisplayName(), info.getModuleInfoHover());
*
* @param sender The person to send the info to, usually the issuer of the command or the console sender. if (curModule != mods.size())
* @return true. */ m.appendText("&7, ");
@Command(hook = "listv", async = AsyncType.ALWAYS) curModule++;
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("\n");
{
m.appendText((ModuleLoader.modules.get(module) ? "§a" : "§c") + classname + "§c" + "(Unknown Version)");
}
if (i + 1 < modules.length)
m.appendText("§7, ");
} }
m.send(); m.send();
return true; return true;
@ -524,6 +537,9 @@ public final class ModuleLoader implements CoreModule
disableModule(m); disableModule(m);
instance.getLogger().info("Disabled module, overriding the implementation"); instance.getLogger().info("Disabled module, overriding the implementation");
modules.remove(m); modules.remove(m);
categorizes.get(moduleInfos.get(m).getCategory()).remove(m);
moduleInfos.remove(m);
try try
{ {
if (loaders.containsKey(m)) if (loaders.containsKey(m))
@ -615,6 +631,8 @@ public final class ModuleLoader implements CoreModule
instance.getLogger().info("Attempting to disable module properly:"); instance.getLogger().info("Attempting to disable module properly:");
disableModule(m); disableModule(m);
modules.remove(m); modules.remove(m);
categorizes.get(moduleInfos.get(m).getCategory()).remove(m);
moduleInfos.remove(m);
instance.getLogger().info("Disabled module."); instance.getLogger().info("Disabled module.");
return true; return true;
} }
@ -734,8 +752,12 @@ public final class ModuleLoader implements CoreModule
e.printStackTrace(); e.printStackTrace();
} }
} }
public static JavaPlugin getPlugin() { public static JavaPlugin getPlugin() {
return Main.plugin; return Main.plugin;
} }
public static boolean hasCategories() {
return !(categorizes.size() == 1 && categorizes.containsKey("Other"));
}
} }

View File

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