Skip to content

Commit

Permalink
Added update-checker and new version in config to disable the message.
Browse files Browse the repository at this point in the history
  • Loading branch information
biscuut committed Sep 16, 2018
1 parent 1193476 commit 88ecd94
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/main/java/xyz/biscut/chunkbuster/events/PlayerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/xyz/biscut/chunkbuster/utils/ConfigValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
64 changes: 62 additions & 2 deletions src/main/java/xyz/biscut/chunkbuster/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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("<version>")) {
String[] newestVersionSplit = currentLine.split(Pattern.quote("<version>"));
newestVersionSplit = newestVersionSplit[1].split(Pattern.quote("</version>"));
newestVersion = newestVersionSplit[0];
break;
}
}
reader.close();
ArrayList<Integer> 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<Integer> 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<Chunk> getWaterChunks() { return waterChunks; }
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 88ecd94

Please sign in to comment.