Added Scheduled server restarts
This commit is contained in:
parent
33e9e749a0
commit
bbaf4f394b
@ -9,6 +9,7 @@ import eu.mhsl.craftattack.spawn.appliances.event.Event;
|
||||
import eu.mhsl.craftattack.spawn.appliances.help.Help;
|
||||
import eu.mhsl.craftattack.spawn.appliances.playerlimit.PlayerLimit;
|
||||
import eu.mhsl.craftattack.spawn.appliances.report.Report;
|
||||
import eu.mhsl.craftattack.spawn.appliances.restart.Restart;
|
||||
import eu.mhsl.craftattack.spawn.appliances.tablist.Tablist;
|
||||
import eu.mhsl.craftattack.spawn.appliances.titleClear.TitleClear;
|
||||
import eu.mhsl.craftattack.spawn.appliances.whitelist.Whitelist;
|
||||
@ -43,6 +44,7 @@ public final class Main extends JavaPlugin {
|
||||
new Help(),
|
||||
new PlayerLimit(),
|
||||
new Whitelist(),
|
||||
new Restart(),
|
||||
new Debug()
|
||||
);
|
||||
Bukkit.getLogger().info("Loading appliances...");
|
||||
|
@ -0,0 +1,68 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.restart;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.restart.command.CancelRestartCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.restart.command.ScheduleRestartCommand;
|
||||
import eu.mhsl.craftattack.spawn.util.Countdown;
|
||||
import eu.mhsl.craftattack.spawn.util.IteratorUtil;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Restart extends Appliance {
|
||||
private Countdown countdown;
|
||||
|
||||
public Restart() {
|
||||
this.countdown = new Countdown(60, this::announce, this::onDone);
|
||||
}
|
||||
|
||||
public void scheduleRestart() {
|
||||
try {
|
||||
this.countdown.start();
|
||||
} catch (IllegalStateException e) {
|
||||
throw new ApplianceCommand.Error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void cancelRestart() {
|
||||
try {
|
||||
this.countdown.cancel();
|
||||
} catch (IllegalStateException e) {
|
||||
throw new ApplianceCommand.Error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void announce(int secondsLeft) {
|
||||
IteratorUtil.onlinePlayers(player -> {
|
||||
player.sendMessage(
|
||||
Component.text()
|
||||
.append(Component.text("Serverneustart in ", NamedTextColor.DARK_RED))
|
||||
.append(Component.text(secondsLeft, NamedTextColor.RED))
|
||||
.append(Component.text(" Sekunden!", NamedTextColor.DARK_RED))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private void onDone() {
|
||||
IteratorUtil.onlinePlayers(player -> {
|
||||
player.kick(
|
||||
Component.text()
|
||||
.appendNewline().appendNewline()
|
||||
.append(Component.text("Serverneustart", NamedTextColor.DARK_RED)).appendNewline().appendNewline()
|
||||
.append(Component.text("Wir sind gleich wieder online!", NamedTextColor.GOLD)).appendNewline()
|
||||
.append(Component.text("Verbinde Dich dann erneut.", NamedTextColor.GRAY)).appendNewline()
|
||||
.build()
|
||||
);
|
||||
});
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||
return List.of(new ScheduleRestartCommand(), new CancelRestartCommand());
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.restart.command;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.restart.Restart;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CancelRestartCommand extends ApplianceCommand<Restart> {
|
||||
public CancelRestartCommand() {
|
||||
super("cancelRestart");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
|
||||
getAppliance().cancelRestart();
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.restart.command;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.restart.Restart;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ScheduleRestartCommand extends ApplianceCommand<Restart> {
|
||||
public ScheduleRestartCommand() {
|
||||
super("scheduleRestart");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
|
||||
getAppliance().scheduleRestart();
|
||||
}
|
||||
}
|
52
src/main/java/eu/mhsl/craftattack/spawn/util/Countdown.java
Normal file
52
src/main/java/eu/mhsl/craftattack/spawn/util/Countdown.java
Normal file
@ -0,0 +1,52 @@
|
||||
package eu.mhsl.craftattack.spawn.util;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.Main;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Countdown {
|
||||
private final int countdownFrom;
|
||||
private boolean running;
|
||||
private int taskId;
|
||||
private int current;
|
||||
private final Consumer<Integer> announcement;
|
||||
private final Runnable onDone;
|
||||
|
||||
public Countdown(int countdownFrom, Consumer<Integer> announcement, Runnable onDone) {
|
||||
this.countdownFrom = countdownFrom;
|
||||
this.current = countdownFrom;
|
||||
this.announcement = announcement;
|
||||
this.onDone = onDone;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if(this.running) throw new IllegalStateException("Countdown already running!");
|
||||
this.running = true;
|
||||
this.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.instance(), this::tick, 20, 20);
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
if(!this.running) throw new IllegalStateException("Countdown not running!");
|
||||
this.running = false;
|
||||
Bukkit.getScheduler().cancelTask(this.taskId);
|
||||
this.taskId = 0;
|
||||
this.current = countdownFrom;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return current <= 0;
|
||||
}
|
||||
|
||||
private void tick() {
|
||||
if(current <= 60 && (current <= 10 || current % 10 == 0)) {
|
||||
this.announcement.accept(current);
|
||||
}
|
||||
this.current--;
|
||||
|
||||
if(isDone()) {
|
||||
this.onDone.run();
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}
|
@ -30,3 +30,5 @@ commands:
|
||||
discord:
|
||||
aliases: ["dc"]
|
||||
setPlayerLimit:
|
||||
scheduleRestart:
|
||||
cancelRestart:
|
Loading…
x
Reference in New Issue
Block a user