Adds update warnings (URL needs to be updated once Spigot resource is created)
This commit is contained in:
		| @@ -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); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -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 <p>The input stream to read</p> | ||||
|      * @return <p>A buffered reader reading the input stream</p> | ||||
|      */ | ||||
|     public static BufferedReader getBufferedReaderFromInputStream(InputStream inputStream) { | ||||
|         InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); | ||||
|         return new BufferedReader(inputStreamReader); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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<String> getVersionMethod, | ||||
|                                       Consumer<String> 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<String> getVersionMethod, | ||||
|                                  Consumer<String> 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 <p>The new available plugin version</p> | ||||
|      * @param oldVersion <p>The old (current) plugin version</p> | ||||
|      * @return <p>The string to display</p> | ||||
|      */ | ||||
|     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 <p>The old version to check</p> | ||||
|      * @param newVersion <p>The new version to check</p> | ||||
|      * @return <p>True if the new version is higher than the old one</p> | ||||
|      */ | ||||
|     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; | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user