From 2014f0fffed2fb2f40f8d38cb1fcf8151863df3d Mon Sep 17 00:00:00 2001 From: TechnicallyCoded Date: Tue, 22 Aug 2023 14:57:27 +0200 Subject: [PATCH] v1.1.0 - Add SimpleUpdateChecker --- build.gradle | 11 ++- .../updatechecker/SimpleUpdateChecker.java | 68 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/tcoded/updatechecker/SimpleUpdateChecker.java diff --git a/build.gradle b/build.gradle index 80a67ba..464faec 100644 --- a/build.gradle +++ b/build.gradle @@ -3,10 +3,17 @@ plugins { } group = 'org.example' -version = '1.0-SNAPSHOT' +version = '1.1.0' repositories { mavenCentral() + maven { + url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' + content { + includeGroup 'org.bukkit' + includeGroup 'org.spigotmc' + } + } } java { @@ -16,6 +23,8 @@ java { } dependencies { + compileOnly 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT' + testImplementation platform('org.junit:junit-bom:5.9.1') testImplementation 'org.junit.jupiter:junit-jupiter' } diff --git a/src/main/java/com/tcoded/updatechecker/SimpleUpdateChecker.java b/src/main/java/com/tcoded/updatechecker/SimpleUpdateChecker.java new file mode 100644 index 0000000..f59a46a --- /dev/null +++ b/src/main/java/com/tcoded/updatechecker/SimpleUpdateChecker.java @@ -0,0 +1,68 @@ +package com.tcoded.updatechecker; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Server; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.java.JavaPlugin; + +public class SimpleUpdateChecker { + + public static void checkUpdate(JavaPlugin plugin, String prefix, int resourceId) { + Server server = plugin.getServer(); + ConsoleCommandSender consoleSender = server.getConsoleSender(); + PluginDescriptionFile pluginDesc = plugin.getDescription(); + String pluginVersion = pluginDesc.getVersion(); + String pluginName = pluginDesc.getName(); + + server.getScheduler().runTaskAsynchronously(plugin, () -> { + consoleSender.sendMessage(prefix + "Checking for updates..."); + + final UpdateResult result = new UpdateChecker(pluginVersion, resourceId).getResult(); + + int prioLevel = 0; + String prioColor = ChatColor.AQUA.toString(); + String prioLevelName = "null"; + + switch (result.getType()) { + case FAIL_SPIGOT: + consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: Could not contact Spigot to check if an update is available."); + break; + case UPDATE_LOW: + prioLevel = 1; + prioLevelName = "minor"; + break; + case UPDATE_MEDIUM: + prioLevel = 2; + prioLevelName = "feature"; + prioColor = ChatColor.GOLD.toString(); + break; + case UPDATE_HIGH: + prioLevel = 3; + prioLevelName = "MAJOR"; + prioColor = ChatColor.RED.toString(); + break; + case DEV_BUILD: + consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: You are running an experimental/development build! Proceed with caution."); + break; + case NO_UPDATE: + consoleSender.sendMessage(prefix + ChatColor.RESET + "You are running the latest version."); + break; + default: + break; + } + + if (prioLevel > 0) { + consoleSender.sendMessage( "\n" + prioColor + + "===============================================================================\n" + + "A " + prioLevelName + " update to " + pluginName + " is available!\n" + + "Download at https://www.spigotmc.org/resources/" + pluginName.toLowerCase() + "." + resourceId + "/\n" + + "(current: " + result.getCurrentVer() + ", latest: " + result.getLatestVer() + ")\n" + + "===============================================================================\n"); + } + + }); + } + +}