|
| 1 | +package me.hsgamer.topper.template.topplayernumber.holder; |
| 2 | + |
| 3 | +import me.hsgamer.topper.agent.core.Agent; |
| 4 | +import me.hsgamer.topper.agent.core.AgentHolder; |
| 5 | +import me.hsgamer.topper.agent.core.DataEntryAgent; |
| 6 | +import me.hsgamer.topper.agent.snapshot.SnapshotAgent; |
| 7 | +import me.hsgamer.topper.agent.storage.StorageAgent; |
| 8 | +import me.hsgamer.topper.agent.update.UpdateAgent; |
| 9 | +import me.hsgamer.topper.data.core.DataEntry; |
| 10 | +import me.hsgamer.topper.data.simple.SimpleDataHolder; |
| 11 | +import me.hsgamer.topper.template.topplayernumber.TopPlayerNumberTemplate; |
| 12 | +import me.hsgamer.topper.template.topplayernumber.holder.display.ValueDisplay; |
| 13 | +import me.hsgamer.topper.template.topplayernumber.manager.EntryConsumeManager; |
| 14 | +import me.hsgamer.topper.value.core.ValueProvider; |
| 15 | +import me.hsgamer.topper.value.core.ValueWrapper; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +import java.util.*; |
| 19 | + |
| 20 | +public class NumberTopHolder extends SimpleDataHolder<UUID, Double> implements AgentHolder<UUID, Double> { |
| 21 | + public static final String GROUP = "topper"; |
| 22 | + |
| 23 | + private final ValueDisplay valueDisplay; |
| 24 | + private final List<Agent> agents; |
| 25 | + private final List<DataEntryAgent<UUID, Double>> entryAgents; |
| 26 | + private final StorageAgent<UUID, Double> storageAgent; |
| 27 | + private final UpdateAgent<UUID, Double> updateAgent; |
| 28 | + private final SnapshotAgent<UUID, Double> snapshotAgent; |
| 29 | + private final Double defaultValue; |
| 30 | + |
| 31 | + public NumberTopHolder(TopPlayerNumberTemplate template, String name, Settings settings) { |
| 32 | + this.defaultValue = settings.defaultValue(); |
| 33 | + |
| 34 | + List<Agent> agents = new ArrayList<>(); |
| 35 | + List<DataEntryAgent<UUID, Double>> entryAgents = new ArrayList<>(); |
| 36 | + this.valueDisplay = new ValueDisplay(template, settings); |
| 37 | + |
| 38 | + this.storageAgent = new StorageAgent<>(template.getTopManager().buildStorage(name)); |
| 39 | + storageAgent.setMaxEntryPerCall(template.getSettings().taskSaveEntryPerTick()); |
| 40 | + agents.add(storageAgent); |
| 41 | + agents.add(storageAgent.getLoadAgent(this)); |
| 42 | + agents.add(template.createTaskAgent(storageAgent, true, template.getSettings().taskSaveDelay())); |
| 43 | + entryAgents.add(storageAgent); |
| 44 | + |
| 45 | + ValueProvider<UUID, Double> valueProvider = template.createValueProvider(settings.valueProvider()).orElseGet(() -> { |
| 46 | + template.logWarning("No value provider found for " + name); |
| 47 | + return ValueProvider.empty(); |
| 48 | + }); |
| 49 | + boolean isAsync = settings.async(); |
| 50 | + boolean showErrors = settings.showErrors(); |
| 51 | + boolean resetOnError = settings.resetOnError(); |
| 52 | + List<String> ignorePermissions = settings.ignorePermissions(); |
| 53 | + List<String> resetPermissions = settings.resetPermissions(); |
| 54 | + this.updateAgent = new UpdateAgent<>(this, valueProvider); |
| 55 | + if (!ignorePermissions.isEmpty() || !resetPermissions.isEmpty()) { |
| 56 | + updateAgent.setFilter(uuid -> { |
| 57 | + if (!template.isOnline(uuid)) { |
| 58 | + return UpdateAgent.FilterResult.SKIP; |
| 59 | + } |
| 60 | + if (!resetPermissions.isEmpty() && resetPermissions.stream().anyMatch(s -> template.hasPermission(uuid, s))) { |
| 61 | + return UpdateAgent.FilterResult.RESET; |
| 62 | + } |
| 63 | + if (!ignorePermissions.isEmpty() && ignorePermissions.stream().anyMatch(s -> template.hasPermission(uuid, s))) { |
| 64 | + return UpdateAgent.FilterResult.SKIP; |
| 65 | + } |
| 66 | + return UpdateAgent.FilterResult.CONTINUE; |
| 67 | + }); |
| 68 | + } |
| 69 | + if (resetOnError) { |
| 70 | + updateAgent.setErrorHandler((uuid, valueWrapper) -> { |
| 71 | + if (showErrors && valueWrapper.state == ValueWrapper.State.ERROR) { |
| 72 | + template.logWarning("Error on getting value for " + name + " from " + uuid + " - " + valueWrapper.errorMessage, valueWrapper.throwable); |
| 73 | + } |
| 74 | + return ValueWrapper.handled(defaultValue); |
| 75 | + }); |
| 76 | + } else if (showErrors) { |
| 77 | + updateAgent.setErrorHandler((uuid, valueWrapper) -> { |
| 78 | + if (valueWrapper.state == ValueWrapper.State.ERROR) { |
| 79 | + template.logWarning("Error on getting value for " + name + " from " + uuid + " - " + valueWrapper.errorMessage, valueWrapper.throwable); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + updateAgent.setMaxSkips(template.getSettings().taskUpdateMaxSkips()); |
| 84 | + entryAgents.add(updateAgent); |
| 85 | + agents.add(template.createTaskAgent(updateAgent.getUpdateRunnable(template.getSettings().taskUpdateEntryPerTick()), isAsync, template.getSettings().taskUpdateDelay())); |
| 86 | + agents.add(template.createTaskAgent(updateAgent.getSetRunnable(), true, template.getSettings().taskUpdateSetDelay())); |
| 87 | + |
| 88 | + this.snapshotAgent = SnapshotAgent.create(this); |
| 89 | + boolean reverseOrder = settings.reverse(); |
| 90 | + snapshotAgent.setComparator(reverseOrder ? Comparator.naturalOrder() : Comparator.reverseOrder()); |
| 91 | + snapshotAgent.setFilter(entry -> entry.getValue() != null); |
| 92 | + agents.add(snapshotAgent); |
| 93 | + agents.add(template.createTaskAgent(snapshotAgent, true, 20L)); |
| 94 | + |
| 95 | + entryAgents.add(new DataEntryAgent<UUID, Double>() { |
| 96 | + @Override |
| 97 | + public void onUpdate(DataEntry<UUID, Double> entry, Double oldValue, Double newValue) { |
| 98 | + template.getEntryConsumeManager().consume(new EntryConsumeManager.Context( |
| 99 | + GROUP, |
| 100 | + name, |
| 101 | + entry.getKey(), |
| 102 | + oldValue, |
| 103 | + newValue |
| 104 | + )); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + template.modifyAgents(this, agents, entryAgents); |
| 109 | + this.agents = Collections.unmodifiableList(agents); |
| 110 | + this.entryAgents = Collections.unmodifiableList(entryAgents); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public @Nullable Double getDefaultValue() { |
| 115 | + return defaultValue; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public List<Agent> getAgents() { |
| 120 | + return agents; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public List<DataEntryAgent<UUID, Double>> getEntryAgents() { |
| 125 | + return entryAgents; |
| 126 | + } |
| 127 | + |
| 128 | + public StorageAgent<UUID, Double> getStorageAgent() { |
| 129 | + return storageAgent; |
| 130 | + } |
| 131 | + |
| 132 | + public UpdateAgent<UUID, Double> getUpdateAgent() { |
| 133 | + return updateAgent; |
| 134 | + } |
| 135 | + |
| 136 | + public SnapshotAgent<UUID, Double> getSnapshotAgent() { |
| 137 | + return snapshotAgent; |
| 138 | + } |
| 139 | + |
| 140 | + public ValueDisplay getValueDisplay() { |
| 141 | + return valueDisplay; |
| 142 | + } |
| 143 | + |
| 144 | + public interface Settings { |
| 145 | + Double defaultValue(); |
| 146 | + |
| 147 | + String defaultLine(); |
| 148 | + |
| 149 | + String displayNullName(); |
| 150 | + |
| 151 | + String displayNullUuid(); |
| 152 | + |
| 153 | + String displayNullValue(); |
| 154 | + |
| 155 | + boolean async(); |
| 156 | + |
| 157 | + boolean showErrors(); |
| 158 | + |
| 159 | + boolean resetOnError(); |
| 160 | + |
| 161 | + boolean reverse(); |
| 162 | + |
| 163 | + List<String> ignorePermissions(); |
| 164 | + |
| 165 | + List<String> resetPermissions(); |
| 166 | + |
| 167 | + Map<String, Object> valueProvider(); |
| 168 | + } |
| 169 | +} |
0 commit comments