Skip to content

Instantly share code, notes, and snippets.

@AkaGiant
Created May 11, 2023 12:20
Show Gist options
  • Save AkaGiant/7c0d08e813815310ccc533f6ead927df to your computer and use it in GitHub Desktop.
Save AkaGiant/7c0d08e813815310ccc533f6ead927df to your computer and use it in GitHub Desktop.
Checks for an Update for a Spigot Plugin using a pom.xml file from a public repository defined within the URL. Ensure it's a raw.githubusercontent url...
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class UpdateChecker {
String url = "https://raw.githubusercontent.com/AkaGiant/SimpleExp/dev/pom.xml";
double current = Double.parseDouble(
SimpleExp.getPlugin().getDescription().getVersion()
.replaceAll("[^\\d.]", "").trim()
);
double latest = 0.0;
public void run() {
this.latest = getGithubLatest();
if (this.latest == -1) return;
if (current > latest) {
Logger.toConsole("&fYou seemingly have a newer version than is currently released");
Logger.toConsole("&fCurrent: &b" + current + " &8| &fLatest: &c" + latest);
Logger.toConsole("&m------------------------------------");
return;
}
if (current == latest) {
Logger.toConsole("&aNo Updates Found");
Logger.toConsole("&fCurrent: &b" + current + " &8| &fLatest: &c" + latest);
Logger.toConsole("&m------------------------------------");
return;
}
Logger.toConsole("&aUpdates Found");
Logger.toConsole("&fCurrent: &b" + current + " &8| &fLatest: &c" + latest);
Logger.toConsole("&fURL Will be provided soon.");
Logger.toConsole("&m------------------------------------");
}
private double getGithubLatest() {
InputStream in = null;
try {
in = new URL(url).openStream();
} catch (IOException e) {
Logger.severe("&fUnable to check for updates!");
}
NodeList nodeList;
try {
nodeList = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in).getElementsByTagName("version");
} catch (SAXException | IOException | ParserConfigurationException e) {
Logger.severe("&fUnable to get and find the Version identifier for the latest version.");
return -1;
}
String latestVersion = nodeList.item(0).getFirstChild().toString().replaceAll("[^\\d.]", "").trim();
return Double.parseDouble(latestVersion);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment