Skip to content

Commit

Permalink
Added a simple, unobtrusive, update checker
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
TechnicJelle committed Jan 16, 2023
1 parent d58786d commit 0deb63e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,6 +40,7 @@ public void onEnable() {

Consumer<BlueMapAPI> onEnableListener = api -> {
logger.info("API Ready! BlueMap Offline Player Markers plugin enabled!");
UpdateChecker.logUpdateMessage(logger);

config = new Config(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 0deb63e

Please sign in to comment.