change "unload" method

dev
sinvo 2015-08-29 14:37:44 +08:00
parent 129549b6c9
commit e0bd34fae7
1 changed files with 66 additions and 40 deletions

View File

@ -106,7 +106,8 @@ public class PluginsManager {
ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED; ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED;
String pluginName = color + plugin.getName(); String pluginName = color + plugin.getName();
if (includeVersions) { if (includeVersions) {
pluginName = pluginName + " (" + plugin.getDescription().getVersion() + ")"; pluginName = pluginName + " ("
+ plugin.getDescription().getVersion() + ")";
} }
return pluginName; return pluginName;
} }
@ -134,7 +135,8 @@ public class PluginsManager {
public static List<String> getPluginNames(boolean fullName) { public static List<String> getPluginNames(boolean fullName) {
List<String> plugins = new ArrayList<String>(); List<String> plugins = new ArrayList<String>();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName()); plugins.add(fullName ? plugin.getDescription().getFullName()
: plugin.getName());
} }
return plugins; return plugins;
} }
@ -149,12 +151,15 @@ public class PluginsManager {
public static String getUsages(Plugin plugin) { public static String getUsages(Plugin plugin) {
List<String> parsedCommands = new ArrayList<String>(); List<String> parsedCommands = new ArrayList<String>();
Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands(); Map<String, Map<String, Object>> commands = plugin.getDescription()
.getCommands();
if (commands != null) { if (commands != null) {
Iterator<Entry<String, Map<String, Object>>> commandsIt = commands.entrySet().iterator(); Iterator<Entry<String, Map<String, Object>>> commandsIt = commands
.entrySet().iterator();
while (commandsIt.hasNext()) { while (commandsIt.hasNext()) {
Entry<String, Map<String, Object>> thisEntry = commandsIt.next(); Entry<String, Map<String, Object>> thisEntry = commandsIt
.next();
if (thisEntry != null) { if (thisEntry != null) {
parsedCommands.add(thisEntry.getKey()); parsedCommands.add(thisEntry.getKey());
} }
@ -228,13 +233,16 @@ public class PluginsManager {
try { try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile); target = Bukkit.getPluginManager().loadPlugin(pluginFile);
} catch (InvalidDescriptionException e) { } catch (InvalidDescriptionException e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + " 的plugin.yml文件存在错误!"); sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name
+ " 的plugin.yml文件存在错误!");
return false; return false;
} catch (InvalidPluginException e) { } catch (InvalidPluginException e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 文件: " + name + " 不是一个可载入的插件!"); sender.sendMessage("§c异常: " + e.getMessage() + " 文件: " + name
+ " 不是一个可载入的插件!");
return false; return false;
} catch (UnknownDependencyException e) { } catch (UnknownDependencyException e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + " 缺少部分依赖项目!"); sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name
+ " 缺少部分依赖项目!");
return false; return false;
} }
@ -280,64 +288,83 @@ public class PluginsManager {
PluginManager pluginManager = Bukkit.getPluginManager(); PluginManager pluginManager = Bukkit.getPluginManager();
SimpleCommandMap commandMap = null; SimpleCommandMap commandMap = null;
List<Plugin> plugins = null; List<Plugin> plugins = null;
Map<String, Plugin> names = null; Map<String, Plugin> lookupNames = null;
Map<String, Command> commands = null; Map<String, Command> knownCommands = null;
Map<Event, SortedSet<RegisteredListener>> listeners = null; Map<Event, SortedSet<RegisteredListener>> listeners = null;
boolean reloadlisteners = true; boolean reloadlisteners = true;
if (pluginManager != null) { if (pluginManager != null) {
try { try {
Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins"); Field pluginsField = Bukkit.getPluginManager().getClass()
.getDeclaredField("plugins");
pluginsField.setAccessible(true); pluginsField.setAccessible(true);
plugins = (List<Plugin>) pluginsField.get(pluginManager); plugins = (List<Plugin>) pluginsField.get(pluginManager);
Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames"); Field lookupNamesField = Bukkit.getPluginManager().getClass()
.getDeclaredField("lookupNames");
lookupNamesField.setAccessible(true); lookupNamesField.setAccessible(true);
names = (Map<String, Plugin>) lookupNamesField.get(pluginManager); lookupNames = (Map<String, Plugin>) lookupNamesField
.get(pluginManager);
try { try {
Field listenersField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners"); Field listenersField = Bukkit.getPluginManager().getClass()
.getDeclaredField("listeners");
listenersField.setAccessible(true); listenersField.setAccessible(true);
listeners = (Map<Event, SortedSet<RegisteredListener>>) listenersField.get(pluginManager); listeners = (Map<Event, SortedSet<RegisteredListener>>) listenersField
.get(pluginManager);
} catch (Exception e) { } catch (Exception e) {
reloadlisteners = false; reloadlisteners = false;
} }
Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); Field commandMapField = Bukkit.getPluginManager().getClass()
.getDeclaredField("commandMap");
commandMapField.setAccessible(true); 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); knownCommandsField.setAccessible(true);
commands = (Map<String, Command>) knownCommandsField.get(commandMap); knownCommands = (Map<String, Command>) knownCommandsField
.get(commandMap);
} catch (Exception e) { } catch (Exception e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 插件 " + name + " 卸载失败!"); sender.sendMessage("§c异常: " + e.getMessage() + " 插件 " + name
+ " 卸载失败!");
return false; return false;
} }
} }
pluginManager.disablePlugin(plugin); for (Plugin next : pluginManager.getPlugins()) {
if (plugins != null && plugins.contains(plugin)) { if (next.getName().equals(name)) {
plugins.remove(plugin); pluginManager.disablePlugin(next);
} if ((plugins != null) && (plugins.contains(next))) {
if (names != null && names.containsKey(name)) { plugins.remove(next);
names.remove(name); }
if ((lookupNames != null) && (lookupNames.containsKey(name))) {
lookupNames.remove(name);
}
if (commandMap != null)
for (Iterator<Map.Entry<String, Command>> it = knownCommands
.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Command> 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) { if (listeners != null && reloadlisteners) {
for (SortedSet<RegisteredListener> set : listeners.values()) { for (SortedSet<RegisteredListener> set : listeners.values()) {
for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext();) { for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext();) {
RegisteredListener value = it.next(); RegisteredListener value = it.next();
if (value.getPlugin() == plugin) { if (value.getPlugin().getName().equals(name)) {
it.remove();
}
}
}
}
if (commandMap != null) {
for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Command> entry = it.next();
if (entry.getValue() instanceof PluginCommand) {
PluginCommand c = (PluginCommand) entry.getValue();
if (c.getPlugin() == plugin) {
c.unregister(commandMap);
it.remove(); it.remove();
} }
} }
@ -350,7 +377,6 @@ public class PluginsManager {
} catch (IOException ex) { } catch (IOException ex) {
} }
} }
System.gc();
sender.sendMessage("§6卸载: §a插件: " + name + " 已成功卸载!"); sender.sendMessage("§6卸载: §a插件: " + name + " 已成功卸载!");
return true; return true;
} }