From 994ccc681f85a51479c93f9984d0c29896ec2290 Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 25 Sep 2012 22:08:22 +0200 Subject: [PATCH] Config version check back in, prepare update checking. --- .../neatmonster/nocheatplus/NoCheatPlus.java | 37 ++++-------- .../nocheatplus/config/ConfPaths.java | 4 +- .../nocheatplus/utilities/Updates.java | 58 +++++++++++++++++++ 3 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 src/fr/neatmonster/nocheatplus/utilities/Updates.java diff --git a/src/fr/neatmonster/nocheatplus/NoCheatPlus.java b/src/fr/neatmonster/nocheatplus/NoCheatPlus.java index 9c171fbe..27d645df 100644 --- a/src/fr/neatmonster/nocheatplus/NoCheatPlus.java +++ b/src/fr/neatmonster/nocheatplus/NoCheatPlus.java @@ -47,6 +47,7 @@ import fr.neatmonster.nocheatplus.players.Permissions; import fr.neatmonster.nocheatplus.utilities.BlockProperties; import fr.neatmonster.nocheatplus.utilities.LagMeasureTask; import fr.neatmonster.nocheatplus.utilities.TickTask; +import fr.neatmonster.nocheatplus.utilities.Updates; /* * M"""""""`YM MM'""""'YMM dP dP MM"""""""`YM dP @@ -295,35 +296,17 @@ public class NoCheatPlus extends JavaPlugin implements Listener { // if (config.getBoolean(ConfPaths.MISCELLANEOUS_CHECKFORUPDATES)){ // // Is a new update available? -// BufferedReader bufferedReader = null; -// updateAvailable = false; -// try { -// final String[] split = getDescription().getVersion().split("-b"); -// final int currentVersion = Integer.parseInt(split[split.length - 1]); -// final URL url = new URL("http://nocheatplus.org:8080/job/NoCheatPlus/lastSuccessfulBuild/api/json"); -// connection.setReadTimeout(config.getInt(ConfPaths.MISCELLANEOUS_READTIMEOUT, 4) * 1000); -// final URLConnection connection = url.openConnection(); -// -// bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); -// String line, content = ""; -// while ((line = bufferedReader.readLine()) != null) -// content += line; -// final int jenkinsVersion = Integer.parseInt(content.split("\"number\":")[1].split(",")[0]); -// updateAvailable = currentVersion < jenkinsVersion; -// } catch (final Exception e) {} -// finally{ -// if (bufferedReader != null) try{bufferedReader.close();}catch (IOException e){}; -// } +// final int timeout = config.getInt(ConfPaths.MISCELLANEOUS_UPDATETIMEOUT, 4) * 1000; +// getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { +// @Override +// public void run() { +// updateAvailable = Updates.CheckForUpdatesTask(getDescription().getVersion(), timeout); +// } +// }); // } -// // Is the configuration outdated? -// try { -// final int currentVersion = Integer.parseInt(getDescription().getVersion().split("-b")[1]); -// final int configurationVersion = Integer.parseInt( -// config.options().header().split("-b")[1].split("\\.")[0]); -// if (currentVersion > configurationVersion) -// configOutdated = true; -// } catch (final Exception e) {} + // Is the configuration outdated? + configOutdated = Updates.isConfigOutdated(getDescription().getVersion(), config); // Debug information about unknown blocks. // (Probably removed later.) diff --git a/src/fr/neatmonster/nocheatplus/config/ConfPaths.java b/src/fr/neatmonster/nocheatplus/config/ConfPaths.java index 7c7b65e4..2f324ba2 100644 --- a/src/fr/neatmonster/nocheatplus/config/ConfPaths.java +++ b/src/fr/neatmonster/nocheatplus/config/ConfPaths.java @@ -47,8 +47,8 @@ public abstract class ConfPaths { public static final String MISCELLANEOUS_ALLOWCLIENTMODS = MISCELLANEOUS + "allowclientmods"; public static final String MISCELLANEOUS_OPINCONSOLEONLY = MISCELLANEOUS + "opinconsoleonly"; public static final String MISCELLANEOUS_PROTECTPLUGINS = MISCELLANEOUS + "protectplugins"; -// public static final String MISCELLANEOUS_CHECKFORUPDATES = MISCELLANEOUS + "checkforupdates"; -// public static final String MISCELLANEOUS_READTIMEOUT = MISCELLANEOUS + "readtimeout"; + public static final String MISCELLANEOUS_CHECKFORUPDATES = MISCELLANEOUS + "checkforupdates"; + public static final String MISCELLANEOUS_UPDATETIMEOUT = MISCELLANEOUS + "updatetimeout"; public static final String MISCELLANEOUS_REPORTTOMETRICS = MISCELLANEOUS + "reporttometrics"; private static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY = MISCELLANEOUS + "nomovedtooquickly."; public static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY_ENABLED = MISCELLANEOUS_NOMOVEDTOOQUICKLY + "enabled"; diff --git a/src/fr/neatmonster/nocheatplus/utilities/Updates.java b/src/fr/neatmonster/nocheatplus/utilities/Updates.java new file mode 100644 index 00000000..6a79c1d3 --- /dev/null +++ b/src/fr/neatmonster/nocheatplus/utilities/Updates.java @@ -0,0 +1,58 @@ +package fr.neatmonster.nocheatplus.utilities; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; + +import fr.neatmonster.nocheatplus.config.ConfigFile; + +public class Updates { + + /** + * + * @param versionString Current version string (getDescription().getVersion()). + * @param config + * @return + */ + public static boolean isConfigOutdated(String versionString, ConfigFile config){ + try { + final int currentVersion = Integer.parseInt(versionString.split("-b")[1]); + final int configurationVersion = Integer.parseInt( + config.options().header().split("-b")[1].split("\\.")[0]); + if (currentVersion > configurationVersion) + return true; + } catch (final Exception e) {} + return false; + } + + /** + * To be called from an async task. + * @param versionString Current version string (getDescription().getVersion()). + * @param updateTimeout + * @return + */ + public static boolean CheckForUpdatesTask(String versionString, int updateTimeout) { + BufferedReader bufferedReader = null; + boolean updateAvailable = false; + try { + final String[] split = versionString.split("-b"); + final int currentVersion = Integer.parseInt(split[split.length - 1]); + final URL url = new URL("http://nocheatplus.org:8080/job/NoCheatPlus/lastSuccessfulBuild/api/json"); + final URLConnection connection = url.openConnection(); + connection.setConnectTimeout(updateTimeout); + connection.setReadTimeout(2 * updateTimeout); + bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line, content = ""; + while ((line = bufferedReader.readLine()) != null) + content += line; + final int jenkinsVersion = Integer.parseInt(content.split("\"number\":")[1].split(",")[0]); + updateAvailable = currentVersion < jenkinsVersion; + } catch (final Exception e) {} + finally{ + if (bufferedReader != null) try{bufferedReader.close();}catch (IOException e){}; + } + return updateAvailable; + } +}