diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index e3055d6..4da5074 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -21,6 +21,7 @@ import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.thread.BlockChangeThread; import net.knarcraft.stargate.thread.ChunkUnloadThread; import net.knarcraft.stargate.thread.StarGateThread; +import net.knarcraft.stargate.utility.UpdateChecker; import org.bukkit.Server; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; @@ -55,6 +56,8 @@ public class Stargate extends JavaPlugin { private static PluginManager pluginManager; private static StargateConfig stargateConfig; + private static String updateAvailable = null; + /** * Empty constructor necessary for Spigot */ @@ -74,6 +77,26 @@ public class Stargate extends JavaPlugin { super(loader, descriptionFile, dataFolder, file); } + /** + * Stores information about an available update + * + *

If a non-null version is given, joining admins will be alerted about the new update.

+ * + * @param version

The version of the new update available

+ */ + public static void setUpdateAvailable(String version) { + updateAvailable = version; + } + + /** + * Gets information about an available update + * + * @return

The version number if an update is available. Null otherwise

+ */ + public static String getUpdateAvailable() { + return updateAvailable; + } + /** * Gets an instance of this plugin * @@ -334,6 +357,9 @@ public class Stargate extends JavaPlugin { runThreads(); this.registerCommands(); + + //Check for any available updates + UpdateChecker.checkForUpdate(); } /** diff --git a/src/main/java/net/knarcraft/stargate/utility/UpdateChecker.java b/src/main/java/net/knarcraft/stargate/utility/UpdateChecker.java new file mode 100644 index 0000000..2df40f4 --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/utility/UpdateChecker.java @@ -0,0 +1,87 @@ +package net.knarcraft.stargate.utility; + +import net.knarcraft.stargate.Stargate; +import org.bukkit.scheduler.BukkitScheduler; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.logging.Level; + +/** + * The update checker is responsible for looking for new updates + */ +public final class UpdateChecker { + + private final static String APIResourceURL = "https://api.spigotmc.org/legacy/update.php?resource=97784"; + private final static String updateNotice = "A new update is available: %s (You are still on %s)"; + + private UpdateChecker() { + + } + + /** + * Checks if there's a new update available, and alerts the user if necessary + */ + public static void checkForUpdate() { + BukkitScheduler scheduler = Stargate.getInstance().getServer().getScheduler(); + scheduler.runTaskAsynchronously(Stargate.getInstance(), UpdateChecker::queryAPI); + } + + /** + * Queries the spigot API to check for a newer version, and informs the user + */ + private static void queryAPI() { + try { + InputStream inputStream = new URL(APIResourceURL).openStream(); + BufferedReader reader = FileHelper.getBufferedReaderFromInputStream(inputStream); + //There should only be one line of output + String newVersion = reader.readLine(); + reader.close(); + + String oldVersion = Stargate.getPluginVersion(); + //If there is a newer version, notify the user + if (isVersionHigher(oldVersion, newVersion)) { + Stargate.getConsoleLogger().log(Level.INFO, Stargate.getBackupString("prefix") + + getUpdateAvailableString(newVersion, oldVersion)); + Stargate.setUpdateAvailable(newVersion); + } + } catch (IOException e) { + Stargate.debug("UpdateChecker", "Unable to get newest version."); + } + } + + /** + * Gets the string to display to a user to alert about a new update + * + * @param newVersion

The new available plugin version

+ * @param oldVersion

The old (current) plugin version

+ * @return

The string to display

+ */ + public static String getUpdateAvailableString(String newVersion, String oldVersion) { + return String.format(updateNotice, newVersion, oldVersion); + } + + /** + * Decides whether one version number is higher than another + * + * @param oldVersion

The old version to check

+ * @param newVersion

The new version to check

+ * @return

True if the new version is higher than the old one

+ */ + public static boolean isVersionHigher(String oldVersion, String newVersion) { + String[] oldVersionParts = oldVersion.split("\\."); + String[] newVersionParts = newVersion.split("\\."); + int versionLength = Math.max(oldVersionParts.length, newVersionParts.length); + for (int i = 0; i < versionLength; i++) { + int oldVersionNumber = oldVersionParts.length > i ? Integer.parseInt(oldVersionParts[i]) : 0; + int newVersionNumber = newVersionParts.length > i ? Integer.parseInt(newVersionParts[i]) : 0; + if (newVersionNumber != oldVersionNumber) { + return newVersionNumber > oldVersionNumber; + } + } + return false; + } + +}