fix: knownCommands get error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-08-28 11:30:19 +08:00
parent 64686592a6
commit 9f54734414
2 changed files with 727 additions and 751 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>Yum</artifactId> <artifactId>Yum</artifactId>
<version>2.8.2</version> <version>2.8.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Minecraft 服务器插件管理系统</description> <description>Minecraft 服务器插件管理系统</description>
@ -16,9 +16,10 @@
</parent> </parent>
<properties> <properties>
<update.description>§a补丁包 2.8.2 版本</update.description> <update.description>§a补丁包 2.8.3 版本</update.description>
<update.changes> <update.changes>
§619-08-28 §cfix: async event on primary thread; §619-08-28 §cfix: knownCommands not compatible;
       §cfix: async event on primary thread;
§619-08-26 §cfix: 修复不兼容 1.14.4 的问题; §619-08-26 §cfix: 修复不兼容 1.14.4 的问题;
§619-02-23 §cfix: 修复不兼容 1.13 的问题; §619-02-23 §cfix: 修复不兼容 1.13 的问题;
</update.changes> </update.changes>

View File

@ -32,9 +32,10 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.UnknownDependencyException; import org.bukkit.plugin.UnknownDependencyException;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.kit.FileKit; import pw.yumc.YumCore.kit.FileKit;
import pw.yumc.YumCore.kit.StrKit; import pw.yumc.YumCore.kit.StrKit;
import pw.yumc.YumCore.reflect.Reflect;
/** /**
* 插件管理类 * 插件管理类
@ -203,7 +204,7 @@ public class PluginsManager {
/** /**
* 通过名称获得插件(处理带空格的插件) * 通过名称获得插件(处理带空格的插件)
* *
* @param name * @param args
* - 名称 * - 名称
* @return 插件 * @return 插件
*/ */
@ -324,7 +325,8 @@ public class PluginsManager {
sender.sendMessage("§c服务器或JAVA的版本低于插件: " + name + " 所需要的版本!!"); sender.sendMessage("§c服务器或JAVA的版本低于插件: " + name + " 所需要的版本!!");
return false; return false;
} catch (InvalidPluginException e) { } catch (InvalidPluginException e) {
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) { if ("Plugin already initialized!".equalsIgnoreCase(e.getMessage()) ||
"java.lang.IllegalArgumentException: Plugin already initialized!".equals(e.getMessage())) {
sender.sendMessage("§4异常: §c" + e.getMessage()); sender.sendMessage("§4异常: §c" + e.getMessage());
sender.sendMessage("§4插件: §c" + name + " 已载入到服务器!"); sender.sendMessage("§4插件: §c" + name + " 已载入到服务器!");
sender.sendMessage("§4注意: §c当前插件无法在运行时重载 请重启服务器!"); sender.sendMessage("§4注意: §c当前插件无法在运行时重载 请重启服务器!");
@ -392,7 +394,7 @@ public class PluginsManager {
pluginFile = file; pluginFile = file;
break; break;
} }
} catch (InvalidDescriptionException e) { } catch (InvalidDescriptionException ignored) {
} }
} }
} }
@ -445,7 +447,7 @@ public class PluginsManager {
* *
* @param sender * @param sender
* - 命令发送者 * - 命令发送者
* @param main * @param name
* - 插件 * - 插件
* @return 是否成功 * @return 是否成功
*/ */
@ -537,12 +539,12 @@ 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, JavaPluginLoader> fileAssociations = null;
if (pluginManager == null) { if (pluginManager == null) {
sender.sendMessage("§4异常: §c插件管理类反射获取失败!"); sender.sendMessage("§4异常: §c插件管理类反射获取失败!");
return false; return false;
} }
try { try {
Class<? extends PluginManager> clazz = pluginManager.getClass();
Field pluginsField = pluginManager.getClass().getDeclaredField("plugins"); Field pluginsField = pluginManager.getClass().getDeclaredField("plugins");
pluginsField.setAccessible(true); pluginsField.setAccessible(true);
plugins = (List<Plugin>) pluginsField.get(pluginManager); plugins = (List<Plugin>) pluginsField.get(pluginManager);
@ -555,16 +557,10 @@ public class PluginsManager {
commandMapField.setAccessible(true); commandMapField.setAccessible(true);
commandMap = (SimpleCommandMap) commandMapField.get(pluginManager); commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands"); knownCommands = Reflect.on(commandMap).field("knownCommands").get();
knownCommandsField.setAccessible(true);
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
// Field fileAssociationsField = pluginManager.getClass().getDeclaredField("fileAssociations");
// fileAssociationsField.setAccessible(true);
// fileAssociations = (Map<Pattern, JavaPluginLoader>) fileAssociationsField.get(pluginManager);
} catch (Exception e) { } catch (Exception e) {
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 §b" + name + " §c卸载失败!"); sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 §b" + name + " §c卸载失败!");
Log.d(e);
return false; return false;
} }
String pluginVersion = ""; String pluginVersion = "";
@ -582,7 +578,7 @@ public class PluginsManager {
sender.sendMessage("§6卸载: §a从插件查找列表删除 §b" + name + " §a的实例!"); sender.sendMessage("§6卸载: §a从插件查找列表删除 §b" + name + " §a的实例!");
} }
for (Iterator<Map.Entry<String, Command>> it = knownCommands.entrySet().iterator(); it.hasNext();) { for (Iterator<Map.Entry<String, Command>> it = knownCommands.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Command> entry = it.next(); Map.Entry<String, Command> entry = it.next();
if ((entry.getValue() instanceof PluginCommand)) { if ((entry.getValue() instanceof PluginCommand)) {
PluginCommand command = (PluginCommand) entry.getValue(); PluginCommand command = (PluginCommand) entry.getValue();
@ -592,33 +588,12 @@ public class PluginsManager {
} }
} }
} }
// try {
// if (fileAssociations != null) {
// for (Entry<Pattern, JavaPluginLoader> entry : fileAssociations.entrySet()) {
// Matcher match = entry.getKey().matcher(getPluginFile(next).getName());
// if (match.find()) {
// PluginLoader pluginLoader = entry.getValue();
// if (pluginLoader instanceof YumPluginLoader) {
// pluginLoader = ((YumPluginLoader) pluginLoader).internal_loader;
// }
// Field loadersField = pluginLoader.getClass().getDeclaredField("loaders");
// loadersField.setAccessible(true);
// Map<String, URLClassLoader> loaders = (Map<String, URLClassLoader>) loadersField.get(pluginLoader);
// for (Entry<String, URLClassLoader> entry2 : loaders.entrySet()) {
// Reflect.on(entry2.getValue()).set("pluginInit", null).set("plugin", null);
// }
// sender.sendMessage("§6卸载: §a移除插件 §b" + name + " §a的类实例缓存!");
// }
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
sender.sendMessage("§6卸载: §a注销插件 §b" + name + " §a的所有命令!"); sender.sendMessage("§6卸载: §a注销插件 §b" + name + " §a的所有命令!");
ClassLoader cl = next.getClass().getClassLoader(); ClassLoader cl = next.getClass().getClassLoader();
try { try {
((URLClassLoader) cl).close(); ((URLClassLoader) cl).close();
} catch (IOException ex) { } catch (IOException ignored) {
} }
System.gc(); System.gc();
} }