From 896357ad1beb7cf9502f6067c71f0fa3bf367c4d Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 23 Aug 2015 11:05:29 +0800 Subject: [PATCH] fix update error... --- .../citycraft/Yum/commands/CommandUpdate.java | 1 - .../citycraft/Yum/utils/DownloadManager.java | 43 +++++++++++-------- src/main/resources/plugin.yml | 2 +- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java b/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java index 1023413..237615c 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java @@ -36,7 +36,6 @@ public class CommandUpdate extends BaseCommand { public void run() { if (yum.download.update(sender, plugin)) { sender.sendMessage(PluginsManager.unload(plugin)); - // PluginsManager.getPluginFile(plugin).delete(); sender.sendMessage(PluginsManager.load(plugin)); } } diff --git a/src/main/java/cn/citycraft/Yum/utils/DownloadManager.java b/src/main/java/cn/citycraft/Yum/utils/DownloadManager.java index 455a337..7702d97 100644 --- a/src/main/java/cn/citycraft/Yum/utils/DownloadManager.java +++ b/src/main/java/cn/citycraft/Yum/utils/DownloadManager.java @@ -22,16 +22,17 @@ public class DownloadManager { this.plugin = main; } - private String getPer(double per) { + private String getPer(int per) { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 11; i++) { if (per > i) { - sb.append(" "); - } else { sb.append("=="); + } else if (per == i) { + sb.append("> "); + } else { + sb.append(" "); } } - sb.append(">"); return sb.toString(); } @@ -39,20 +40,22 @@ public class DownloadManager { return run(sender, pluginname, null); } - public boolean run(CommandSender sender, String pluginname, String filename) { + public boolean run(final CommandSender sender, final String pluginname, final String filename) { String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar"; // String url = "https://502647092.github.io/plugins/%1$s/%1$s.jar"; + BufferedInputStream in = null; FileOutputStream fout = null; + CommandSender resultsender = sender; if (sender == null) { - sender = Bukkit.getConsoleSender(); + resultsender = Bukkit.getConsoleSender(); } try { - sender.sendMessage("§6开始下载: §3" + pluginname); + resultsender.sendMessage("§6开始下载: §3" + pluginname); URL fileUrl = new URL(String.format(url, pluginname)); - sender.sendMessage("§6下载地址: §3" + fileUrl.getPath()); + resultsender.sendMessage("§6下载地址: §3" + fileUrl.getPath()); int fileLength = fileUrl.openConnection().getContentLength(); - sender.sendMessage("§6文件长度: §3" + fileLength); + resultsender.sendMessage("§6文件长度: §3" + fileLength); in = new BufferedInputStream(fileUrl.openStream()); File file = null; if (filename == null) { @@ -62,12 +65,13 @@ public class DownloadManager { } if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); - sender.sendMessage("§d创建新目录: " + file.getParentFile().getAbsolutePath()); + resultsender.sendMessage("§d创建新目录: " + file.getParentFile().getAbsolutePath()); } - if (!file.exists()) { - file.createNewFile(); - sender.sendMessage("§d创建新文件: " + file.getName()); + if (file.exists()) { + file.delete(); } + file.createNewFile(); + resultsender.sendMessage("§6创建新文件: §d" + file.getAbsolutePath()); fout = new FileOutputStream(file); byte[] data = new byte[1024]; long downloaded = 0L; @@ -75,14 +79,15 @@ public class DownloadManager { while ((count = in.read(data)) != -1) { downloaded += count; fout.write(data, 0, count); - double percent = downloaded / fileLength * 10000; - sender.sendMessage(String.format("§a已下载: §a" + getPer(percent) + " %.2f%%", percent)); + int percent = (int) (downloaded * 100L / fileLength); + if (percent % 10 == 0) { + resultsender.sendMessage(String.format("§6已下载: §a" + getPer(percent / 10) + " %s%%", percent)); + } } - sender.sendMessage("§6已下载: §a====================> 100%"); - sender.sendMessage("§a插件: " + pluginname + " 下载完成!"); + resultsender.sendMessage("§a插件: " + pluginname + " 下载完成!"); return true; } catch (Exception ex) { - sender.sendMessage("§c插件" + pluginname + "下载失败!"); + resultsender.sendMessage("§c插件" + pluginname + "下载失败!"); ex.printStackTrace(); return false; } finally { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 88daba3..4515ef4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: Yum main: cn.citycraft.Yum.Yum website: http://ci.citycraft.cn:8800/jenkins/job/Yum/ -version: 1.1 +version: 1.2 commands: yum: description: MC插件仓库