diff --git a/src/de/steamwar/towerrun/countdowns/Countdown.java b/src/de/steamwar/towerrun/countdowns/Countdown.java new file mode 100644 index 0000000..a0e1879 --- /dev/null +++ b/src/de/steamwar/towerrun/countdowns/Countdown.java @@ -0,0 +1,72 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.towerrun.countdowns; + +import de.steamwar.towerrun.TowerRun; +import de.steamwar.towerrun.state.GameStateToggleListener; +import de.steamwar.towerrun.state.GameStates; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; + +import java.util.EnumSet; + +public abstract class Countdown extends GameStateToggleListener { + + protected int time = defaultTime(); + protected BukkitTask task; + + protected Countdown(EnumSet enabledStates) { + super(enabledStates); + } + + int defaultTime() {return 0;} + void timerEnd() {} + boolean timerShouldCancel() {return true;} + void run() {} + void timerStart() {} + void timerReset() {} + + private void timer() { + if (timerShouldCancel()) { + time = defaultTime(); + timerReset(); + } else { + if (time == defaultTime()) { + timerStart(); + } + if (time == 0) { + timerEnd(); + return; + } + run(); + time--; + } + } + + @Override + public void enable() { + task = Bukkit.getScheduler().runTaskTimer(TowerRun.getInstance(), this::timer, 0, 20); + } + + @Override + public void disable() { + task.cancel(); + } +} diff --git a/src/de/steamwar/towerrun/countdowns/EndCountdown.java b/src/de/steamwar/towerrun/countdowns/EndCountdown.java index 653f8ac..166e8ea 100644 --- a/src/de/steamwar/towerrun/countdowns/EndCountdown.java +++ b/src/de/steamwar/towerrun/countdowns/EndCountdown.java @@ -29,29 +29,28 @@ import org.bukkit.scheduler.BukkitTask; import java.util.EnumSet; -public class EndCountdown extends GameStateToggleListener { - - private BukkitTask task; - private int time = 10; +public class EndCountdown extends Countdown { public EndCountdown() { super(EnumSet.of(GameStates.ENDING)); } - private void timer() { - if (time == 0) { - Bukkit.shutdown(); - } + @Override + int defaultTime() { + return 10; + } + + @Override + void timerEnd() { + Bukkit.shutdown(); + } + + @Override + boolean timerShouldCancel() { + return false; + } + + @Override + void run() { Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cServer wird in " + time + " Sekunden heruntergefahren..."))); - time--; - } - - @Override - public void enable() { - task = Bukkit.getScheduler().runTaskTimer(TowerRun.getInstance(), this::timer, 0, 20); - } - - @Override - public void disable() { - task.cancel(); } } diff --git a/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java b/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java index d0e7b21..def1df5 100644 --- a/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java +++ b/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java @@ -31,38 +31,37 @@ import org.bukkit.scheduler.BukkitTask; import java.util.EnumSet; -public class LobbyCountdown extends GameStateToggleListener { - - private BukkitTask task; - private int timer = Config.LOBBY_TIMER; +public class LobbyCountdown extends Countdown { public LobbyCountdown() { super(EnumSet.of(GameStates.LOBBY)); } - private void timer() { - if (Bukkit.getOnlinePlayers().size() >= Config.MIN_PLAYERS) { - Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§aStartet: " + timer))); - timer--; - if (timer < 10) { - Bukkit.getOnlinePlayers().forEach(player -> player.playSound(player.getLocation(), "note.pling", 1, 1)); - } - if (timer == 0) { - TowerRunGame.start(); - } - } else { - timer = Config.LOBBY_TIMER; - Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cEs wird noch auf Spieler gewartet..."))); + @Override + int defaultTime() { + return Config.LOBBY_TIMER; + } + + @Override + void timerEnd() { + TowerRunGame.start(); + } + + @Override + boolean timerShouldCancel() { + return super.timerShouldCancel(); + } + + @Override + void run() { + Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§aStartet: " + time))); + if (time < 10) { + Bukkit.getOnlinePlayers().forEach(player -> player.playSound(player.getLocation(), "note.pling", 1, 1)); } } @Override - public void enable() { - task = Bukkit.getScheduler().runTaskTimer(TowerRun.getInstance(), this::timer, 0, 20); - } - - @Override - public void disable() { - task.cancel(); + void timerReset() { + Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cEs wird noch auf Spieler gewartet..."))); } }