diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java b/src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java index d0d0bb9..14cbc99 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java @@ -158,10 +158,11 @@ public static void reload() { getInstance().initDbWrapper(); createServerAccount(); createServerVaultAccount(); - getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> checkForUpdates().ifPresent(latestVersion -> { - getInstance().getLogger().warning("An update is available: " + latestVersion); + getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> ModrinthUpdate.checkForUpdates().ifPresent(update -> { + getInstance().getLogger().warning("An update is available: " + update.name); + getInstance().getLogger().warning("You are running: " + BankAccounts.getInstance().getPluginMeta().getVersion() + " · Latest version: " + update.version); getInstance().getLogger().warning("Please update to the latest version to benefit from bug fixes, security patches, new features and support."); - getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion); + getInstance().getLogger().warning("Update details: " + update.url()); })); getInstance().startInterestTimer(); if (getInstance().invoiceNotificationTask != null) { @@ -394,41 +395,6 @@ private static void createServerVaultAccount() { return sender instanceof OfflinePlayer ? (OfflinePlayer) sender : getConsoleOfflinePlayer(); } - /** - * Check for plugin updates using Modrinth API - * - * @return The latest version if an update is available, otherwise empty - */ - public static Optional checkForUpdates() { - final @NotNull BankAccounts plugin = BankAccounts.getInstance(); - final @NotNull String mcVersion = plugin.getServer().getMinecraftVersion(); - final @NotNull String pluginName = plugin.getPluginMeta().getName(); - final @NotNull String pluginVersion = plugin.getPluginMeta().getVersion(); - try { - final @NotNull HttpClient client = HttpClient.newHttpClient(); - final @NotNull HttpRequest req = HttpRequest.newBuilder() - .uri(URI.create("https://api.modrinth.com/v2/project/Dc8RS2En/version?featured=true&game_versions=[%22" + mcVersion + "%22]")) - .header("User-Agent", - pluginName + "/" + pluginVersion - ) - .GET() - .build(); - final @NotNull HttpResponse res = client.send(req, HttpResponse.BodyHandlers.ofString()); - if (res.statusCode() < 400 && res.statusCode() >= 200 && res.body() != null && !(JsonParser.parseString(res.body()).getAsJsonArray().isEmpty())) { - final @NotNull JsonObject json = JsonParser.parseString(res.body()).getAsJsonArray().get(0).getAsJsonObject(); - if (json.has("version_number")) { - final @NotNull String latestVersion = json.get("version_number").getAsString(); - if (!latestVersion.equals(pluginVersion)) - return Optional.of(latestVersion); - } - } - } - catch (final @NotNull Exception e) { - plugin.getLogger().log(Level.WARNING, "Failed to check for updates", e); - } - return Optional.empty(); - } - /** * Run a task on the main thread * @param task The task to run diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java b/src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java index c3ac8ed..66fff30 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java @@ -1451,10 +1451,13 @@ public int invoiceNotifyInterval() { } // messages.update-available - public @NotNull Component messagesUpdateAvailable(final @NotNull String version) { + public @NotNull Component messagesUpdateAvailable(final @NotNull ModrinthUpdate update) { return MiniMessage.miniMessage().deserialize( Objects.requireNonNull(config.getString("messages.update-available")) - .replace("", version) + .replace("", update.version) + .replace("", update.name) + .replace("", update.url()) + .replace("", BankAccounts.getInstance().getPluginMeta().getVersion()) ); } diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/ModrinthUpdate.java b/src/main/java/pro/cloudnode/smp/bankaccounts/ModrinthUpdate.java new file mode 100644 index 0000000..519971e --- /dev/null +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/ModrinthUpdate.java @@ -0,0 +1,58 @@ +package pro.cloudnode.smp.bankaccounts; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.jetbrains.annotations.NotNull; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Optional; +import java.util.logging.Level; + +public final class ModrinthUpdate { + public final @NotNull String version; + public final @NotNull String name; + + public ModrinthUpdate(final @NotNull String version, final @NotNull String name) { + this.version = version; + this.name = name; + } + + public final @NotNull String url() { + return "https://modrinth.com/plugin/bankaccounts/version/" + this.version; + } + + /** + * Query the Modrinth versions API for a compatible update. + * + * @return The latest version if an update is available, otherwise empty. + */ + public static @NotNull Optional<@NotNull ModrinthUpdate> checkForUpdates() { + final @NotNull BankAccounts plugin = BankAccounts.getInstance(); + final @NotNull String mcVersion = plugin.getServer().getMinecraftVersion(); + final @NotNull String pluginName = plugin.getPluginMeta().getName(); + final @NotNull String pluginVersion = plugin.getPluginMeta().getVersion(); + try { + final @NotNull HttpRequest req = HttpRequest.newBuilder() + .uri(URI.create("https://api.modrinth.com/v2/project/Dc8RS2En/version?version_type=release&game_versions=[%22" + mcVersion + "%22]")) + .header("User-Agent", pluginName + "/" + pluginVersion).GET().build(); + final @NotNull HttpResponse<@NotNull String> res = HttpClient.newHttpClient() + .send(req, HttpResponse.BodyHandlers.ofString()); + if (res.statusCode() >= 400 || res.statusCode() < 200 || res.body() == null || JsonParser + .parseString(res.body()).getAsJsonArray().isEmpty()) return Optional.empty(); + final @NotNull JsonObject json = JsonParser.parseString(res.body()).getAsJsonArray().get(0) + .getAsJsonObject(); + final @NotNull String version = json.get("version_number").getAsString(); + final @NotNull String name = json.get("name").getAsString(); + final @NotNull ModrinthUpdate latest = new ModrinthUpdate(version, name); + if (!latest.version.equals(pluginVersion)) return Optional.of(latest); + return Optional.empty(); + } + catch (final @NotNull Exception e) { + plugin.getLogger().log(Level.WARNING, "Failed to check for updates", e); + return Optional.empty(); + } + } +} diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java b/src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java index 453d3c5..ebc8493 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java @@ -9,6 +9,7 @@ import pro.cloudnode.smp.bankaccounts.Account; import pro.cloudnode.smp.bankaccounts.BankAccounts; import pro.cloudnode.smp.bankaccounts.Invoice; +import pro.cloudnode.smp.bankaccounts.ModrinthUpdate; import pro.cloudnode.smp.bankaccounts.Permissions; import java.math.BigDecimal; @@ -33,7 +34,7 @@ else if (BankAccounts.getInstance().config().integrationsVaultEnabled()) { } }); if (player.hasPermission(Permissions.NOTIFY_UPDATE)) { - BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion -> + BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> ModrinthUpdate.checkForUpdates().ifPresent(latestVersion -> player.sendMessage(BankAccounts.getInstance().config().messagesUpdateAvailable(latestVersion)) ), 20L); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 854f780..18f07da 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -668,7 +668,15 @@ messages: # A new version of the plugin is available. # Placeholders: # → The new BankAccounts version number, e.g. ‘1.10.1’. - update-available: Click to view update - Changelog & release notes - Download links'>>[BankAccounts] A new version has been released.Please update to get the latest bug fixes and features. > Click to view update + # → The name of the update, either something like ‘BankAccounts 1.10.1’, or a short description/title. + # → The URL where you can see the changelog and download the update. + # → The version number of BankAccounts you are currently running. + update-available: |- + Click to view update - Changelog & release notes - Download links'>>[BankAccounts] A new update has been released: . + Current: · Latest: + + Please update to get the latest bug fixes and features. + > Click to view update # Error messages. errors: