diff --git a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java index 16b9679..0bef189 100644 --- a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java +++ b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java @@ -13,6 +13,7 @@ import net.knarcraft.permissionsigns.manager.PermissionManager; import net.knarcraft.permissionsigns.manager.SignManager; import net.knarcraft.permissionsigns.thread.PermissionTimeoutThread; import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread; +import net.knarcraft.permissionsigns.utility.UpdateChecker; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; @@ -145,8 +146,6 @@ public final class PermissionSigns extends JavaPlugin { PluginDescriptionFile pluginDescriptionFile = this.getDescription(); pluginVersion = pluginDescriptionFile.getVersion(); - //TODO: Display a notice in the console if a new version is available (requires Spigot resource first) - //Load config and write the default config if necessary FileConfiguration config = this.getConfig(); config.options().copyDefaults(true); @@ -159,6 +158,8 @@ public final class PermissionSigns extends JavaPlugin { SignManager.loadSigns(); PermissionManager.loadTemporaryPermissions(); + + UpdateChecker.checkForUpdate(this, "", () -> pluginVersion, null); } /** diff --git a/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java b/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java index d5a5c00..4fe271d 100644 --- a/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java @@ -25,4 +25,15 @@ public class FileHelper { return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); } + /** + * Gets a buffered reader given an input stream + * + * @param inputStream

The input stream to read

+ * @return

A buffered reader reading the input stream

+ */ + public static BufferedReader getBufferedReaderFromInputStream(InputStream inputStream) { + InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); + return new BufferedReader(inputStreamReader); + } + } diff --git a/src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java b/src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java new file mode 100644 index 0000000..677c85e --- /dev/null +++ b/src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java @@ -0,0 +1,92 @@ +package net.knarcraft.permissionsigns.utility; + +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitScheduler; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.function.Consumer; +import java.util.function.Supplier; +import java.util.logging.Level; + +/** + * The update checker is responsible for looking for new updates + */ +public final class UpdateChecker { + + 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(Plugin plugin, String apiResourceURL, Supplier getVersionMethod, + Consumer setVersionMethod) { + BukkitScheduler scheduler = plugin.getServer().getScheduler(); + scheduler.runTaskAsynchronously(plugin, () -> UpdateChecker.queryAPI(plugin, apiResourceURL, getVersionMethod, + setVersionMethod)); + } + + /** + * Queries the spigot API to check for a newer version, and informs the user + */ + private static void queryAPI(Plugin plugin, String APIResourceURL, Supplier getVersionMethod, + Consumer setVersionMethod) { + 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 = getVersionMethod.get(); + //If there is a newer version, notify the user + if (isVersionHigher(oldVersion, newVersion)) { + plugin.getLogger().log(Level.INFO, getUpdateAvailableString(newVersion, oldVersion)); + if (setVersionMethod != null) { + setVersionMethod.accept(newVersion); + } + } + } catch (IOException e) { + plugin.getLogger().log(Level.WARNING, "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; + } + +}