From 5570d82bed6fcab955c24def62bc956e3abbd74b Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 6 Sep 2015 23:55:37 +0800 Subject: [PATCH] fix upgrade command ... Signed-off-by: j502647092 --- .../Yum/commands/CommandHandler.java | 22 +++++----- .../Yum/commands/CommandUpgrade.java | 8 ++-- .../citycraft/Yum/manager/PluginsManager.java | 40 ++++++++++++++----- .../cn/citycraft/Yum/manager/YumManager.java | 20 ---------- 4 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java b/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java index 67646a1..78493d9 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java @@ -71,6 +71,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter { registerCommand(new CommandReload(yum)); registerCommand(new CommandLoad(yum)); registerCommand(new CommandUnload(yum)); + registerCommand(new CommandUpgrade(yum)); RegisterCommandList = getRegisterCommands(); } @@ -82,9 +83,8 @@ public class CommandHandler implements CommandExecutor, TabCompleter { */ public List getRegisterCommands() { List cmds = new ArrayList(); - for (BaseCommand command : commandlist) { + for (BaseCommand command : commandlist) cmds.addAll(command.getCommandList()); - } return cmds; } @@ -94,7 +94,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter { return true; String subcmd = args[0]; String[] subargs = moveStrings(args, 1); - for (BaseCommand command : commandlist) { + for (BaseCommand command : commandlist) if (command.isValidTrigger(subcmd)) { if (!command.hasPermission(sender)) { sender.sendMessage("你没有此命令的权限!"); @@ -104,18 +104,16 @@ public class CommandHandler implements CommandExecutor, TabCompleter { sender.sendMessage("控制台无法使用此命令!"); return true; } - if (subargs.length >= command.getMinimumArguments()) { + if (subargs.length >= command.getMinimumArguments()) try { command.execute(sender, subcmd, subargs); return true; } catch (CommandException e) { sender.sendMessage(e.getMessage()); } - } else { + else sender.sendMessage("错误的参数 /yum " + command.getName() + command.getPossibleArguments()); - } } - } return false; } @@ -134,12 +132,10 @@ public class CommandHandler implements CommandExecutor, TabCompleter { if (args[0].equalsIgnoreCase("install")) plugins = YumManager.repo.getAllPluginName(); else if (args[0].equalsIgnoreCase("repo")) - plugins = Arrays.asList(new String[] { - "add", - "list", - "clean", - "update" - }); + plugins = Arrays.asList(new String[] { "add", + "list", + "clean", + "update" }); else plugins = YumManager.plugman.getPluginNames(false); StringUtil.copyPartialMatches(partialPlugin, plugins, completions); diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java b/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java index 9d45c26..43e1ff5 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java @@ -29,13 +29,13 @@ public class CommandUpgrade extends BaseCommand { public void execute(final CommandSender sender, String label, final String[] args) throws CommandException { final String pluginname = args[0]; final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); - sender.sendMessage("§a开始更新插件: " + pluginname); + sender.sendMessage("§a开始升级插件: " + pluginname); if (plugin != null) Bukkit.getScheduler().runTaskAsynchronously(main, () -> { - if (args.length < 2) - YumManager.upgrade(sender, plugin); + if (args.length == 1) + YumManager.plugman.upgrade(sender, plugin); else - YumManager.upgrade(sender, plugin, args[1]); + YumManager.plugman.upgrade(sender); }); else sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!"); diff --git a/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java b/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java index a512c27..03a47af 100644 --- a/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java @@ -1,6 +1,7 @@ package cn.citycraft.Yum.manager; import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; import java.net.URL; import java.net.URLClassLoader; @@ -485,8 +486,9 @@ public class PluginsManager { * * @return 是否成功 */ - public boolean updateall(CommandSender sender) { - return updateall(sender, Bukkit.getServer().getUpdateFolderFile()); + public boolean upgrade(CommandSender sender) { + sender.sendMessage("§6升级: §a开始升级 服务器更新 目录下的所有插件!"); + return upgrade(sender, null, null); } /** @@ -498,27 +500,36 @@ public class PluginsManager { * - 更新目录 * @return 是否成功 */ - public boolean updateall(CommandSender sender, File directory) { + public boolean upgrade(CommandSender sender, File directory, Plugin plugin) { + boolean result = false; PluginLoader loader = main.getPluginLoader(); File updateDirectory; - if (!directory.isDirectory()) + if (directory == null || !directory.isDirectory()) updateDirectory = Bukkit.getServer().getUpdateFolderFile(); else updateDirectory = directory; + try { + sender.sendMessage("§6升级: §b从 " + updateDirectory.getCanonicalPath() + " 文件夹检索插件插件!"); + } catch (SecurityException | IOException e1) { + sender.sendMessage("§4异常: §c文件夹 " + updateDirectory.getName() + " 权限不足或IO错误!"); + } for (File file : updateDirectory.listFiles()) { PluginDescriptionFile description = null; try { description = loader.getPluginDescription(file); String name = description.getName(); - sender.sendMessage("§6升级: 开始升级 " + name + " 插件!"); + if (plugin != null && name != plugin.getName()) + continue; + result = true; + sender.sendMessage("§6升级: §a开始升级 " + name + " 插件!"); reload(sender, name); } catch (InvalidDescriptionException e) { sender.sendMessage("§4异常: §c" + e.getMessage()); - sender.sendMessage("§c文件: " + file.getName() + " 的plugin.yml文件存在错误!"); + sender.sendMessage("§4文件: §c" + file.getName() + " 的plugin.yml文件存在错误!"); continue; } } - return false; + return result; } /** @@ -526,7 +537,18 @@ public class PluginsManager { * * @return 是否成功 */ - public boolean updateall(File directory) { - return updateall(Bukkit.getConsoleSender(), directory); + public boolean upgrade(CommandSender sender, Plugin plugin) { + sender.sendMessage("§6升级: §a开始升级 " + plugin.getName() + " 插件!"); + return upgrade(sender, null, plugin); + } + + /** + * 重载update文件夹的插件 + * + * @return 是否成功 + */ + public boolean upgrade(File directory) { + Bukkit.getConsoleSender().sendMessage("§6升级: §a开始升级 " + directory.getName() + " 目录下的所有插件!"); + return upgrade(Bukkit.getConsoleSender(), directory, null); } } diff --git a/src/main/java/cn/citycraft/Yum/manager/YumManager.java b/src/main/java/cn/citycraft/Yum/manager/YumManager.java index 7013fc5..b7ea6bd 100644 --- a/src/main/java/cn/citycraft/Yum/manager/YumManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/YumManager.java @@ -55,24 +55,4 @@ public class YumManager { sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName()); return false; } - - public static boolean upgrade(CommandSender sender) { - return plugman.updateall(sender); - } - - public static boolean upgrade(CommandSender sender, Plugin plugin) { - return update(sender, plugin, null); - } - - public static boolean upgrade(CommandSender sender, Plugin plugin, String version) { - PluginInfo pi = repo.getPlugin(plugin.getName()); - if (pi != null) { - if (download.run(sender, pi.getMavenUrl(version), plugman.getPluginFile(plugin))) - return plugman.reload(sender, plugin); - return false; - } else { - sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName()); - return false; - } - } }