Refactor Countdowns
Dieser Commit ist enthalten in:
Ursprung
ac499b32b8
Commit
2984d6886a
72
src/de/steamwar/towerrun/countdowns/Countdown.java
Normale Datei
72
src/de/steamwar/towerrun/countdowns/Countdown.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<GameStates> 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();
|
||||||
|
}
|
||||||
|
}
|
@ -29,29 +29,28 @@ import org.bukkit.scheduler.BukkitTask;
|
|||||||
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
public class EndCountdown extends GameStateToggleListener {
|
public class EndCountdown extends Countdown {
|
||||||
|
|
||||||
private BukkitTask task;
|
|
||||||
private int time = 10;
|
|
||||||
public EndCountdown() {
|
public EndCountdown() {
|
||||||
super(EnumSet.of(GameStates.ENDING));
|
super(EnumSet.of(GameStates.ENDING));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void timer() {
|
@Override
|
||||||
if (time == 0) {
|
int defaultTime() {
|
||||||
Bukkit.shutdown();
|
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...")));
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,38 +31,37 @@ import org.bukkit.scheduler.BukkitTask;
|
|||||||
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
public class LobbyCountdown extends GameStateToggleListener {
|
public class LobbyCountdown extends Countdown {
|
||||||
|
|
||||||
private BukkitTask task;
|
|
||||||
private int timer = Config.LOBBY_TIMER;
|
|
||||||
|
|
||||||
public LobbyCountdown() {
|
public LobbyCountdown() {
|
||||||
super(EnumSet.of(GameStates.LOBBY));
|
super(EnumSet.of(GameStates.LOBBY));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void timer() {
|
@Override
|
||||||
if (Bukkit.getOnlinePlayers().size() >= Config.MIN_PLAYERS) {
|
int defaultTime() {
|
||||||
Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§aStartet: " + timer)));
|
return Config.LOBBY_TIMER;
|
||||||
timer--;
|
}
|
||||||
if (timer < 10) {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(player -> player.playSound(player.getLocation(), "note.pling", 1, 1));
|
@Override
|
||||||
}
|
void timerEnd() {
|
||||||
if (timer == 0) {
|
TowerRunGame.start();
|
||||||
TowerRunGame.start();
|
}
|
||||||
}
|
|
||||||
} else {
|
@Override
|
||||||
timer = Config.LOBBY_TIMER;
|
boolean timerShouldCancel() {
|
||||||
Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cEs wird noch auf Spieler gewartet...")));
|
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
|
@Override
|
||||||
public void enable() {
|
void timerReset() {
|
||||||
task = Bukkit.getScheduler().runTaskTimer(TowerRun.getInstance(), this::timer, 0, 20);
|
Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cEs wird noch auf Spieler gewartet...")));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disable() {
|
|
||||||
task.cancel();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren