|
| 1 | +package com.tcoded.updatechecker; |
| 2 | + |
| 3 | +import org.bukkit.Bukkit; |
| 4 | +import org.bukkit.ChatColor; |
| 5 | +import org.bukkit.Server; |
| 6 | +import org.bukkit.command.ConsoleCommandSender; |
| 7 | +import org.bukkit.plugin.PluginDescriptionFile; |
| 8 | +import org.bukkit.plugin.java.JavaPlugin; |
| 9 | + |
| 10 | +public class SimpleUpdateChecker { |
| 11 | + |
| 12 | + public static void checkUpdate(JavaPlugin plugin, String prefix, int resourceId) { |
| 13 | + Server server = plugin.getServer(); |
| 14 | + ConsoleCommandSender consoleSender = server.getConsoleSender(); |
| 15 | + PluginDescriptionFile pluginDesc = plugin.getDescription(); |
| 16 | + String pluginVersion = pluginDesc.getVersion(); |
| 17 | + String pluginName = pluginDesc.getName(); |
| 18 | + |
| 19 | + server.getScheduler().runTaskAsynchronously(plugin, () -> { |
| 20 | + consoleSender.sendMessage(prefix + "Checking for updates..."); |
| 21 | + |
| 22 | + final UpdateResult result = new UpdateChecker(pluginVersion, resourceId).getResult(); |
| 23 | + |
| 24 | + int prioLevel = 0; |
| 25 | + String prioColor = ChatColor.AQUA.toString(); |
| 26 | + String prioLevelName = "null"; |
| 27 | + |
| 28 | + switch (result.getType()) { |
| 29 | + case FAIL_SPIGOT: |
| 30 | + consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: Could not contact Spigot to check if an update is available."); |
| 31 | + break; |
| 32 | + case UPDATE_LOW: |
| 33 | + prioLevel = 1; |
| 34 | + prioLevelName = "minor"; |
| 35 | + break; |
| 36 | + case UPDATE_MEDIUM: |
| 37 | + prioLevel = 2; |
| 38 | + prioLevelName = "feature"; |
| 39 | + prioColor = ChatColor.GOLD.toString(); |
| 40 | + break; |
| 41 | + case UPDATE_HIGH: |
| 42 | + prioLevel = 3; |
| 43 | + prioLevelName = "MAJOR"; |
| 44 | + prioColor = ChatColor.RED.toString(); |
| 45 | + break; |
| 46 | + case DEV_BUILD: |
| 47 | + consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: You are running an experimental/development build! Proceed with caution."); |
| 48 | + break; |
| 49 | + case NO_UPDATE: |
| 50 | + consoleSender.sendMessage(prefix + ChatColor.RESET + "You are running the latest version."); |
| 51 | + break; |
| 52 | + default: |
| 53 | + break; |
| 54 | + } |
| 55 | + |
| 56 | + if (prioLevel > 0) { |
| 57 | + consoleSender.sendMessage( "\n" + prioColor + |
| 58 | + "===============================================================================\n" + |
| 59 | + "A " + prioLevelName + " update to " + pluginName + " is available!\n" + |
| 60 | + "Download at https://www.spigotmc.org/resources/" + pluginName.toLowerCase() + "." + resourceId + "/\n" + |
| 61 | + "(current: " + result.getCurrentVer() + ", latest: " + result.getLatestVer() + ")\n" + |
| 62 | + "===============================================================================\n"); |
| 63 | + } |
| 64 | + |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments