From 2ecb988e50343950e09917fdd0e8cb648730f14a Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sun, 6 Sep 2015 21:06:44 +0800 Subject: [PATCH] add Upgrade command... Signed-off-by: 502647092 --- .../Yum/commands/CommandUpgrade.java | 58 ++++++++++++ .../citycraft/Yum/manager/PluginsManager.java | 89 ++++++++++++++++++- .../cn/citycraft/Yum/manager/YumManager.java | 35 ++++++-- 3 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java b/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java new file mode 100644 index 0000000..9d45c26 --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java @@ -0,0 +1,58 @@ +/** + * + */ +package cn.citycraft.Yum.commands; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; + +import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; + +/** + * @author 蒋天蓓 2015年8月12日下午2:04:05 + */ +public class CommandUpgrade extends BaseCommand { + Yum main; + + /** + * @param name + */ + public CommandUpgrade(Yum main) { + super("update"); + this.main = main; + } + + @Override + 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); + if (plugin != null) + Bukkit.getScheduler().runTaskAsynchronously(main, () -> { + if (args.length < 2) + YumManager.upgrade(sender, plugin); + else + YumManager.upgrade(sender, plugin, args[1]); + }); + else + sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!"); + }; + + @Override + public int getMinimumArguments() { + return 1; + } + + @Override + public String getPossibleArguments() { + return "<插件名称> <插件版本>"; + } + + @Override + public boolean isOnlyPlayerExecutable() { + return false; + } +} diff --git a/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java b/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java index 0a03512..a512c27 100644 --- a/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java @@ -20,6 +20,7 @@ import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidPluginException; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.UnknownDependencyException; @@ -312,6 +313,13 @@ public class PluginsManager { return true; } + /** + * 载入插件 + * + * @param name + * - 插件名称 + * @return 是否成功 + */ public boolean load(String name) { return load(Bukkit.getConsoleSender(), name); } @@ -331,6 +339,21 @@ public class PluginsManager { return false; } + /** + * 删除重载插件 + * + * @param sender + * - 命令发送者 + * @param plugin + * - 插件 + * @return 是否成功 + */ + public boolean reload(CommandSender sender, String name) { + if (name != null) + return unload(sender, name) && load(sender, name); + return false; + } + /** * 重载插件 * @@ -369,9 +392,21 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - @SuppressWarnings("unchecked") public boolean unload(CommandSender sender, Plugin plugin) { - String name = plugin.getName(); + return unload(sender, plugin.getName()); + } + + /** + * 卸载插件 + * + * @param sender + * - 命令发送者 + * @param name + * - 插件名称 + * @return 是否成功 + */ + @SuppressWarnings("unchecked") + public boolean unload(CommandSender sender, String name) { if (sender == null) sender = Bukkit.getConsoleSender(); PluginManager pluginManager = Bukkit.getPluginManager(); @@ -444,4 +479,54 @@ public class PluginsManager { public boolean unload(Plugin plugin) { return unload(Bukkit.getConsoleSender(), plugin); } + + /** + * 重载update文件夹的插件 + * + * @return 是否成功 + */ + public boolean updateall(CommandSender sender) { + return updateall(sender, Bukkit.getServer().getUpdateFolderFile()); + } + + /** + * 重载update文件夹的插件 + * + * @param sender + * - 命令发送者 + * @param directory + * - 更新目录 + * @return 是否成功 + */ + public boolean updateall(CommandSender sender, File directory) { + PluginLoader loader = main.getPluginLoader(); + File updateDirectory; + if (!directory.isDirectory()) + updateDirectory = Bukkit.getServer().getUpdateFolderFile(); + else + updateDirectory = directory; + for (File file : updateDirectory.listFiles()) { + PluginDescriptionFile description = null; + try { + description = loader.getPluginDescription(file); + String name = description.getName(); + sender.sendMessage("§6升级: 开始升级 " + name + " 插件!"); + reload(sender, name); + } catch (InvalidDescriptionException e) { + sender.sendMessage("§4异常: §c" + e.getMessage()); + sender.sendMessage("§c文件: " + file.getName() + " 的plugin.yml文件存在错误!"); + continue; + } + } + return false; + } + + /** + * 重载update文件夹的插件 + * + * @return 是否成功 + */ + public boolean updateall(File directory) { + return updateall(Bukkit.getConsoleSender(), directory); + } } diff --git a/src/main/java/cn/citycraft/Yum/manager/YumManager.java b/src/main/java/cn/citycraft/Yum/manager/YumManager.java index 4dfc6c5..7013fc5 100644 --- a/src/main/java/cn/citycraft/Yum/manager/YumManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/YumManager.java @@ -1,5 +1,8 @@ package cn.citycraft.Yum.manager; +import java.io.File; + +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; @@ -7,16 +10,17 @@ import cn.citycraft.Yum.repository.PluginInfo; /** * 自动更新类 - * + * * @author 蒋天蓓 * 2015年9月1日上午10:59:47 */ public class YumManager { - Plugin plugin; public static DownloadManager download; public static PluginsManager plugman; public static RepositoryManager repo; + Plugin plugin; + public YumManager(Plugin plugin) { this.plugin = plugin; plugman = new PluginsManager(plugin); @@ -31,9 +35,8 @@ public class YumManager { public static boolean install(CommandSender sender, String pluginname, String version) { PluginInfo pi = repo.getPlugin(pluginname); if (pi != null) - if (download.run(sender, pi.getMavenUrl(version))) { + if (download.run(sender, pi.getMavenUrl(version))) return plugman.load(sender, pluginname); - } return false; } @@ -44,10 +47,28 @@ public class YumManager { public static boolean update(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); + if (download.run(sender, pi.getMavenUrl(version), new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName()))) { + sender.sendMessage("§6更新: §a已下载插件 " + plugin.getName() + " 到update文件夹 重启后自动更新(或使用upgrade直接升级)!"); + return true; } + } else + 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());