diff --git a/config.yml b/config.yml index 4e23a27..ad957b7 100644 --- a/config.yml +++ b/config.yml @@ -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: diff --git a/src/de/epiceric/shopchest/Commands.java b/src/de/epiceric/shopchest/Commands.java index 30533ae..b4c00b4 100644 --- a/src/de/epiceric/shopchest/Commands.java +++ b/src/de/epiceric/shopchest/Commands.java @@ -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); } diff --git a/src/de/epiceric/shopchest/ShopChest.java b/src/de/epiceric/shopchest/ShopChest.java index 599f4b1..fa81e27 100644 --- a/src/de/epiceric/shopchest/ShopChest.java +++ b/src/de/epiceric/shopchest/ShopChest.java @@ -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()) diff --git a/src/de/epiceric/shopchest/config/Config.java b/src/de/epiceric/shopchest/config/Config.java index 2192ef8..aee7412 100644 --- a/src/de/epiceric/shopchest/config/Config.java +++ b/src/de/epiceric/shopchest/config/Config.java @@ -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"); diff --git a/src/de/epiceric/shopchest/event/NotifyUpdate.java b/src/de/epiceric/shopchest/event/NotifyUpdate.java index 36f1624..51ab594 100644 --- a/src/de/epiceric/shopchest/event/NotifyUpdate.java +++ b/src/de/epiceric/shopchest/event/NotifyUpdate.java @@ -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); } } diff --git a/src/de/epiceric/shopchest/utils/UpdateChecker.java b/src/de/epiceric/shopchest/utils/UpdateChecker.java index c6610f0..83f8038 100644 --- a/src/de/epiceric/shopchest/utils/UpdateChecker.java +++ b/src/de/epiceric/shopchest/utils/UpdateChecker.java @@ -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; - } + }