From e0bd34fae7973f7dee08d3df4c0be7a95e87c9e1 Mon Sep 17 00:00:00 2001 From: sinvo Date: Sat, 29 Aug 2015 14:37:44 +0800 Subject: [PATCH] change "unload" method --- .../citycraft/Yum/utils/PluginsManager.java | 106 +++++++++++------- 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/src/main/java/cn/citycraft/Yum/utils/PluginsManager.java b/src/main/java/cn/citycraft/Yum/utils/PluginsManager.java index 94dc7c9..5668af8 100644 --- a/src/main/java/cn/citycraft/Yum/utils/PluginsManager.java +++ b/src/main/java/cn/citycraft/Yum/utils/PluginsManager.java @@ -106,7 +106,8 @@ public class PluginsManager { ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED; String pluginName = color + plugin.getName(); if (includeVersions) { - pluginName = pluginName + " (" + plugin.getDescription().getVersion() + ")"; + pluginName = pluginName + " (" + + plugin.getDescription().getVersion() + ")"; } return pluginName; } @@ -134,7 +135,8 @@ public class PluginsManager { public static List getPluginNames(boolean fullName) { List plugins = new ArrayList(); for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName()); + plugins.add(fullName ? plugin.getDescription().getFullName() + : plugin.getName()); } return plugins; } @@ -149,12 +151,15 @@ public class PluginsManager { public static String getUsages(Plugin plugin) { List parsedCommands = new ArrayList(); - Map> commands = plugin.getDescription().getCommands(); + Map> commands = plugin.getDescription() + .getCommands(); if (commands != null) { - Iterator>> commandsIt = commands.entrySet().iterator(); + Iterator>> commandsIt = commands + .entrySet().iterator(); while (commandsIt.hasNext()) { - Entry> thisEntry = commandsIt.next(); + Entry> thisEntry = commandsIt + .next(); if (thisEntry != null) { parsedCommands.add(thisEntry.getKey()); } @@ -228,13 +233,16 @@ public class PluginsManager { try { target = Bukkit.getPluginManager().loadPlugin(pluginFile); } catch (InvalidDescriptionException e) { - sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + " 的plugin.yml文件存在错误!"); + sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + + " 的plugin.yml文件存在错误!"); return false; } catch (InvalidPluginException e) { - sender.sendMessage("§c异常: " + e.getMessage() + " 文件: " + name + " 不是一个可载入的插件!"); + sender.sendMessage("§c异常: " + e.getMessage() + " 文件: " + name + + " 不是一个可载入的插件!"); return false; } catch (UnknownDependencyException e) { - sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + " 缺少部分依赖项目!"); + sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + + " 缺少部分依赖项目!"); return false; } @@ -280,64 +288,83 @@ public class PluginsManager { PluginManager pluginManager = Bukkit.getPluginManager(); SimpleCommandMap commandMap = null; List plugins = null; - Map names = null; - Map commands = null; + Map lookupNames = null; + Map knownCommands = null; Map> listeners = null; boolean reloadlisteners = true; if (pluginManager != null) { try { - Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins"); + Field pluginsField = Bukkit.getPluginManager().getClass() + .getDeclaredField("plugins"); pluginsField.setAccessible(true); plugins = (List) pluginsField.get(pluginManager); - Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames"); + Field lookupNamesField = Bukkit.getPluginManager().getClass() + .getDeclaredField("lookupNames"); lookupNamesField.setAccessible(true); - names = (Map) lookupNamesField.get(pluginManager); + lookupNames = (Map) lookupNamesField + .get(pluginManager); try { - Field listenersField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners"); + Field listenersField = Bukkit.getPluginManager().getClass() + .getDeclaredField("listeners"); listenersField.setAccessible(true); - listeners = (Map>) listenersField.get(pluginManager); + listeners = (Map>) listenersField + .get(pluginManager); } catch (Exception e) { reloadlisteners = false; } - Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); + Field commandMapField = Bukkit.getPluginManager().getClass() + .getDeclaredField("commandMap"); commandMapField.setAccessible(true); - commandMap = (SimpleCommandMap) commandMapField.get(pluginManager); + commandMap = (SimpleCommandMap) commandMapField + .get(pluginManager); - Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands"); + Field knownCommandsField = SimpleCommandMap.class + .getDeclaredField("knownCommands"); knownCommandsField.setAccessible(true); - commands = (Map) knownCommandsField.get(commandMap); + knownCommands = (Map) knownCommandsField + .get(commandMap); } catch (Exception e) { - sender.sendMessage("§c异常: " + e.getMessage() + " 插件 " + name + " 卸载失败!"); + sender.sendMessage("§c异常: " + e.getMessage() + " 插件 " + name + + " 卸载失败!"); return false; } } - pluginManager.disablePlugin(plugin); - if (plugins != null && plugins.contains(plugin)) { - plugins.remove(plugin); - } - if (names != null && names.containsKey(name)) { - names.remove(name); + for (Plugin next : pluginManager.getPlugins()) { + if (next.getName().equals(name)) { + pluginManager.disablePlugin(next); + if ((plugins != null) && (plugins.contains(next))) { + plugins.remove(next); + } + + if ((lookupNames != null) && (lookupNames.containsKey(name))) { + lookupNames.remove(name); + } + + if (commandMap != null) + for (Iterator> it = knownCommands + .entrySet().iterator(); it.hasNext();) { + Map.Entry entry = it.next(); + + if ((entry.getValue() instanceof PluginCommand)) { + PluginCommand command = (PluginCommand) entry + .getValue(); + + if (command.getPlugin() == next) { + command.unregister(commandMap); + it.remove(); + } + } + } + } } if (listeners != null && reloadlisteners) { for (SortedSet set : listeners.values()) { for (Iterator it = set.iterator(); it.hasNext();) { RegisteredListener value = it.next(); - if (value.getPlugin() == plugin) { - it.remove(); - } - } - } - } - if (commandMap != null) { - for (Iterator> it = commands.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = it.next(); - if (entry.getValue() instanceof PluginCommand) { - PluginCommand c = (PluginCommand) entry.getValue(); - if (c.getPlugin() == plugin) { - c.unregister(commandMap); + if (value.getPlugin().getName().equals(name)) { it.remove(); } } @@ -350,7 +377,6 @@ public class PluginsManager { } catch (IOException ex) { } } - System.gc(); sender.sendMessage("§6卸载: §a插件: " + name + " 已成功卸载!"); return true; }