卸载插件时移除类加载器...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-12-31 21:05:18 +08:00
parent 8f5d39ead0
commit 8887c6e291

View File

@ -14,6 +14,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -507,6 +509,7 @@ public class PluginsManager {
List<Plugin> plugins = null; List<Plugin> plugins = null;
Map<String, Plugin> lookupNames = null; Map<String, Plugin> lookupNames = null;
Map<String, Command> knownCommands = null; Map<String, Command> knownCommands = null;
Map<Pattern, PluginLoader> fileAssociations = null;
if (pluginManager == null) { if (pluginManager == null) {
sender.sendMessage("§4异常: §c插件管理类反射获取失败!"); sender.sendMessage("§4异常: §c插件管理类反射获取失败!");
return false; return false;
@ -524,9 +527,13 @@ public class PluginsManager {
commandMapField.setAccessible(true); commandMapField.setAccessible(true);
commandMap = (SimpleCommandMap) commandMapField.get(pluginManager); commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
final Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands"); final Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true); knownCommandsField.setAccessible(true);
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap); knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
final Field fileAssociationsField = pluginManager.getClass().getDeclaredField("fileAssociations");
fileAssociationsField.setAccessible(true);
fileAssociations = (Map<Pattern, PluginLoader>) fileAssociationsField.get(pluginManager);
} catch (final Exception e) { } catch (final Exception e) {
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 §b" + name + " §c卸载失败!"); sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 §b" + name + " §c卸载失败!");
return false; return false;
@ -564,6 +571,16 @@ public class PluginsManager {
((URLClassLoader) cl).close(); ((URLClassLoader) cl).close();
} catch (final IOException ex) { } catch (final IOException ex) {
} }
if (fileAssociations != null) {
for (final Iterator<Entry<Pattern, PluginLoader>> filter = fileAssociations.entrySet().iterator(); filter.hasNext();) {
final Entry<Pattern, PluginLoader> entry = filter.next();
final Matcher match = entry.getKey().matcher(getPluginFile(next).getName());
if (match.find()) {
filter.remove();
sender.sendMessage("§6卸载: §a移除插件 §b" + name + " §a的类加载器!");
}
}
}
System.gc(); System.gc();
} }
} }