From 80d18049539fdc4ae85a3e29eff6367a3967c41d Mon Sep 17 00:00:00 2001 From: j502647092 Date: Fri, 14 Aug 2015 16:17:38 +0800 Subject: [PATCH] . --- .../SimpleProtect/utils/VersionChecker.java | 62 +++++++++++++++---- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/src/cn/citycraft/SimpleProtect/utils/VersionChecker.java b/src/cn/citycraft/SimpleProtect/utils/VersionChecker.java index 5f4e593..e4524a1 100644 --- a/src/cn/citycraft/SimpleProtect/utils/VersionChecker.java +++ b/src/cn/citycraft/SimpleProtect/utils/VersionChecker.java @@ -17,45 +17,83 @@ import org.bukkit.plugin.Plugin; import com.google.common.base.Charsets; +/** + * @author 蒋天蓓 + * 2015年8月14日下午4:01:15 + * 自动更新类 + */ public class VersionChecker implements Listener { Plugin plugin; + public String checkurl = "https://coding.net/u/502647092/p/%s/git/raw/%s/src/plugin.yml"; + public String branch = "master"; + /** + * @param plugin + * - 插件 + */ public VersionChecker(Plugin plugin) { this.plugin = plugin; plugin.getServer().getPluginManager().registerEvents(this, plugin); - this.VersionCheck(null); + this.versioncheck(null); + } + + /** + * @param plugin + * - 插件 + * @param branch + * - 分支名称 + */ + public VersionChecker(Plugin plugin, String branch) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + this.checkurl = branch; + this.versioncheck(null); + } + + /** + * 获取插件更新链接 + * + * @param pluginName + * - 插件名称 + * @param branch + * - 插件分支 + * @return 更新链接 + */ + public String getCheckUrl(String pluginName, String branch) { + return String.format(checkurl, pluginName, branch); } @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { if (e.getPlayer().isOp()) { - this.VersionCheck(e.getPlayer()); + this.versioncheck(e.getPlayer()); } } - public void VersionCheck(final Player player) { + /** + * 开始更新 + * + * @param player + * - 获取更新的玩家(null则默认为控制台) + */ + public void versioncheck(final Player player) { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - String website = plugin.getDescription().getWebsite(); - String readURL = website - + (website.substring(website.length() - 1).equals("/") ? "" : "/") - + "/lastSuccessfulBuild/artifact/src/plugin.yml"; + String readURL = getCheckUrl(plugin.getName(), branch); FileConfiguration config; String currentVersion = plugin.getDescription().getVersion(); try { URL url = new URL(readURL); - BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), - Charsets.UTF_8)); + BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); config = YamlConfiguration.loadConfiguration(br); String newVersion = config.getString("version"); br.close(); if (!newVersion.equals(currentVersion)) { String[] msg = new String[] { - ChatColor.GREEN + plugin.getName() + "插件最新版本 v" + newVersion, + ChatColor.GREEN + plugin.getName() + " 插件最新版本 v" + newVersion, ChatColor.RED + "服务器运行版本: v" + currentVersion, - ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE - + plugin.getDescription().getWebsite() + ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE + plugin.getDescription().getWebsite() }; if (player != null) { player.sendMessage(msg);