This repository was archived by the owner on Nov 25, 2019. It is now read-only.
forked from OvercastNetwork/ProjectAres
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathStartCountdown.java
More file actions
135 lines (114 loc) · 4.7 KB
/
StartCountdown.java
File metadata and controls
135 lines (114 loc) · 4.7 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package tc.oc.pgm.start;
import java.time.Duration;
import javax.annotation.Nullable;
import javax.inject.Inject;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import org.bukkit.boss.BarColor;
import org.bukkit.entity.Player;
import tc.oc.commons.core.chat.Component;
import tc.oc.commons.core.util.Comparables;
import tc.oc.pgm.autojoin.AutoJoinMatchModule;
import tc.oc.pgm.match.Match;
import tc.oc.pgm.match.MatchState;
import tc.oc.pgm.teams.Team;
import tc.oc.pgm.teams.TeamMatchModule;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Countdown to team huddle, or match start if huddle is disabled
*/
public class StartCountdown extends PreMatchCountdown {
// At this duration before match start, broadcast a warning if teams will be auto-balanced
private static final Duration BALANCE_WARNING_TIME = Duration.ofSeconds(15);
// TODO: Avoid coupling to the team module, either by subclassing this countdown,
// or implementing some kind of countdown listener system.
private final @Nullable TeamMatchModule tmm;
private final StartMatchModule smm;
private final AutoJoinMatchModule autoJoinMatchModule;
private final Duration huddle;
private boolean autoBalanced, balanceWarningSent;
protected final boolean forced;
public StartCountdown(Match match, boolean forced, Duration huddle) {
super(match);
this.huddle = checkNotNull(huddle);
this.forced = forced;
this.smm = match.needMatchModule(StartMatchModule.class);
this.tmm = match.getMatchModule(TeamMatchModule.class);
this.autoJoinMatchModule = match.needMatchModule(AutoJoinMatchModule.class);
}
protected boolean willHuddle() {
return Comparables.greaterThan(huddle, Duration.ZERO);
}
@Override
public BaseComponent barText(Player viewer) {
return new Component(new TranslatableComponent("countdown.matchStart.message",
secondsRemaining(ChatColor.DARK_RED)),
ChatColor.GREEN);
}
@Override
public BarColor barColor(Player viewer) {
return BarColor.GREEN;
}
@Override
public @Nullable Duration timeUntilMatchStart() {
return remaining == null ? null : remaining.plus(huddle);
}
@Override
public void onStart(Duration remaining, Duration total) {
super.onStart(remaining, total);
match.ensureState(MatchState.Starting);
this.autoBalanced = false;
}
@Override
@SuppressWarnings("deprecation")
public void onTick(Duration remaining, Duration total) {
super.onTick(remaining, total);
if(remaining.getSeconds() <= 10) {
// Autojoin feature - Send the player hotbar messages and alert them that match is starting
autoJoinMatchModule.joiningPlayers()
.forEach(player -> player.sendHotbarMessage(new Component(ChatColor.DARK_PURPLE).translate("autojoin.starting",
String.valueOf(remaining.getSeconds())).bold(true)));
}
if(remaining.getSeconds() >= 1 && remaining.getSeconds() <= 3) {
autoJoinMatchModule.enterAllPlayers();
// Auto-balance runs at match start as well, but try to run it a few seconds in advance
if(this.tmm != null && !this.autoBalanced) {
this.autoBalanced = true;
this.tmm.balanceTeams();
}
}
if(this.tmm != null && !this.autoBalanced && !this.balanceWarningSent && Comparables.lessOrEqual(remaining, BALANCE_WARNING_TIME)) {
for(Team team : this.tmm.getTeams()) {
if(team.isStacked()) {
this.balanceWarningSent = true;
this.getMatch().sendWarning(new TranslatableComponent("team.balanceWarning", team.getComponentName()), false);
}
}
if(this.balanceWarningSent) {
this.getMatch().playSound(COUNT_SOUND);
}
}
}
@Override
public void onEnd(Duration total) {
super.onEnd(total);
if(this.tmm != null) this.tmm.balanceTeams();
if(willHuddle()) {
match.countdowns().start(new HuddleCountdown(getMatch()), huddle);
} else {
match.transitionTo(MatchState.Running);
}
}
@Override
public void onCancel(Duration remaining, Duration total, boolean manual) {
super.onCancel(remaining, total, manual);
if(manual) {
smm.setAutoStart(false);
}
match.ensureState(MatchState.Idle);
}
public boolean isForced() {
return forced;
}
}