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>
<artifactId>Yum</artifactId>
<version>2.8.2</version>
<version>2.8.3</version>
<packaging>jar</packaging>
<description>Minecraft 服务器插件管理系统</description>
@ -16,9 +16,10 @@
</parent>
<properties>
<update.description>§a补丁包 2.8.2 版本</update.description>
<update.description>§a补丁包 2.8.3 版本</update.description>
<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-02-23 §cfix: 修复不兼容 1.13 的问题;
</update.changes>

View File

@ -32,9 +32,10 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.UnknownDependencyException;
import com.google.common.base.Joiner;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.kit.FileKit;
import pw.yumc.YumCore.kit.StrKit;
import pw.yumc.YumCore.reflect.Reflect;
/**
* 插件管理类
@ -203,7 +204,7 @@ public class PluginsManager {
/**
* 通过名称获得插件(处理带空格的插件)
*
* @param name
* @param args
* - 名称
* @return 插件
*/
@ -324,7 +325,8 @@ public class PluginsManager {
sender.sendMessage("§c服务器或JAVA的版本低于插件: " + name + " 所需要的版本!!");
return false;
} 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" + name + " 已载入到服务器!");
sender.sendMessage("§4注意: §c当前插件无法在运行时重载 请重启服务器!");
@ -392,7 +394,7 @@ public class PluginsManager {
pluginFile = file;
break;
}
} catch (InvalidDescriptionException e) {
} catch (InvalidDescriptionException ignored) {
}
}
}
@ -445,7 +447,7 @@ public class PluginsManager {
*
* @param sender
* - 命令发送者
* @param main
* @param name
* - 插件
* @return 是否成功
*/
@ -537,12 +539,12 @@ public class PluginsManager {
List<Plugin> plugins = null;
Map<String, Plugin> lookupNames = null;
Map<String, Command> knownCommands = null;
// Map<Pattern, JavaPluginLoader> fileAssociations = null;
if (pluginManager == null) {
sender.sendMessage("§4异常: §c插件管理类反射获取失败!");
return false;
}
try {
Class<? extends PluginManager> clazz = pluginManager.getClass();
Field pluginsField = pluginManager.getClass().getDeclaredField("plugins");
pluginsField.setAccessible(true);
plugins = (List<Plugin>) pluginsField.get(pluginManager);
@ -555,16 +557,10 @@ public class PluginsManager {
commandMapField.setAccessible(true);
commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
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);
knownCommands = Reflect.on(commandMap).field("knownCommands").get();
} catch (Exception e) {
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 §b" + name + " §c卸载失败!");
Log.d(e);
return false;
}
String pluginVersion = "";
@ -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的所有命令!");
ClassLoader cl = next.getClass().getClassLoader();
try {
((URLClassLoader) cl).close();
} catch (IOException ex) {
} catch (IOException ignored) {
}
System.gc();
}