Improved Update Checker error handling

(temporary removed broadcast messages)
This commit is contained in:
Eric 2016-05-07 15:08:40 +02:00
parent af2cf38ddf
commit e1198028ee
6 changed files with 48 additions and 40 deletions

View File

@ -272,7 +272,10 @@ messages:
no-update: "&6&lNo new update available."
# Set the message when the update checker is started.
checking: "&6&lChecking for Updates..."
checking: "&6&lChecking for updates..."
# Set the message when an error occurs while checking for updates.
error: "&c&lError while checking for updates."
hologram:

View File

@ -21,6 +21,7 @@ import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.ClickType.EnumClickType;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker;
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
import net.milkbowl.vault.permission.Permission;
public class Commands extends BukkitCommand {
@ -182,10 +183,11 @@ public class Commands extends BukkitCommand {
player.sendMessage(Config.checking_update());
UpdateChecker uc = new UpdateChecker(ShopChest.getInstance(), ShopChest.getInstance().getDescription().getWebsite());
if (uc.updateNeeded(player)) {
UpdateCheckerResult result = uc.updateNeeded();
if (result == UpdateCheckerResult.TRUE) {
ShopChest.latestVersion = uc.getVersion();
ShopChest.downloadLink = uc.getLink();
ShopChest.broadcast = uc.getBroadcast();
ShopChest.isUpdateNeeded = true;
JsonBuilder jb;
@ -198,14 +200,18 @@ public class Commands extends BukkitCommand {
}
jb.sendJson(player);
} else {
} else if (result == UpdateCheckerResult.FALSE) {
ShopChest.latestVersion = "";
ShopChest.downloadLink = "";
ShopChest.isUpdateNeeded = false;
player.sendMessage(Config.no_new_update());
} else {
ShopChest.latestVersion = "";
ShopChest.downloadLink = "";
ShopChest.isUpdateNeeded = false;
player.sendMessage(Config.update_check_error());
}
if (ShopChest.broadcast != null && Config.enable_broadcast()) player.sendMessage(ShopChest.broadcast);
}

View File

@ -10,7 +10,6 @@ import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
@ -38,13 +37,13 @@ import de.epiceric.shopchest.utils.Metrics.Graph;
import de.epiceric.shopchest.utils.Metrics.Plotter;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker;
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
public class ShopChest extends JavaPlugin{
private static ShopChest instance;
private static UpdateChecker uc;
public static Statement statement;
public static Logger logger;
@ -57,7 +56,6 @@ public class ShopChest extends JavaPlugin{
public static boolean isUpdateNeeded = false;
public static String latestVersion = "";
public static String downloadLink = "";
public static String broadcast = null;
public static Utils utils;
@ -190,21 +188,16 @@ public class ShopChest extends JavaPlugin{
instance = this;
if (uc == null) uc = new UpdateChecker(this, getDescription().getWebsite());
logger.info("Checking for Updates");
if(uc.updateNeeded(Bukkit.getConsoleSender())) {
UpdateChecker uc = new UpdateChecker(this, getDescription().getWebsite());
UpdateCheckerResult result = uc.updateNeeded();
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + Config.checking_update());
if(result == UpdateCheckerResult.TRUE) {
latestVersion = uc.getVersion();
downloadLink = uc.getLink();
broadcast = uc.getBroadcast();
isUpdateNeeded = true;
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + ChatColor.GOLD + "New version available: " + ChatColor.RED + latestVersion);
if (broadcast != null && Config.enable_broadcast()) Bukkit.getConsoleSender().sendMessage("[ShopChest] " + broadcast);
} else {
logger.info("No new version available");
isUpdateNeeded = false;
}
if (isUpdateNeeded) {
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + Config.update_available(latestVersion));
for (Player p : getServer().getOnlinePlayers()) {
if (p.isOp() || perm.has(p, "shopchest.notification.update")) {
JsonBuilder jb;
@ -216,11 +209,21 @@ public class ShopChest extends JavaPlugin{
default: return;
}
jb.sendJson(p);
if (broadcast != null && Config.enable_broadcast()) p.sendMessage(broadcast);
}
}
} else if (result == UpdateCheckerResult.FALSE) {
latestVersion = "";
downloadLink = "";
isUpdateNeeded = false;
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + Config.no_new_update());
} else {
latestVersion = "";
downloadLink = "";
isUpdateNeeded = false;
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + Config.update_check_error());
}
File itemNamesFile = new File(getDataFolder(), "item_names.txt");
if (!itemNamesFile.exists())

View File

@ -71,9 +71,9 @@ public class Config {
public static String checking_update() {return plugin.getConfig().getString("messages.update.checking").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}
public static String no_new_update() {return plugin.getConfig().getString("messages.update.no-update").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}
public static String click_to_download() {return plugin.getConfig().getString("messages.update.click-to-download").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}
public static String update_check_error() {return plugin.getConfig().getString("messages.update.error").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}
public static String cannot_sell_item() {return plugin.getConfig().getString("messages.cannot-sell-item").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}
public static String none() {return plugin.getConfig().getString("messages.shop-info.none").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}
public static String limit_reached(int limit) {
return plugin.getConfig().getString("messages.shop-limit-reached").replace(Regex.limit, String.valueOf(limit)).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");

View File

@ -35,7 +35,6 @@ public class NotifyUpdate implements Listener {
default: return;
}
jb.sendJson(p);
if (ShopChest.broadcast != null && Config.enable_broadcast()) p.sendMessage(ShopChest.broadcast);
}
}

View File

@ -1,28 +1,30 @@
package de.epiceric.shopchest.utils;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import de.epiceric.shopchest.ShopChest;
import net.md_5.bungee.api.ChatColor;
public class UpdateChecker {
public enum UpdateCheckerResult {
TRUE,
FALSE,
ERROR;
}
private ShopChest plugin;
private String url;
private String version;
private String link;
private String broadcast;
public UpdateChecker(ShopChest plugin, String url) {
this.plugin = plugin;
this.url = url;
}
public boolean updateNeeded(CommandSender sender) {
public UpdateCheckerResult updateNeeded() {
try {
Connection con = Jsoup.connect("http://textuploader.com/all1l/raw");
con.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0");
@ -32,15 +34,13 @@ public class UpdateChecker {
version = doc.text().split("\\|")[0];
link = url + "download?version=" + doc.text().split("\\|")[1];
if (doc.text().split("\\|").length == 3) {
broadcast = doc.text().split("\\|")[2];
}
return !plugin.getDescription().getVersion().equals(version);
if(plugin.getDescription().getVersion().equals(version))
return UpdateCheckerResult.FALSE;
else
return UpdateCheckerResult.TRUE;
} catch (Exception | Error e) {
sender.sendMessage((sender instanceof ConsoleCommandSender ? "[ShopChest] " : "") + ChatColor.RED + "Error while checking for updates");
return false;
return UpdateCheckerResult.ERROR;
}
}
@ -51,9 +51,6 @@ public class UpdateChecker {
public String getLink() {
return link;
}
public String getBroadcast() {
return broadcast;
}
}