From ca2185a7a6fafed544b34782d1f09bb694b84439 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 5 May 2016 18:09:51 +0200 Subject: [PATCH] Added update message broadcast --- config.yml | 5 +++++ src/de/epiceric/shopchest/Commands.java | 5 ++++- src/de/epiceric/shopchest/ShopChest.java | 12 +++++++----- src/de/epiceric/shopchest/config/Config.java | 1 + .../epiceric/shopchest/event/InteractShop.java | 1 - .../epiceric/shopchest/event/NotifyUpdate.java | 1 + .../epiceric/shopchest/utils/UpdateChecker.java | 17 +++++++++++++---- 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/config.yml b/config.yml index a74fda6..f54110a 100644 --- a/config.yml +++ b/config.yml @@ -47,6 +47,11 @@ hopper-protection: true # Set whether the shop's chest should be protected by explosions explosion-protection: true +# Set whether broadcast messages should be enabled +# The messages are sent to every player who have permission to get update notifications. +# They are sent each time the update checker checks for an update below the message whether an update is available. +enable-broadcast: true + # Set the currency symbol after price values currency-symbol: $ diff --git a/src/de/epiceric/shopchest/Commands.java b/src/de/epiceric/shopchest/Commands.java index 4bfd337..30533ae 100644 --- a/src/de/epiceric/shopchest/Commands.java +++ b/src/de/epiceric/shopchest/Commands.java @@ -182,9 +182,10 @@ public class Commands extends BukkitCommand { player.sendMessage(Config.checking_update()); UpdateChecker uc = new UpdateChecker(ShopChest.getInstance(), ShopChest.getInstance().getDescription().getWebsite()); - if (uc.updateNeeded()) { + if (uc.updateNeeded(player)) { ShopChest.latestVersion = uc.getVersion(); ShopChest.downloadLink = uc.getLink(); + ShopChest.broadcast = uc.getBroadcast(); ShopChest.isUpdateNeeded = true; JsonBuilder jb; @@ -204,6 +205,8 @@ public class Commands extends BukkitCommand { player.sendMessage(Config.no_new_update()); } + if (ShopChest.broadcast != null && Config.enable_broadcast()) player.sendMessage(ShopChest.broadcast); + } private void reload(Player player) { diff --git a/src/de/epiceric/shopchest/ShopChest.java b/src/de/epiceric/shopchest/ShopChest.java index 37eac13..599f4b1 100644 --- a/src/de/epiceric/shopchest/ShopChest.java +++ b/src/de/epiceric/shopchest/ShopChest.java @@ -57,6 +57,7 @@ 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; @@ -189,14 +190,15 @@ public class ShopChest extends JavaPlugin{ instance = this; - if (uc == null) uc = new UpdateChecker(this, getDescription().getWebsite()); logger.info("Checking for Updates"); - if(uc.updateNeeded()) { + if(uc.updateNeeded(Bukkit.getConsoleSender())) { 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; @@ -212,12 +214,12 @@ public class ShopChest extends JavaPlugin{ case "v1_8_R3": jb = new JsonBuilder_1_8_R3(Config.update_available(latestVersion)); break; case "v1_9_R1": jb = new JsonBuilder_1_9_R1(Config.update_available(latestVersion)); break; default: return; - } + } jb.sendJson(p); - + if (broadcast != null && Config.enable_broadcast()) p.sendMessage(broadcast); } } - } + } File itemNamesFile = new File(getDataFolder(), "item_names.txt"); diff --git a/src/de/epiceric/shopchest/config/Config.java b/src/de/epiceric/shopchest/config/Config.java index 444e13c..2192ef8 100644 --- a/src/de/epiceric/shopchest/config/Config.java +++ b/src/de/epiceric/shopchest/config/Config.java @@ -20,6 +20,7 @@ public class Config { public static boolean buy_greater_or_equal_sell() {return plugin.getConfig().getBoolean("buy-greater-or-equal-sell");} public static boolean hopper_protection() {return plugin.getConfig().getBoolean("hopper-protection");} public static boolean explosion_protection() {return plugin.getConfig().getBoolean("explosion-protection)");} + public static boolean enable_broadcast() {return plugin.getConfig().getBoolean("enable-broadcast)");} public static double maximal_distance() {return plugin.getConfig().getDouble("maximal-distance");} public static int default_limit() {return plugin.getConfig().getInt("shop-limits.default");} diff --git a/src/de/epiceric/shopchest/event/InteractShop.java b/src/de/epiceric/shopchest/event/InteractShop.java index b1d0976..f9dc55d 100644 --- a/src/de/epiceric/shopchest/event/InteractShop.java +++ b/src/de/epiceric/shopchest/event/InteractShop.java @@ -18,7 +18,6 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; -import org.bukkit.inventory.meta.PotionMeta; import org.yi.acru.bukkit.Lockette.Lockette; import com.griefcraft.model.Protection; diff --git a/src/de/epiceric/shopchest/event/NotifyUpdate.java b/src/de/epiceric/shopchest/event/NotifyUpdate.java index 51ab594..36f1624 100644 --- a/src/de/epiceric/shopchest/event/NotifyUpdate.java +++ b/src/de/epiceric/shopchest/event/NotifyUpdate.java @@ -35,6 +35,7 @@ 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 f161b33..c6610f0 100644 --- a/src/de/epiceric/shopchest/utils/UpdateChecker.java +++ b/src/de/epiceric/shopchest/utils/UpdateChecker.java @@ -1,6 +1,7 @@ package de.epiceric.shopchest.utils; -import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -14,13 +15,14 @@ public class UpdateChecker { 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() { + public boolean updateNeeded(CommandSender sender) { 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"); @@ -30,11 +32,14 @@ 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); - } catch (Exception | Error e) { - Bukkit.getConsoleSender().sendMessage("[ShopChest] " + ChatColor.RED + "Error while checking for updates"); + sender.sendMessage((sender instanceof ConsoleCommandSender ? "[ShopChest] " : "") + ChatColor.RED + "Error while checking for updates"); return false; } } @@ -47,4 +52,8 @@ public class UpdateChecker { return link; } + public String getBroadcast() { + return broadcast; + } + }