From 7670eec13ef178a5ee68ab96be51393bec30b3e8 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 19 Sep 2019 22:25:59 -0500 Subject: [PATCH] Add CheckupCommand Signed-off-by: Etzelia --- src/main/java/xyz/etztech/qol/QoL.java | 2 + .../etztech/qol/commands/CheckupCommand.java | 138 ++++++++++++++++++ .../etztech/qol/listeners/JoinListener.java | 7 +- 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 src/main/java/xyz/etztech/qol/commands/CheckupCommand.java diff --git a/src/main/java/xyz/etztech/qol/QoL.java b/src/main/java/xyz/etztech/qol/QoL.java index 429e41b..a6eda57 100644 --- a/src/main/java/xyz/etztech/qol/QoL.java +++ b/src/main/java/xyz/etztech/qol/QoL.java @@ -105,6 +105,8 @@ public class QoL extends JavaPlugin { this.getCommand("deathmute").setExecutor(deathMuteCommand); KaratTrophyCommand karatTrophyCommand = new KaratTrophyCommand(this); this.getCommand("karattrophy").setExecutor(karatTrophyCommand); + CheckupCommand checkupCommand = new CheckupCommand(this); + this.getCommand("checkup").setExecutor(checkupCommand); if (getConfig().getStringList("list").size() > 0) { ListCommand listCommand = new ListCommand(this); diff --git a/src/main/java/xyz/etztech/qol/commands/CheckupCommand.java b/src/main/java/xyz/etztech/qol/commands/CheckupCommand.java new file mode 100644 index 0000000..7f035b8 --- /dev/null +++ b/src/main/java/xyz/etztech/qol/commands/CheckupCommand.java @@ -0,0 +1,138 @@ +package xyz.etztech.qol.commands; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import xyz.etztech.qol.EtzTechUtil; +import xyz.etztech.qol.Lang; +import xyz.etztech.qol.QoL; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +public class CheckupCommand implements CommandExecutor { + + + QoL plugin; + private static HashMap checkups = new HashMap<>(); + + public CheckupCommand(QoL paramQoL) + { + this.plugin = paramQoL; + } + + public static void join(Player player) { + Checkup checkup; + for (UUID uuid : checkups.keySet()) { + checkup = checkups.get(uuid); + for (Player pl : checkup.players) { + if (pl.getUniqueId().equals(player.getUniqueId())) { + return; // Don't check twice, even if they re-logged + } + } + checkup.players.add(player); + checkups.put(uuid, checkup); + } + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { + if (!( commandSender instanceof Player)) { + EtzTechUtil.sms(commandSender, Lang.NO_CONSOLE.getDef()); + return true; + } + if (!commandSender.hasPermission("qol.checkup")) { + EtzTechUtil.sms(commandSender, Lang.NO_PERMISSION.getDef()); + return true; + } + if (args.length < 1) { + EtzTechUtil.sms(commandSender, ChatColor.RED + "Please specify a sub-command. `/checkup `"); + return true; + } + + Player player = (Player) commandSender; + switch (args[0].toLowerCase()) { + case "start": + start(player); + break; + case "stop": + stop(player); + break; + case "next": + next(player); + break; + default: + EtzTechUtil.sms(commandSender, ChatColor.RED + "Invalid sub-command. `/checkup `"); + } + + return true; + } + + private void start(Player player) { + if (checkups.containsKey(player.getUniqueId())) { + EtzTechUtil.sms(player, ChatColor.RED + "You are already performing a checkup. Stop with `/checkup stop` or continue with `/checkup next`"); + return; + } + + checkups.put(player.getUniqueId(), new Checkup(new ArrayList<>(Bukkit.getOnlinePlayers()), player.getLocation())); + + } + + private void stop(Player player) { + if (!checkups.containsKey(player.getUniqueId())) { + EtzTechUtil.sms(player, ChatColor.RED + "You aren't currently performing a checkup. Start one with `/checkup start`"); + return; + } + + Checkup checkup = checkups.get(player.getUniqueId()); + checkups.remove(player.getUniqueId()); + player.teleport(checkup.back); + } + + private void next(Player player) { + if (!checkups.containsKey(player.getUniqueId())) { + EtzTechUtil.sms(player, ChatColor.RED + "You aren't currently performing a checkup. Start one with `/checkup start`"); + return; + } + + Checkup checkup = checkups.get(player.getUniqueId()); + Player next; + while (checkup.hasNext()) { + // Loop until current player is online + next = checkup.next(); + if (next.isOnline()) { + player.teleport(next.getLocation()); + checkups.put(player.getUniqueId(), checkup); + return; + } + } + + EtzTechUtil.sms(player, ChatColor.GREEN + "This is the last player in the checkup. Once you are finished, use `/checkup stop`"); + } + + private class Checkup { + Integer index; + List players; + Location back; + + private Checkup(List players, Location back) { + this.index = 0; + this.players = players; + this.back = back; + } + + private boolean hasNext() { + return this.index < this.players.size(); + } + + private Player next() { + return this.players.get(this.index++); + } + } +} diff --git a/src/main/java/xyz/etztech/qol/listeners/JoinListener.java b/src/main/java/xyz/etztech/qol/listeners/JoinListener.java index 2caa482..1af0ade 100644 --- a/src/main/java/xyz/etztech/qol/listeners/JoinListener.java +++ b/src/main/java/xyz/etztech/qol/listeners/JoinListener.java @@ -9,11 +9,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerLoginEvent; import xyz.etztech.qol.QoL; +import xyz.etztech.qol.commands.CheckupCommand; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class JoinListener implements Listener { @@ -60,6 +58,7 @@ public class JoinListener implements Listener { plugin.updatePlayerViewDistance(player); + CheckupCommand.join(player); } }