Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<String> 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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>", version)
.replace("<version>", update.version)
.replace("<name>", update.name)
.replace("<url>", update.url())
.replace("<current>", BankAccounts.getInstance().getPluginMeta().getVersion())
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,15 @@ messages:
# A new version of the plugin is available.
# Placeholders:
# <version> → The new BankAccounts version number, e.g. ‘1.10.1’.
update-available: <hover:show_text:'<white>Click to view update</white><newline><gray> - Changelog & release notes<newline> - Download links'><click:open_url:https://modrinth.com/plugin/bankaccounts/version/<version>><dark_gray>[<green>BankAccounts</green>]</dark_gray> <white>A new version <green><version></green> has been released.</white><newline><gray>Please update to get the latest bug fixes and features.</gray><newline> <green>> Click to view update<green></click></hover>
# <name> → The name of the update, either something like ‘BankAccounts 1.10.1’, or a short description/title.
# <url> → The URL where you can see the changelog and download the update.
# <current> → The version number of BankAccounts you are currently running.
update-available: |-
<hover:show_text:'<white>Click to view update</white><newline><gray> - Changelog & release notes<newline> - Download links'><click:open_url:<url>><dark_gray>[<green>BankAccounts</green>]</dark_gray> <white>A new update has been released: <green><b><name></b></green>.</white>
<gray>Current: <white><current></white> <dark_gray>·</dark_gray> Latest: <green><version></green></gray>

<white>Please update to get the latest bug fixes and features.</white>
<green>> Click to view update<green></click></hover>

# Error messages.
errors:
Expand Down
Loading