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) {