Skip to content

Commit f6fbe58

Browse files
authored
Improved the update checker (#157)
2 parents 191c7be + 7889043 commit f6fbe58

File tree

5 files changed

+78
-42
lines changed

5 files changed

+78
-42
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ public static void reload() {
158158
getInstance().initDbWrapper();
159159
createServerAccount();
160160
createServerVaultAccount();
161-
getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> checkForUpdates().ifPresent(latestVersion -> {
162-
getInstance().getLogger().warning("An update is available: " + latestVersion);
161+
getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> ModrinthUpdate.checkForUpdates().ifPresent(update -> {
162+
getInstance().getLogger().warning("An update is available: " + update.name);
163+
getInstance().getLogger().warning("You are running: " + BankAccounts.getInstance().getPluginMeta().getVersion() + " · Latest version: " + update.version);
163164
getInstance().getLogger().warning("Please update to the latest version to benefit from bug fixes, security patches, new features and support.");
164-
getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion);
165+
getInstance().getLogger().warning("Update details: " + update.url());
165166
}));
166167
getInstance().startInterestTimer();
167168
if (getInstance().invoiceNotificationTask != null) {
@@ -394,41 +395,6 @@ private static void createServerVaultAccount() {
394395
return sender instanceof OfflinePlayer ? (OfflinePlayer) sender : getConsoleOfflinePlayer();
395396
}
396397

397-
/**
398-
* Check for plugin updates using Modrinth API
399-
*
400-
* @return The latest version if an update is available, otherwise empty
401-
*/
402-
public static Optional<String> checkForUpdates() {
403-
final @NotNull BankAccounts plugin = BankAccounts.getInstance();
404-
final @NotNull String mcVersion = plugin.getServer().getMinecraftVersion();
405-
final @NotNull String pluginName = plugin.getPluginMeta().getName();
406-
final @NotNull String pluginVersion = plugin.getPluginMeta().getVersion();
407-
try {
408-
final @NotNull HttpClient client = HttpClient.newHttpClient();
409-
final @NotNull HttpRequest req = HttpRequest.newBuilder()
410-
.uri(URI.create("https://api.modrinth.com/v2/project/Dc8RS2En/version?featured=true&game_versions=[%22" + mcVersion + "%22]"))
411-
.header("User-Agent",
412-
pluginName + "/" + pluginVersion
413-
)
414-
.GET()
415-
.build();
416-
final @NotNull HttpResponse<String> res = client.send(req, HttpResponse.BodyHandlers.ofString());
417-
if (res.statusCode() < 400 && res.statusCode() >= 200 && res.body() != null && !(JsonParser.parseString(res.body()).getAsJsonArray().isEmpty())) {
418-
final @NotNull JsonObject json = JsonParser.parseString(res.body()).getAsJsonArray().get(0).getAsJsonObject();
419-
if (json.has("version_number")) {
420-
final @NotNull String latestVersion = json.get("version_number").getAsString();
421-
if (!latestVersion.equals(pluginVersion))
422-
return Optional.of(latestVersion);
423-
}
424-
}
425-
}
426-
catch (final @NotNull Exception e) {
427-
plugin.getLogger().log(Level.WARNING, "Failed to check for updates", e);
428-
}
429-
return Optional.empty();
430-
}
431-
432398
/**
433399
* Run a task on the main thread
434400
* @param task The task to run

src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,10 +1451,13 @@ public int invoiceNotifyInterval() {
14511451
}
14521452

14531453
// messages.update-available
1454-
public @NotNull Component messagesUpdateAvailable(final @NotNull String version) {
1454+
public @NotNull Component messagesUpdateAvailable(final @NotNull ModrinthUpdate update) {
14551455
return MiniMessage.miniMessage().deserialize(
14561456
Objects.requireNonNull(config.getString("messages.update-available"))
1457-
.replace("<version>", version)
1457+
.replace("<version>", update.version)
1458+
.replace("<name>", update.name)
1459+
.replace("<url>", update.url())
1460+
.replace("<current>", BankAccounts.getInstance().getPluginMeta().getVersion())
14581461
);
14591462
}
14601463

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package pro.cloudnode.smp.bankaccounts;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.net.URI;
8+
import java.net.http.HttpClient;
9+
import java.net.http.HttpRequest;
10+
import java.net.http.HttpResponse;
11+
import java.util.Optional;
12+
import java.util.logging.Level;
13+
14+
public final class ModrinthUpdate {
15+
public final @NotNull String version;
16+
public final @NotNull String name;
17+
18+
public ModrinthUpdate(final @NotNull String version, final @NotNull String name) {
19+
this.version = version;
20+
this.name = name;
21+
}
22+
23+
public final @NotNull String url() {
24+
return "https://modrinth.com/plugin/bankaccounts/version/" + this.version;
25+
}
26+
27+
/**
28+
* Query the Modrinth versions API for a compatible update.
29+
*
30+
* @return The latest version if an update is available, otherwise empty.
31+
*/
32+
public static @NotNull Optional<@NotNull ModrinthUpdate> checkForUpdates() {
33+
final @NotNull BankAccounts plugin = BankAccounts.getInstance();
34+
final @NotNull String mcVersion = plugin.getServer().getMinecraftVersion();
35+
final @NotNull String pluginName = plugin.getPluginMeta().getName();
36+
final @NotNull String pluginVersion = plugin.getPluginMeta().getVersion();
37+
try {
38+
final @NotNull HttpRequest req = HttpRequest.newBuilder()
39+
.uri(URI.create("https://api.modrinth.com/v2/project/Dc8RS2En/version?version_type=release&game_versions=[%22" + mcVersion + "%22]"))
40+
.header("User-Agent", pluginName + "/" + pluginVersion).GET().build();
41+
final @NotNull HttpResponse<@NotNull String> res = HttpClient.newHttpClient()
42+
.send(req, HttpResponse.BodyHandlers.ofString());
43+
if (res.statusCode() >= 400 || res.statusCode() < 200 || res.body() == null || JsonParser
44+
.parseString(res.body()).getAsJsonArray().isEmpty()) return Optional.empty();
45+
final @NotNull JsonObject json = JsonParser.parseString(res.body()).getAsJsonArray().get(0)
46+
.getAsJsonObject();
47+
final @NotNull String version = json.get("version_number").getAsString();
48+
final @NotNull String name = json.get("name").getAsString();
49+
final @NotNull ModrinthUpdate latest = new ModrinthUpdate(version, name);
50+
if (!latest.version.equals(pluginVersion)) return Optional.of(latest);
51+
return Optional.empty();
52+
}
53+
catch (final @NotNull Exception e) {
54+
plugin.getLogger().log(Level.WARNING, "Failed to check for updates", e);
55+
return Optional.empty();
56+
}
57+
}
58+
}

src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pro.cloudnode.smp.bankaccounts.Account;
1010
import pro.cloudnode.smp.bankaccounts.BankAccounts;
1111
import pro.cloudnode.smp.bankaccounts.Invoice;
12+
import pro.cloudnode.smp.bankaccounts.ModrinthUpdate;
1213
import pro.cloudnode.smp.bankaccounts.Permissions;
1314

1415
import java.math.BigDecimal;
@@ -33,7 +34,7 @@ else if (BankAccounts.getInstance().config().integrationsVaultEnabled()) {
3334
}
3435
});
3536
if (player.hasPermission(Permissions.NOTIFY_UPDATE)) {
36-
BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion ->
37+
BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> ModrinthUpdate.checkForUpdates().ifPresent(latestVersion ->
3738
player.sendMessage(BankAccounts.getInstance().config().messagesUpdateAvailable(latestVersion))
3839
), 20L);
3940
}

src/main/resources/config.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,15 @@ messages:
668668
# A new version of the plugin is available.
669669
# Placeholders:
670670
# <version> → The new BankAccounts version number, e.g. ‘1.10.1’.
671-
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>
671+
# <name> → The name of the update, either something like ‘BankAccounts 1.10.1’, or a short description/title.
672+
# <url> → The URL where you can see the changelog and download the update.
673+
# <current> → The version number of BankAccounts you are currently running.
674+
update-available: |-
675+
<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>
676+
<gray>Current: <white><current></white> <dark_gray>·</dark_gray> Latest: <green><version></green></gray>
677+
678+
<white>Please update to get the latest bug fixes and features.</white>
679+
<green>> Click to view update<green></click></hover>
672680
673681
# Error messages.
674682
errors:

0 commit comments

Comments
 (0)