From 9419b665d3e7ddcddf78e5be57f90aa3bff56436 Mon Sep 17 00:00:00 2001 From: Cry_Legende Date: Sun, 1 Nov 2026 19:46:36 +0100 Subject: [PATCH] Fetch users before giving up on an unknown player in /azlink money A player who joined after the last data fetch is not in the user cache yet, so '/azlink money' failed with 'Unable to find player' for up to a minute after a join. Commands queued for delivery on join (vote rewards...) were lost this way. When the target player is online but unknown, fetch the data and retry once the response is handled. FetcherTask.fetch() now returns the pending request instead of an already completed future when it is called again within five seconds, so callers can wait for its response. --- .../azlink/common/command/AzLinkCommand.java | 37 +++++++++++++++++-- .../azlink/common/tasks/FetcherTask.java | 15 ++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/com/azuriom/azlink/common/command/AzLinkCommand.java b/common/src/main/java/com/azuriom/azlink/common/command/AzLinkCommand.java index d88de3b..ad15ab2 100644 --- a/common/src/main/java/com/azuriom/azlink/common/command/AzLinkCommand.java +++ b/common/src/main/java/com/azuriom/azlink/common/command/AzLinkCommand.java @@ -162,12 +162,43 @@ public void editMoney(CommandSender sender, String[] args) throws NumberFormatEx return; } - if (!user.isPresent()) { - sender.sendMessage(TextComponent.text("Unable to find player '" + args[2] + "', please try again in few seconds or use '/azlink fetch'.", TextColor.RED)); + if (user.isPresent()) { + editMoney(sender, user.get(), action, amount); return; } - this.plugin.getUserManager().editMoney(user.get(), action, amount) + String name = args[2]; + + if (!isOnline(name)) { + sendUnknownPlayer(sender, name); + return; + } + + // The player may have joined after the last data fetch, refresh the users before giving up + MoneyAction moneyAction = action; + + this.plugin.fetch().whenComplete((v, ex) -> { + Optional fetchedUser = this.plugin.getUserManager().getUserByName(name); + + if (fetchedUser.isPresent()) { + editMoney(sender, fetchedUser.get(), moneyAction, amount); + } else { + sendUnknownPlayer(sender, name); + } + }); + } + + private boolean isOnline(String name) { + return this.plugin.getPlatform().getOnlinePlayers() + .anyMatch(player -> player.getName().equalsIgnoreCase(name)); + } + + private void sendUnknownPlayer(CommandSender sender, String name) { + sender.sendMessage(TextComponent.text("Unable to find player '" + name + "', please try again in few seconds or use '/azlink fetch'.", TextColor.RED)); + } + + private void editMoney(CommandSender sender, UserInfo user, MoneyAction action, double amount) { + this.plugin.getUserManager().editMoney(user, action, amount) .thenAccept(u -> { sender.sendMessage(TextComponent.text("Money has been edited successfully.", TextColor.GREEN)); sender.sendMessage(TextComponent.text("New balance: " + u.getMoney(), TextColor.GREEN)); diff --git a/common/src/main/java/com/azuriom/azlink/common/tasks/FetcherTask.java b/common/src/main/java/com/azuriom/azlink/common/tasks/FetcherTask.java index 3ea805b..5db38b1 100644 --- a/common/src/main/java/com/azuriom/azlink/common/tasks/FetcherTask.java +++ b/common/src/main/java/com/azuriom/azlink/common/tasks/FetcherTask.java @@ -20,6 +20,7 @@ public class FetcherTask implements Runnable { private Instant lastFullDataSent = Instant.MIN; private Instant lastRequest = Instant.MIN; + private volatile CompletableFuture lastFetch = CompletableFuture.completedFuture(null); public FetcherTask(AzLinkPlugin plugin) { this.plugin = plugin; @@ -37,11 +38,15 @@ public void run() { public CompletableFuture fetch() { Instant now = Instant.now(); - if (!this.plugin.getConfig().isValid() - || this.lastRequest.isAfter(now.minusSeconds(5))) { + if (!this.plugin.getConfig().isValid()) { return CompletableFuture.completedFuture(null); } + if (this.lastRequest.isAfter(now.minusSeconds(5))) { + // A request was just sent, let callers wait for its response instead of returning immediately + return this.lastFetch; + } + this.plugin.getPlatform().prepareDataAsync(); this.lastRequest = now; @@ -52,9 +57,13 @@ public CompletableFuture fetch() { boolean sendFullData = currentTime.getMinute() % 15 == 0 && this.lastFullDataSent.isBefore(now.minusSeconds(60)); - return CompletableFuture.supplyAsync(() -> this.plugin.getServerData(sendFullData), sync) + CompletableFuture fetch = CompletableFuture.supplyAsync(() -> this.plugin.getServerData(sendFullData), sync) .thenComposeAsync(this.plugin.getHttpClient()::postData, async) .thenAcceptAsync(res -> handleResponse(res, sendFullData), sync); + + this.lastFetch = fetch; + + return fetch; } private void handleResponse(WebsiteResponse response, boolean sendFullData) {