|
24 | 24 |
|
25 | 25 | import java.util.*; |
26 | 26 | import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ConcurrentLinkedQueue; |
27 | 28 | import java.util.logging.Level; |
28 | 29 |
|
29 | 30 | public class NumberTopHolder extends AgentDataHolder<UUID, Double> { |
@@ -56,15 +57,29 @@ public NumberTopHolder(TopperPlugin instance, String name, Map<String, Object> m |
56 | 57 | .map(String::toLowerCase) |
57 | 58 | .map(Boolean::parseBoolean) |
58 | 59 | .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) -> { |
61 | 69 | if (showErrors) { |
62 | 70 | instance.getLogger().log(Level.WARNING, "Error on getting value for " + name + " from " + uuid + " - " + errorMessage, throwable); |
63 | 71 | } |
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 | + }); |
65 | 80 | updateAgent.setMaxEntryPerCall(instance.get(MainConfig.class).getTaskUpdateEntryPerTick()); |
66 | 81 | addEntryAgent(updateAgent); |
67 | | - addAgent(new SpigotRunnableAgent(updateAgent, AsyncScheduler.get(instance), instance.get(MainConfig.class).getTaskUpdateDelay())); |
| 82 | + addAgent(new SpigotRunnableAgent(updateAgent, AsyncScheduler.get(instance), 0)); |
68 | 83 | List<String> ignorePermissions = CollectionUtils.createStringListFromObject(map.get("ignore-permission"), true); |
69 | 84 | if (!ignorePermissions.isEmpty()) { |
70 | 85 | updateAgent.addFilter(uuid -> { |
|
0 commit comments