From 29eea417b9fd33b89c0ff3b5a32231d00420b878 Mon Sep 17 00:00:00 2001 From: BrendonCurmi Date: Fri, 10 Apr 2020 11:22:55 +0200 Subject: [PATCH] Add update checker --- pom.xml | 33 +++++++++- .../FusionPixelmon/FusionPixelmon.java | 13 +++- .../FusionPixelmon/apis/UpdateChecker.java | 60 +++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/FusionDev/FusionPixelmon/apis/UpdateChecker.java diff --git a/pom.xml b/pom.xml index 1efa2ab..ea38dfa 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.BrendonCurmi FusionPixelmon - 1.2 + 1.3 UTF-8 @@ -42,5 +42,36 @@ minecraftforge 1.12.2-14.23.5.2768 + + + org.json + json + 20190722 + + + + + + org.apache.maven.plugins + maven-shade-plugin + 1.6 + + + package + + shade + + + + + org.json:json + + + + + + + + \ No newline at end of file diff --git a/src/main/java/me/FusionDev/FusionPixelmon/FusionPixelmon.java b/src/main/java/me/FusionDev/FusionPixelmon/FusionPixelmon.java index 9bb38d7..64fd4dc 100644 --- a/src/main/java/me/FusionDev/FusionPixelmon/FusionPixelmon.java +++ b/src/main/java/me/FusionDev/FusionPixelmon/FusionPixelmon.java @@ -1,6 +1,7 @@ package me.FusionDev.FusionPixelmon; import com.google.inject.Inject; +import me.FusionDev.FusionPixelmon.apis.UpdateChecker; import me.FusionDev.FusionPixelmon.commands.ArcPlatesCmd; import me.FusionDev.FusionPixelmon.commands.PokeDesignerCmd; @@ -27,6 +28,7 @@ import org.spongepowered.api.text.Text; import java.io.File; +import java.io.IOException; import java.nio.file.Path; import static com.pixelmonmod.pixelmon.Pixelmon.EVENT_BUS; @@ -50,9 +52,9 @@ public static void main(String[] args) { // todo if change form and do something to affect form, wasted money // todo may need to update the lvl thing for nature as well - static final String ID = "fusionpixelmon"; - static final String NAME = "FusionPixelmon"; - static final String VERSION = "1.2"; + public static final String ID = "fusionpixelmon"; + public static final String NAME = "FusionPixelmon"; + public static final String VERSION = "1.3"; private static final String CMD_PERM = ID + ".command."; @@ -141,6 +143,11 @@ public void postInit(GamePostInitializationEvent event) { @Listener public void onServerStart(GameStartedServerEvent event) { logger.info("Successfully running FusionPixelmon!"); + try { + UpdateChecker.check(logger); + } catch (IOException ignored) { + // If an exception occurs, just don't check for newer versions + } } @Listener diff --git a/src/main/java/me/FusionDev/FusionPixelmon/apis/UpdateChecker.java b/src/main/java/me/FusionDev/FusionPixelmon/apis/UpdateChecker.java new file mode 100644 index 0000000..e30dfca --- /dev/null +++ b/src/main/java/me/FusionDev/FusionPixelmon/apis/UpdateChecker.java @@ -0,0 +1,60 @@ +package me.FusionDev.FusionPixelmon.apis; + +import me.FusionDev.FusionPixelmon.FusionPixelmon; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; + +import java.io.*; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; + +public class UpdateChecker { + + private static final String VERSIONS_ENDPOINT = "https://ore.spongepowered.org/api/v1/projects/" + FusionPixelmon.ID + "/versions"; + private static final String ORE_VERSIONS = "https://ore.spongepowered.org/FusionDev/FusionPixelmon/versions"; + + public static void main(String[] args) throws IOException { + check(null); + } + + public static void check(Logger logger) throws IOException { + JSONArray payload = readJsonFromUrl(VERSIONS_ENDPOINT); + // Get 0th element as versions are in order of latest first + JSONObject data = (JSONObject) payload.get(0); + String name = data.getString("name"); + if (name != null && !name.equals(FusionPixelmon.VERSION)) { + logger.info("There is a newer version of FusionPixelmon available! Version " + name + " @ " + ORE_VERSIONS); + } + } + + /** + * Reads the data from the specified url and extracts it as a {@link JSONObject}. + * + * @param url the url of the data. + * @return the data from the url as a JSONObject. + * @throws IOException if an I/O error occurs. + */ + public static JSONArray readJsonFromUrl(String url) throws IOException { + URLConnection urlConnection = new URL(url).openConnection(); + urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); + InputStream inputStream = urlConnection.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + return new JSONArray(read(reader)); + } + + /** + * Reads the data from the specified reader and returns it as a string. + * + * @param reader the reader. + * @return the contents of the reader. + * @throws IOException if an I/O error occurs. + */ + private static String read(Reader reader) throws IOException { + StringBuilder builder = new StringBuilder(); + int cp; + while ((cp = reader.read()) != -1) builder.append((char) cp); + return builder.toString(); + } +}