Added a simple Update Checker

This commit is contained in:
Sn0wStorm 2015-01-09 18:08:38 +01:00
parent 224e096448
commit a8720e01c0
5 changed files with 208 additions and 11 deletions

View File

@ -51,11 +51,15 @@ colorInBrewer: false
# If a Large Barrel can be opened by clicking on any of its blocks, not just Spigot or Sign. This is always true for Small Barrels. [true]
openLargeBarrelEverywhere: true
# Enable checking for Updates, Checks the curseforge api for updates to Brewery [true]
# If an Update is found a Message is logged on Server-start and displayed to ops joining the game
updateCheck: true
# Autosave interval in minutes [3]
autosave: 3
# Config Version
version: '1.3'
version: '1.3.1'
# -- Recipes for Potions --

View File

@ -36,9 +36,10 @@ import com.dre.brewery.integration.LogBlockBarrel;
public class P extends JavaPlugin {
public static P p;
public static String configVersion = "1.3";
public static String configVersion = "1.3.1";
public static boolean debug;
public static boolean useUUID;
public static boolean updateCheck;
// Third Party Enabled
public boolean useWG; //WorldGuard
@ -90,6 +91,10 @@ public class P extends JavaPlugin {
p.getServer().getScheduler().runTaskTimer(p, new BreweryRunnable(), 650, 1200);
p.getServer().getScheduler().runTaskTimer(p, new DrunkRunnable(), 120, 120);
if (updateCheck) {
p.getServer().getScheduler().runTaskLaterAsynchronously(p, new UpdateChecker(), 135);
}
this.log(this.getDescription().getName() + " enabled!");
}
@ -205,6 +210,9 @@ public class P extends JavaPlugin {
}
}
// If the Update Checker should be enabled
updateCheck = config.getBoolean("updateCheck", false);
// Third-Party
useWG = config.getBoolean("useWorldGuard", true) && getServer().getPluginManager().isPluginEnabled("WorldGuard");
if (useWG) {
@ -656,10 +664,6 @@ public class P extends JavaPlugin {
// Runnables
public class DrunkRunnable implements Runnable {
public DrunkRunnable() {
}
@Override
public void run() {
if (!BPlayer.isEmpty()) {
@ -669,10 +673,6 @@ public class P extends JavaPlugin {
}
public class BreweryRunnable implements Runnable {
public BreweryRunnable() {
}
@Override
public void run() {
for (BCauldron cauldron : BCauldron.bcauldrons) {

View File

@ -116,7 +116,16 @@ public class ConfigUpdater {
fromVersion = "1.3";
}
if (!fromVersion.equals("1.3")) {
if (fromVersion.equals("1.3")) {
if (lang.equals("de")) {
update13de();
} else {
update13en();
}
fromVersion = "1.3.1";
}
if (!fromVersion.equals("1.3.1")) {
P.p.log(P.p.languageReader.get("Error_ConfigUpdate", fromVersion));
return;
}
@ -584,4 +593,54 @@ public class ConfigUpdater {
}
private void update13de() {
updateVersion("1.3.1");
int index = indexOfStart("# Autosave");
String[] lines = new String[] { "# Aktiviert das Suchen nach Updates für Brewery mit der curseforge api [true]",
"# Wenn ein Update gefunden wurde, wird dies bei Serverstart im log angezeigt, sowie ops benachrichtigt",
"updateCheck: true",
"" };
if (index == -1) {
index = indexOfStart("autosave:");
if (index == -1) {
index = indexOfStart("# Sprachedatei");
if (index == -1) {
index = indexOfStart("language:");
}
}
}
if (index == -1) {
appendLines(lines);
} else {
addLines(index, lines);
}
}
private void update13en() {
updateVersion("1.3.1");
int index = indexOfStart("# Autosave");
String[] lines = new String[] { "# Enable checking for Updates, Checks the curseforge api for updates to Brewery [true]",
"# If an Update is found a Message is logged on Server-start and displayed to ops joining the game",
"updateCheck: true",
"" };
if (index == -1) {
index = indexOfStart("autosave:");
if (index == -1) {
index = indexOfStart("# Languagefile");
if (index == -1) {
index = indexOfStart("language:");
}
}
}
if (index == -1) {
appendLines(lines);
} else {
addLines(index, lines);
}
}
}

View File

@ -0,0 +1,128 @@
package com.dre.brewery.filedata;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.bukkit.entity.Player;
import com.dre.brewery.P;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
/**
* Update Checker modified from the Gravity Update Checker Example:
* https://github.com/gravitylow/ServerModsAPI-Example/blob/master/Update.java
*/
public class UpdateChecker implements Runnable {
// The project's unique ID
private final int projectID = 68006;
// Used for locating version numbers in file names
private static final String DELIMETER = "^v|[\\s_-]v";
// Keys for extracting file information from JSON response
private static final String API_NAME_VALUE = "name";
/* private static final String API_LINK_VALUE = "downloadUrl";
private static final String API_RELEASE_TYPE_VALUE = "releaseType";
private static final String API_FILE_NAME_VALUE = "fileName";
private static final String API_GAME_VERSION_VALUE = "gameVersion";*/
// Static information for querying the API
private static final String API_QUERY = "/servermods/files?projectIds=";
private static final String API_HOST = "https://api.curseforge.com";
public static String update = null;
public static void notify(final Player player) {
if (update == null || !player.isOp()) {
return;
}
P.p.msg(player, update);
}
@Override
public void run() {
query();
}
/**
* Query the API to find the latest approved file's details.
*/
public void query() {
URL url;
try {
// Create the URL to query using the project's ID
url = new URL(API_HOST + API_QUERY + projectID);
} catch (MalformedURLException e) {
// There was an error creating the URL
e.printStackTrace();
return;
}
try {
// Open a connection and query the project
URLConnection conn = url.openConnection();
/*if (apiKey != null) {
// Add the API key to the request if present
conn.addRequestProperty("X-API-Key", apiKey);
}*/
// Add the user-agent to identify the program
conn.addRequestProperty("User-Agent", "Brewery UpdateChecker (by Gravity)");
// Read the response of the query
// The response will be in a JSON format, so only reading one line is necessary.
final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = reader.readLine();
// Parse the array of files from the query's response
JSONArray array = (JSONArray) JSONValue.parse(response);
if (array.size() > 0) {
// Get the newest file's details
JSONObject latest = (JSONObject) array.get(array.size() - 1);
// Get the version's title
String versionName = (String) latest.get(API_NAME_VALUE);
/*// Get the version's link
String versionLink = (String) latest.get(API_LINK_VALUE);
// Get the version's release type
String versionType = (String) latest.get(API_RELEASE_TYPE_VALUE);
// Get the version's file name
String versionFileName = (String) latest.get(API_FILE_NAME_VALUE);
// Get the version's game version
String versionGameVersion = (String) latest.get(API_GAME_VERSION_VALUE);*/
String[] split = versionName.split(DELIMETER);
if (split.length < 2) {
P.p.log("Malformed Remote File Name, could not check for updates");
} else {
if (!P.p.getDescription().getVersion().equals(split[1].split(" ")[0])) {
P.p.log("Update available for Brewery-" + P.p.getDescription().getVersion() + ": " + versionName);
update = "Update available: v" + split[1];
}
}
} else {
P.p.log("There are no files for this project");
}
} catch (IOException e) {
// There was an error reading the query
P.p.errorLog("Could not check for Updates. This error can probably be ignored");
e.printStackTrace();
}
}
}

View File

@ -19,6 +19,7 @@ import com.dre.brewery.BPlayer;
import com.dre.brewery.Words;
import com.dre.brewery.Wakeup;
import com.dre.brewery.P;
import com.dre.brewery.filedata.UpdateChecker;
public class PlayerListener implements Listener {
@ -231,6 +232,11 @@ public class PlayerListener implements Listener {
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
UpdateChecker.notify(event.getPlayer());
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
BPlayer bplayer = BPlayer.get(event.getPlayer());