Skip to content

Commit 76263e1

Browse files
committed
run all value update in your task
1 parent 5adbd94 commit 76263e1

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/holder/NumberTopHolder.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.*;
2626
import java.util.concurrent.CompletableFuture;
27+
import java.util.concurrent.ConcurrentLinkedQueue;
2728
import java.util.logging.Level;
2829

2930
public class NumberTopHolder extends AgentDataHolder<UUID, Double> {
@@ -56,15 +57,29 @@ public NumberTopHolder(TopperPlugin instance, String name, Map<String, Object> m
5657
.map(String::toLowerCase)
5758
.map(Boolean::parseBoolean)
5859
.orElse(false);
59-
this.updateAgent = new UpdateAgent<>(this, uuid -> CompletableFuture.supplyAsync(() ->
60-
valueProvider.apply(uuid).asOptional((errorMessage, throwable) -> {
60+
Queue<Map.Entry<UUID, CompletableFuture<Optional<Double>>>> queue = new ConcurrentLinkedQueue<>();
61+
addAgent(new SpigotRunnableAgent(() -> {
62+
while (true) {
63+
Map.Entry<UUID, CompletableFuture<Optional<Double>>> entry = queue.poll();
64+
if (entry == null) {
65+
break;
66+
}
67+
UUID uuid = entry.getKey();
68+
Optional<Double> value = valueProvider.apply(uuid).asOptional((errorMessage, throwable) -> {
6169
if (showErrors) {
6270
instance.getLogger().log(Level.WARNING, "Error on getting value for " + name + " from " + uuid + " - " + errorMessage, throwable);
6371
}
64-
}), (isAsync ? AsyncScheduler.get(instance) : GlobalScheduler.get(instance)).getExecutor()));
72+
});
73+
entry.getValue().complete(value);
74+
}
75+
}, isAsync ? AsyncScheduler.get(instance) : GlobalScheduler.get(instance), instance.get(MainConfig.class).getTaskUpdateDelay()));
76+
this.updateAgent = new UpdateAgent<>(this, uuid -> {
77+
CompletableFuture<Optional<Double>> future = new CompletableFuture<>();
78+
return queue.offer(new AbstractMap.SimpleEntry<>(uuid, future)) ? future : CompletableFuture.completedFuture(Optional.empty());
79+
});
6580
updateAgent.setMaxEntryPerCall(instance.get(MainConfig.class).getTaskUpdateEntryPerTick());
6681
addEntryAgent(updateAgent);
67-
addAgent(new SpigotRunnableAgent(updateAgent, AsyncScheduler.get(instance), instance.get(MainConfig.class).getTaskUpdateDelay()));
82+
addAgent(new SpigotRunnableAgent(updateAgent, AsyncScheduler.get(instance), 0));
6883
List<String> ignorePermissions = CollectionUtils.createStringListFromObject(map.get("ignore-permission"), true);
6984
if (!ignorePermissions.isEmpty()) {
7085
updateAgent.addFilter(uuid -> {

0 commit comments

Comments
 (0)