-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllianceManager.java
55 lines (45 loc) · 1.51 KB
/
AllianceManager.java
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
package frc.robot.ShamLib;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class AllianceManager {
private static DriverStation.Alliance alliance = DriverStation.Alliance.Red;
private static boolean overrideAlliance = false;
// List of runnables to execute whenever the alliance changes
private static List<Runnable> allianceChangeHooks = new ArrayList<>();
public static void applyAlliance(Optional<DriverStation.Alliance> newAlliance) {
if (!overrideAlliance && newAlliance.isPresent()) {
setAlliance(newAlliance.get());
}
}
public static InstantCommand switchAlliance() {
return new WhileDisabledInstantCommand(
() -> {
setAlliance(alliance == Alliance.Red ? Alliance.Blue : Alliance.Red);
overrideAlliance = true;
});
}
public static InstantCommand syncAlliance() {
return new WhileDisabledInstantCommand(
() -> {
applyAlliance(DriverStation.getAlliance());
overrideAlliance = false;
});
}
private static void setAlliance(Alliance newAlliance) {
alliance = newAlliance;
allianceChangeHooks.forEach(
(e) -> {
e.run();
});
}
public static Alliance getAlliance() {
return alliance;
}
public static void addAllianceChangeHook(Runnable toRun) {
allianceChangeHooks.add(toRun);
}
}