-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43f8890
commit 29eea41
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/me/FusionDev/FusionPixelmon/apis/UpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |