updata VersionChecker...

pull/1/MERGE
502647092 2015-08-18 16:19:41 +08:00
parent 6dea0ada1f
commit ae08f1b8c5
1 changed files with 51 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package com.me.tft_02.soulbound.util; package com.me.tft_02.soulbound.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
@ -17,45 +16,83 @@ import org.bukkit.plugin.Plugin;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
/**
* @author
* 20158144:01:15
*
*/
public class VersionChecker implements Listener { public class VersionChecker implements Listener {
Plugin plugin; 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) { public VersionChecker(Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, 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.branch = branch;
this.versioncheck(null);
}
/**
*
*
* @param pluginName
* -
* @param branch
* -
* @return
*/
public String getCheckUrl(String pluginName, String branch) {
return String.format(checkurl, pluginName, branch);
} }
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
if (e.getPlayer().isOp()) { 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() { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
String website = plugin.getDescription().getWebsite(); String readURL = getCheckUrl(plugin.getName(), branch);
String readURL = website
+ (website.substring(website.length() - 1).equals("/") ? "" : "/")
+ "/lastSuccessfulBuild/artifact/src/plugin.yml";
FileConfiguration config; FileConfiguration config;
String currentVersion = plugin.getDescription().getVersion(); String currentVersion = plugin.getDescription().getVersion();
try { try {
URL url = new URL(readURL); URL url = new URL(readURL);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
Charsets.UTF_8));
config = YamlConfiguration.loadConfiguration(br); config = YamlConfiguration.loadConfiguration(br);
String newVersion = config.getString("version"); String newVersion = config.getString("version");
br.close(); br.close();
if (!newVersion.equals(currentVersion)) { if (!newVersion.equals(currentVersion)) {
String[] msg = new String[] { String[] msg = new String[] {
ChatColor.GREEN + plugin.getName() + "插件最新版本 v" + newVersion, ChatColor.GREEN + plugin.getName() + " 插件最新版本 v" + newVersion,
ChatColor.RED + "服务器运行版本: v" + currentVersion, ChatColor.RED + "服务器运行版本: v" + currentVersion,
ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE + plugin.getDescription().getWebsite()
+ plugin.getDescription().getWebsite()
}; };
if (player != null) { if (player != null) {
player.sendMessage(msg); player.sendMessage(msg);
@ -63,7 +100,7 @@ public class VersionChecker implements Listener {
plugin.getServer().getConsoleSender().sendMessage(msg); plugin.getServer().getConsoleSender().sendMessage(msg);
} }
} }
} catch (IOException e) { } catch (Exception e) {
plugin.getLogger().warning("版本更新检查失败!"); plugin.getLogger().warning("版本更新检查失败!");
} }
} }