Skip to content

Commit

Permalink
Added my Update Checker
Browse files Browse the repository at this point in the history
  • Loading branch information
TechnicJelle committed Jan 17, 2023
1 parent ed0dfa9 commit 7c5aa84
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/technicjelle/bluemapfloodgate/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ private void verboseLog(String message) {
public void onEnable() {
new Metrics(this, 16426);

UpdateChecker.check("TechnicJelle", "BlueMapFloodgate", getDescription().getVersion());

BlueMapAPI.onEnable(blueMapOnEnableListener);

getLogger().info("BlueMap Floodgate compatibility plugin enabled!");
}

private final Consumer<BlueMapAPI> blueMapOnEnableListener = blueMapAPI -> {
UpdateChecker.logUpdateMessage(getLogger());

SkinProvider floodgateSkinProvider = new SkinProvider() {
private final SkinProvider defaultSkinProvider = blueMapAPI.getPlugin().getSkinProvider();

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/technicjelle/bluemapfloodgate/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//TechnicJelle's GitHub Update Checker v1
package com.technicjelle.bluemapfloodgate;

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 7c5aa84

Please sign in to comment.