-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTriggerCheckTask.java
More file actions
90 lines (80 loc) · 4.71 KB
/
TriggerCheckTask.java
File metadata and controls
90 lines (80 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.froobworld.farmcontrol.controller.task;
import com.froobworld.farmcontrol.FarmControl;
import com.froobworld.farmcontrol.controller.ActionProfile;
import com.froobworld.farmcontrol.controller.FarmController;
import com.froobworld.farmcontrol.controller.entity.SnapshotEntity;
import com.froobworld.farmcontrol.controller.tracker.CycleTracker;
import com.froobworld.farmcontrol.controller.trigger.Trigger;
import com.froobworld.farmcontrol.controller.trigger.UntriggerStrategy;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.bukkit.World;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TriggerCheckTask implements Runnable {
private final FarmControl farmControl;
private final FarmController farmController;
private final ExecutorService executorService;
private final Map<World, Map<Trigger, Set<ActionProfile>>> worldTriggerProfilesMap;
private final Map<UUID, Map<Trigger, Integer>> worldLastTriggerCount = new HashMap<>();
public TriggerCheckTask(FarmControl farmControl, FarmController farmController, Map<World, Map<Trigger, Set<ActionProfile>>> worldTriggerProfilesMap) {
this.farmControl = farmControl;
this.farmController = farmController;
executorService = Executors.newFixedThreadPool(1,
new ThreadFactoryBuilder()
.setThreadFactory(Executors.defaultThreadFactory())
.setNameFormat("farmcontrol-worker-%d")
.build()
);
this.worldTriggerProfilesMap = worldTriggerProfilesMap;
}
public void run() {
CycleTracker cycleTracker = farmController.getCycleHistoryManager().startCycleTracker(worldTriggerProfilesMap.keySet());
for (World world : worldTriggerProfilesMap.keySet()) {
worldLastTriggerCount.putIfAbsent(world.getUID(), new HashMap<>());
Map<Trigger, Set<ActionProfile>> triggerProfilesMap = worldTriggerProfilesMap.get(world);
Map<Trigger, Set<ActionProfile>> profilesToRun = new HashMap<>();
Map<Trigger, UntriggerStrategy> untriggerStrategyMap = new HashMap<>();
Set<Trigger> triggeredTriggers = new HashSet<>();
for (Trigger trigger : triggerProfilesMap.keySet()) {
Trigger.TriggerStatus triggerStatus = trigger.getTriggerStatus(world);
if (triggerStatus == Trigger.TriggerStatus.TRIGGERED) {
triggeredTriggers.add(trigger);
profilesToRun.computeIfAbsent(trigger, t -> new HashSet<>()).addAll(triggerProfilesMap.get(trigger));
worldLastTriggerCount.get(world.getUID()).put(trigger, 0);
} else if (triggerStatus == Trigger.TriggerStatus.UNTRIGGERED) {
untriggerStrategyMap.put(trigger, trigger.getUntriggerStrategy(world));
worldLastTriggerCount.get(world.getUID()).compute(trigger, (t, v) -> v == null ? 1 : (v + 1));
}
}
untriggerStrategyMap.entrySet().removeIf(entry -> worldLastTriggerCount.get(world.getUID()).getOrDefault(entry.getKey(), 0) <= entry.getValue().getMinimumCyclesBeforeUndo());
CompletableFuture<List<SnapshotEntity>> completableFuture = CompletableFuture.completedFuture(null);
if (!profilesToRun.isEmpty() || !untriggerStrategyMap.isEmpty()) {
completableFuture = farmControl.getHookManager().getEntityGetterHook().getSnapshotEntities(world, FarmController.ENTITY_CLASSES);
}
completableFuture.thenAccept(snapshotEntities -> {
if (snapshotEntities == null || snapshotEntities.isEmpty()) {
cycleTracker.signalCompletion(world);
return;
}
if (!profilesToRun.isEmpty()) {
executorService.submit(new ActionAllocationTask(farmController, world, farmControl.getHookManager().getSchedulerHook(), triggeredTriggers, snapshotEntities, profilesToRun, farmControl.getExclusionManager().getExclusionPredicate(world), farmControl.getActionManager().getActions(), cycleTracker));
} else {
cycleTracker.signalCompletion(world);
}
if (!untriggerStrategyMap.isEmpty()) {
executorService.submit(new UntriggerAllocationTask(farmControl, farmController, snapshotEntities, untriggerStrategyMap));
}
});
}
}
public void stop() {
executorService.shutdown();
}
}