Skip to content

Commit

Permalink
Add update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendonCurmi committed Apr 10, 2020
1 parent 43f8890 commit 29eea41
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
33 changes: 32 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.BrendonCurmi</groupId>
<artifactId>FusionPixelmon</artifactId>
<version>1.2</version>
<version>1.3</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -42,5 +42,36 @@
<artifactId>minecraftforge</artifactId>
<version>1.12.2-14.23.5.2768</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.json:json</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
13 changes: 10 additions & 3 deletions src/main/java/me/FusionDev/FusionPixelmon/FusionPixelmon.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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.";

Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/me/FusionDev/FusionPixelmon/apis/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 29eea41

Please sign in to comment.