Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserInfo> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FetcherTask implements Runnable {

private Instant lastFullDataSent = Instant.MIN;
private Instant lastRequest = Instant.MIN;
private volatile CompletableFuture<Void> lastFetch = CompletableFuture.completedFuture(null);

public FetcherTask(AzLinkPlugin plugin) {
this.plugin = plugin;
Expand All @@ -37,11 +38,15 @@ public void run() {
public CompletableFuture<Void> 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;

Expand All @@ -52,9 +57,13 @@ public CompletableFuture<Void> fetch() {
boolean sendFullData = currentTime.getMinute() % 15 == 0
&& this.lastFullDataSent.isBefore(now.minusSeconds(60));

return CompletableFuture.supplyAsync(() -> this.plugin.getServerData(sendFullData), sync)
CompletableFuture<Void> 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) {
Expand Down
Loading