TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stop.java

75 lines
2.2 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2011-10-19 00:37:00 +00:00
import java.util.HashMap;
import java.util.Map;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.StringUtils;
2011-10-19 00:37:00 +00:00
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
2011-10-19 00:37:00 +00:00
2020-08-15 22:42:46 +00:00
@CommandPermissions(level = Rank.TRIAL_MOD, source = SourceType.BOTH)
2018-02-22 01:22:13 +00:00
@CommandParameters(description = "Kicks everyone and stops the server.", usage = "/<command> [reason]")
public class Command_stop extends FreedomCommand
2011-10-19 00:37:00 +00:00
{
private static final Map<CommandSender, String> STOP_CONFIRM = new HashMap<>();
2011-10-19 00:37:00 +00:00
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
2011-10-19 00:37:00 +00:00
{
2018-02-22 01:22:13 +00:00
String reason = "Server is going offline, come back in about 20 seconds.";
2020-06-30 07:25:38 +00:00
if (args.length != 0)
2018-02-22 01:22:13 +00:00
{
2018-02-23 06:49:12 +00:00
reason = StringUtils.join(args, " ");
2018-02-22 01:22:13 +00:00
}
2020-06-30 07:25:38 +00:00
if (sender.getName().equals("CONSOLE"))
{
shutdown(reason);
return true;
}
else if (STOP_CONFIRM.containsKey(sender))
{
shutdown(STOP_CONFIRM.get(sender));
return true;
}
msg("Warning: You're about to stop the server. Type /stop again to confirm you want to do this.");
STOP_CONFIRM.put(sender, reason);
new BukkitRunnable()
{
@Override
public void run()
{
if (STOP_CONFIRM.containsKey(sender))
{
STOP_CONFIRM.remove(sender);
msg("Stop request expired.");
}
}
}.runTaskLater(plugin, 15 * 20);
2011-10-19 00:37:00 +00:00
return true;
}
2020-06-30 07:25:38 +00:00
public void shutdown(String reason)
{
FUtil.bcastMsg("Server is going offline!", ChatColor.LIGHT_PURPLE);
for (Player player : server.getOnlinePlayers())
{
player.kickPlayer(ChatColor.LIGHT_PURPLE + reason);
}
STOP_CONFIRM.remove(sender);
server.shutdown();
}
2011-10-19 00:37:00 +00:00
}