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
4 changed files with 158 additions and 62 deletions

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,24 +28,25 @@ 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.
* *
* @author Pepich */ * @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 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,35 @@ public final class ModuleLoader implements CoreModule
{ {
try 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()) if (module.onEnable())
{ {
modules.put(module, true); modules.put(module, true);
@ -242,9 +276,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 +323,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)
{ {
boolean hasCategorys = hasCategories();
Message m = new Message(sender, null); Message m = new Message(sender, null);
m.appendText(getLogger().getHeader()); ModuleInfo ml_info = moduleInfos.get(instance);
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. m.appendText("§2--=[ ")
* .appendTextHover("§2" + ml_info.getDisplayName(), ml_info.getModuleInfoHover())
* @param sender The person to send the info to, usually the issuer of the command or the console sender. .appendText("§2 ]=--\nModules:\n");
* @return true. */
@Command(hook = "listv", async = AsyncType.ALWAYS) for (String cat: categorizes.keySet()) {
public boolean listModulesCommandVersion(CommandSender sender) if (hasCategorys)
{ m.appendText("\n&7" + cat + ":\n");
Message m = new Message(sender, null);
m.appendText(getLogger().getHeader()); int curModule = 1;
m.appendText("§2Modules:\n&e"); List<Module> mods = categorizes.get(cat);
Module[] modules = ModuleLoader.modules.keySet().toArray(new Module[] {}); for (Module mod : mods) {
for (int i = 0; i < modules.length; i++)
{ ModuleInfo info = moduleInfos.get(mod);
Module module = modules[i]; m.appendTextHover((modules.get(mod) ? "§a" : "§c") + info.getDisplayName(), info.getModuleInfoHover());
String[] classPath = module.getClass().getName().split("\\.");
String classname = classPath[classPath.length - 1]; if (curModule != mods.size())
try m.appendText("&7, ");
{ curModule++;
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 +539,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 +633,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;
} }
@ -738,4 +758,8 @@ public final class ModuleLoader implements CoreModule
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());
}
}

View File

@ -13,6 +13,7 @@ public class ModuleLogger
{ {
public static final String PREFIX_WARN = "§8[§eWARN§8]:§7 "; 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_ERROR = "§8[§cERROR§8]:§7 ";
public static final String PREFIX_INFO = "§8[§fINFO§8]:§7 ";
private String name; private String name;
@ -23,7 +24,7 @@ public class ModuleLogger
public void info(final String message) 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) public void warn(final String message)