From 7c3971aa59fdf204aee8974ddca036919c992f00 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 19 Dec 2016 18:24:34 +0100 Subject: [PATCH] Added custom restart cmd with an event and a bar --- .../core/ScheduledRestartCommand.java | 71 +++++++++++++++++++ .../lib/ScheduledServerRestartEvent.java | 27 +++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/buttondevteam/core/ScheduledRestartCommand.java create mode 100644 src/main/java/buttondevteam/lib/ScheduledServerRestartEvent.java diff --git a/src/main/java/buttondevteam/core/ScheduledRestartCommand.java b/src/main/java/buttondevteam/core/ScheduledRestartCommand.java new file mode 100644 index 0000000..3c2a237 --- /dev/null +++ b/src/main/java/buttondevteam/core/ScheduledRestartCommand.java @@ -0,0 +1,71 @@ +package buttondevteam.core; + +import org.bukkit.Bukkit; +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BarFlag; +import org.bukkit.boss.BarStyle; +import org.bukkit.boss.BossBar; +import org.bukkit.command.CommandSender; +import org.bukkit.scheduler.BukkitTask; + +import buttondevteam.lib.ScheduledServerRestartEvent; +import buttondevteam.lib.chat.TBMCCommandBase; + +public class ScheduledRestartCommand extends TBMCCommandBase { + private static volatile int restartcounter; + private static volatile BukkitTask restarttask; + private static volatile BossBar restartbar; + + @Override + public boolean OnCommand(CommandSender sender, String alias, String[] args) { + int ticks = 20 * 60; + try { + if (args.length > 0) + ticks = Integer.parseInt(args[0]); + } catch (NumberFormatException e) { + } + if (ticks < 20) { + sender.sendMessage("§cError: Ticks must be more than 20."); + return false; + } + final int restarttime = restartcounter = ticks; + restartbar = Bukkit.createBossBar("Server restart in " + ticks / 20f, BarColor.RED, BarStyle.SOLID, + BarFlag.DARKEN_SKY); + restartbar.setProgress(1); + Bukkit.getOnlinePlayers().stream().forEach(p -> restartbar.addPlayer(p)); + sender.sendMessage("Scheduled restart in " + ticks / 20f); + ScheduledServerRestartEvent e = new ScheduledServerRestartEvent(ticks); + Bukkit.getPluginManager().callEvent(e); + restarttask = Bukkit.getScheduler().runTaskTimerAsynchronously(MainPlugin.Instance, () -> { + if (restartcounter < 0) { + restarttask.cancel(); + restartbar.getPlayers().stream().forEach(p -> restartbar.removePlayer(p)); + Bukkit.spigot().restart(); + } + if (restartcounter % 200 == 0) + Bukkit.broadcastMessage("§c-- The server is restarting in " + restartcounter); + restartbar.setProgress(restartcounter / restarttime); + restartcounter--; + }, 1, 1); + return true; + } + + @Override + public String[] GetHelpText(String alias) { + return new String[] { // + "§6---- Scheduled restart ----", // + "This command restarts the server 1 minute after it's executed, warning players every 10 seconds.", // + "You can optionally set the amount of ticks to wait before the restart." // + }; + } + + @Override + public boolean GetPlayerOnly() { + return false; + } + + @Override + public boolean GetModOnly() { + return true; + } +} diff --git a/src/main/java/buttondevteam/lib/ScheduledServerRestartEvent.java b/src/main/java/buttondevteam/lib/ScheduledServerRestartEvent.java new file mode 100644 index 0000000..c73ca4e --- /dev/null +++ b/src/main/java/buttondevteam/lib/ScheduledServerRestartEvent.java @@ -0,0 +1,27 @@ +package buttondevteam.lib; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ScheduledServerRestartEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private int restartticks; + + public ScheduledServerRestartEvent(int restartticks) { + this.restartticks = restartticks; + } + + public int getRestartTicks() { + return restartticks; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} \ No newline at end of file