From 0deb63e570a80309388436c5afc5a5f365644d8e Mon Sep 17 00:00:00 2001 From: TechnicJelle <22576047+TechnicJelle@users.noreply.github.com> Date: Mon, 16 Jan 2023 02:26:48 +0100 Subject: [PATCH] Added a simple, unobtrusive, update checker Closes #27 --- .../bluemapofflineplayermarkers/Main.java | 3 ++ .../UpdateChecker.java | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/com/technicjelle/bluemapofflineplayermarkers/UpdateChecker.java diff --git a/src/main/java/com/technicjelle/bluemapofflineplayermarkers/Main.java b/src/main/java/com/technicjelle/bluemapofflineplayermarkers/Main.java index ad45599..fda2c06 100644 --- a/src/main/java/com/technicjelle/bluemapofflineplayermarkers/Main.java +++ b/src/main/java/com/technicjelle/bluemapofflineplayermarkers/Main.java @@ -29,6 +29,8 @@ public void onEnable() { logger = getLogger(); + UpdateChecker.check("TechnicJelle", "BlueMapOfflinePlayerMarkers", getDescription().getVersion()); + getServer().getPluginManager().registerEvents(this, this); //all actual startup and shutdown logic moved to BlueMapAPI enable/disable methods, so `/bluemap reload` also reloads this plugin @@ -38,6 +40,7 @@ public void onEnable() { Consumer onEnableListener = api -> { logger.info("API Ready! BlueMap Offline Player Markers plugin enabled!"); + UpdateChecker.logUpdateMessage(logger); config = new Config(this); diff --git a/src/main/java/com/technicjelle/bluemapofflineplayermarkers/UpdateChecker.java b/src/main/java/com/technicjelle/bluemapofflineplayermarkers/UpdateChecker.java new file mode 100644 index 0000000..9c75735 --- /dev/null +++ b/src/main/java/com/technicjelle/bluemapofflineplayermarkers/UpdateChecker.java @@ -0,0 +1,54 @@ +package com.technicjelle.bluemapofflineplayermarkers; + + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.logging.Logger; + +@SuppressWarnings("SameParameterValue") +public class UpdateChecker { + private static boolean updateAvailable = false; + private static URL url = null; + private static String latestVersion = null; + private static String curVer = null; + + + static void check(String author, String name, String currentVersion) { + curVer = currentVersion; + new Thread(() -> { + try { + url = new URL("https://github.com/"+author+"/"+name+"/releases/latest"); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + + HttpURLConnection con; + try { + con = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + throw new RuntimeException(e); + } + con.setInstanceFollowRedirects(false); + + String newUrl = con.getHeaderField("Location"); + + if(newUrl == null) { + throw new RuntimeException("Did not get a redirect"); + } + + String[] split = newUrl.split("/"); + latestVersion = split[split.length - 1].replace("v", ""); + + if (!latestVersion.equals(curVer)) updateAvailable = true; + }, name + "-Update-Checker").start(); + } + + static void logUpdateMessage(Logger logger) { + if (updateAvailable) { + logger.warning("New version available: v" + latestVersion + " (current: v" + curVer + ")"); + logger.warning("Download it at " + url); + } + } +}