feat: 调整载入插件的异常判断

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-08-05 10:21:54 +08:00
parent 4a4cb932d7
commit 56131b1274
2 changed files with 67 additions and 43 deletions

View File

@ -47,9 +47,13 @@ public class YumPluginLoader implements PluginLoader {
replaceJavaPluginLoaders(yumPluginLoader);
}
private static void injectExistingPlugins(final YumPluginLoader yumPluginLoader) {
for (final org.bukkit.plugin.Plugin p : Bukkit.getPluginManager().getPlugins()) {
if (p instanceof JavaPlugin) {
public static void inject(final String pname) {
injectExistingPlugin(Bukkit.getPluginManager().getPlugin(pname), yumPluginLoader);
replaceJavaPluginLoaders(yumPluginLoader);
}
private static void injectExistingPlugin(final Plugin p, final YumPluginLoader yumPluginLoader) {
if (p != null && p instanceof JavaPlugin) {
final JavaPlugin jp = (JavaPlugin) p;
try {
final Field f = JavaPlugin.class.getDeclaredField("loader");
@ -60,6 +64,11 @@ public class YumPluginLoader implements PluginLoader {
}
}
}
private static void injectExistingPlugins(final YumPluginLoader yumPluginLoader) {
for (final org.bukkit.plugin.Plugin p : Bukkit.getPluginManager().getPlugins()) {
injectExistingPlugin(p, yumPluginLoader);
}
}
private static void replaceJavaPluginLoaders(final YumPluginLoader yumPluginLoader) {
@ -75,10 +84,8 @@ public class YumPluginLoader implements PluginLoader {
final Iterator<Map.Entry<Pattern, PluginLoader>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
final Entry<Pattern, PluginLoader> entry = iter.next();
if (entry.getValue() instanceof JavaPluginLoader) {
entry.setValue(yumPluginLoader);
}
}
field.set(spm, map);
} catch (final Exception e) {
Bukkit.getServer().getLogger().log(Level.SEVERE, "Yum failed replacing the existing PluginLoader, contact the developers on YUMC!", e);

View File

@ -324,24 +324,34 @@ public class PluginsManager {
public boolean load(final CommandSender sender, final File pluginFile, final boolean clean) {
Plugin target = null;
final String name = pluginFile.getName();
if (clean) {
YumPluginLoader.inject();
}
String pname = name;
try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
try {
try {
final PluginDescriptionFile pluginDescriptionFile = main.getPluginLoader().getPluginDescription(pluginFile);
pname = pluginDescriptionFile.getName();
} catch (final InvalidDescriptionException e) {
sender.sendMessage("§4异常: §c" + e.getMessage());
sender.sendMessage("§4插件: §c" + name + " 的 plugin.yml 文件存在错误!");
return false;
}
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
} catch (final UnsupportedClassVersionError e) {
sender.sendMessage("§4异常: §c" + e.getMessage());
sender.sendMessage("§c服务器或JAVA的版本低于插件: " + name + " 所需要的版本!!");
return false;
} catch (final InvalidPluginException e) {
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) {
if (!clean) {
YumPluginLoader.inject(pname);
return load(sender, pluginFile, true);
}
sender.sendMessage("§4异常: §c" + e.getMessage());
sender.sendMessage("§4插件: §c" + name + " 已载入到服务器!");
sender.sendMessage("§4注意: §c当前插件无法在运行时重载 请重启服务器!");
return false;
}
sender.sendMessage("§4异常: §c" + e.getMessage());
sender.sendMessage("§4文件: §c" + name + " 不是一个可载入的插件!");
sender.sendMessage("§4注意: §cMOD服重载插件3次以上需重启服务器");
return false;
@ -358,6 +368,13 @@ public class PluginsManager {
Bukkit.getPluginManager().enablePlugin(target);
sender.sendMessage("§6载入: §a插件 §b" + target.getName() + " §a版本 §d" + getVersion(target) + " §a已成功载入到服务器!");
return true;
} catch (final Throwable e) {
sender.sendMessage("§4错误: §c" + e.getClass().getName() + ": " + e.getMessage());
sender.sendMessage("§4异常: §c具体信息请查看后台异常堆栈!");
e.printStackTrace();
sender.sendMessage("§4载入: §c插件 §b" + target.getName() + " §c版本 §d" + getVersion(target) + " §c载入失败!");
return false;
}
}
/**