diff --git a/src/main/java/xyz/biscut/chunkbuster/events/PlayerEvents.java b/src/main/java/xyz/biscut/chunkbuster/events/PlayerEvents.java index 5995c60..ae46b59 100644 --- a/src/main/java/xyz/biscut/chunkbuster/events/PlayerEvents.java +++ b/src/main/java/xyz/biscut/chunkbuster/events/PlayerEvents.java @@ -9,6 +9,7 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -235,4 +236,11 @@ public void onGUIClose(InventoryCloseEvent e) { chunkBusterLocations.remove(e.getPlayer()); } } + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + if (main.getConfigValues().showUpdateMessage() && e.getPlayer().isOp()) { + main.getUtils().checkUpdates(e.getPlayer()); + } + } } diff --git a/src/main/java/xyz/biscut/chunkbuster/utils/ConfigValues.java b/src/main/java/xyz/biscut/chunkbuster/utils/ConfigValues.java index 633b03c..6950d83 100644 --- a/src/main/java/xyz/biscut/chunkbuster/utils/ConfigValues.java +++ b/src/main/java/xyz/biscut/chunkbuster/utils/ConfigValues.java @@ -362,4 +362,8 @@ public int getCooldown() { public String getCooldownMessage(int minutes, int seconds) { return ChatColor.translateAlternateColorCodes('&', main.getConfig().getString("messages.cooldown")).replace("{minutes}", String.valueOf(minutes)).replace("{seconds}", String.valueOf(seconds)); } + + public boolean showUpdateMessage() { + return main.getConfig().getBoolean("show-update-messages"); + } } diff --git a/src/main/java/xyz/biscut/chunkbuster/utils/Utils.java b/src/main/java/xyz/biscut/chunkbuster/utils/Utils.java index c78f2ad..d0e58b4 100644 --- a/src/main/java/xyz/biscut/chunkbuster/utils/Utils.java +++ b/src/main/java/xyz/biscut/chunkbuster/utils/Utils.java @@ -1,6 +1,8 @@ package xyz.biscut.chunkbuster.utils; import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; @@ -13,8 +15,12 @@ import xyz.biscut.chunkbuster.ChunkBuster; import xyz.biscut.chunkbuster.timers.RemovalQueue; -import java.util.ArrayList; -import java.util.HashSet; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.*; +import java.util.regex.Pattern; public class Utils { @@ -267,6 +273,60 @@ public void updateConfig(ChunkBuster main) { main.getConfig().set("config-version", 1.5); main.saveConfig(); } + if (main.getConfig().getDouble("config-version") < 1.6) { + if (!main.getConfig().isSet("show-update-messages")) { + main.getConfig().set("show-update-messages", true); + } + main.getConfig().set("config-version", 1.6); + main.saveConfig(); + } + } + + public void checkUpdates(Player p) { + try { + URL url = new URL("https://raw.githubusercontent.com/biscuut/ChunkBuster/master/pom.xml"); + URLConnection connection = url.openConnection(); + connection.setReadTimeout(5000); + connection.addRequestProperty("User-Agent", "ChunkBuster update checker"); + connection.setDoOutput(true); + final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String currentLine; + String newestVersion = ""; + while ((currentLine = reader.readLine()) != null) { + if (currentLine.contains("")) { + String[] newestVersionSplit = currentLine.split(Pattern.quote("")); + newestVersionSplit = newestVersionSplit[1].split(Pattern.quote("")); + newestVersion = newestVersionSplit[0]; + break; + } + } + reader.close(); + ArrayList newestVersionNumbers = new ArrayList<>(); + try { + for (String s : newestVersion.split(Pattern.quote("."))) { + newestVersionNumbers.add(Integer.parseInt(s)); + } + } catch (Exception ex) { + return; + } + if (newestVersionNumbers.size() != 3) { + return; + } + ArrayList thisVersionNumbers = new ArrayList<>(); + try { + for (String s : main.getDescription().getVersion().split(Pattern.quote("."))) { + thisVersionNumbers.add(Integer.parseInt(s)); + } + } catch (Exception ex) { + return; + } + if (newestVersionNumbers.get(0) > thisVersionNumbers.get(0) || newestVersionNumbers.get(1) > thisVersionNumbers.get(1) || newestVersionNumbers.get(2) > thisVersionNumbers.get(2)) { + TextComponent one = new TextComponent("A new version of ChunkBuster, " + newestVersion + " is available. Download it by clicking here."); + one.setColor(ChatColor.RED); + one.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.spigotmc.org/resources/chunkbuster-1-8-1-12-clear-any-chunk-area.60057/")); + p.spigot().sendMessage(one); + } + } catch (Exception ignored) {} } public HashSet getWaterChunks() { return waterChunks; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 51f14d6..3b35576 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -112,5 +112,9 @@ messages: ignored-materials: - 'BEDROCK' +# Whether to show the newer version messages. I recommend to keep this true to +# stay on top of the new bug fixes and features. +show-update-messages: true + # Please do not edit :) -config-version: 1.5 +config-version: 1.6