Fix executor handling in phase transitions and game ending

Ensure proper management of executor service during CombatPhase and shutdown when the game ends. Avoids potential resource leaks or invalid executor state during phase transitions.
This commit is contained in:
Teriuihi 2025-02-11 23:17:10 +01:00
parent 3b4beefb37
commit 56455b4c50
2 changed files with 10 additions and 6 deletions

View File

@ -58,13 +58,15 @@ public class RunningGame implements Runnable {
}
private void nextPhaseActions(@Nullable GamePhase previousPhase, @NotNull GamePhase phase) {
//TODO command to go to next phase
this.currentPhase = phase;
this.phaseStartTime = Instant.now();
if (previousPhase != null) {
gameManager.getPhaseExecutor(previousPhase).end(phase);
}
gameManager.getPhaseExecutor(phase).start(flag);
if (phase.equals(GamePhase.ENDED)) {
executorService.shutdown();
}
}
private static final List<Integer> NOTIFY_SECONDS = List.of(45, 30, 15, 10, 5, 3, 2, 1);

View File

@ -15,13 +15,15 @@ public class CombatPhase implements GamePhaseExecutor {
private ScheduledExecutorService executorService = null;
@Override
public void start(Flag flag) {
public synchronized void start(Flag flag) {
Bukkit.broadcast(MiniMessage.miniMessage().deserialize("<green>CAPTURE THE FLAG</green>"));
flag.spawnFlag();
if (executorService == null || !executorService.isShutdown()) {
if (executorService != null) {
executorService.shutdown();
}
if (executorService == null) {
executorService = Executors.newSingleThreadScheduledExecutor();
} else if (executorService.isTerminated() || executorService.isShutdown()) {
executorService = Executors.newSingleThreadScheduledExecutor();
} else {
executorService.shutdown();
executorService = Executors.newSingleThreadScheduledExecutor();
}
executorService.scheduleAtFixedRate(flag, 0, 1, TimeUnit.SECONDS);