From f3abebe8882128e868ebd70693ee98783bb0abde Mon Sep 17 00:00:00 2001 From: mathiascode Date: Tue, 17 Dec 2019 20:51:46 +0200 Subject: [PATCH] Split command into smaller methods --- .../kaboom/icontrolu/commands/CommandIcu.java | 143 ++++++++++-------- 1 file changed, 77 insertions(+), 66 deletions(-) diff --git a/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java b/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java index 8c4703f..5432955 100644 --- a/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java +++ b/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java @@ -17,78 +17,89 @@ import pw.kaboom.icontrolu.Main; import pw.kaboom.icontrolu.utilities.PlayerList; public final class CommandIcu implements CommandExecutor { + private void controlCommand(final Player controller, final String label, final String[] args) { + if (args.length == 1) { + controller.sendMessage(ChatColor.RED + "Usage: /" + label + " control "); + } else { + final Player target = Bukkit.getPlayer(args[1]); + + if (target != null) { + if (target == controller) { + controller.sendMessage("You are already controlling yourself"); + } else if (PlayerList.getTarget(controller.getUniqueId()) != null) { + controller.sendMessage("You are already controlling \"" + target.getName() + "\""); + } else if (PlayerList.getController(target.getUniqueId()) != null) { + controller.sendMessage("Player \"" + target.getName() + "\" is already being controlled"); + } else if (PlayerList.getTarget(target.getUniqueId()) != null) { + controller.sendMessage("Player \"" + target.getName() + "\" is already controlling another player"); + } else if (!controller.canSee(target)) { + controller.sendMessage("You may not control this player"); + } else { + controller.teleportAsync(target.getLocation()); + + controller.getInventory().setContents(target.getInventory().getContents()); + + PlayerList.setTarget(controller.getUniqueId(), target); + PlayerList.setController(target.getUniqueId(), controller); + + controller.sendMessage("You are now controlling \"" + target.getName() + "\""); + } + } else { + controller.sendMessage("Player \"" + args[1] + "\" not found"); + } + } + } + + private void stopCommand(final Player controller) { + final Player target = PlayerList.getTarget(controller.getUniqueId()); + + if (target != null) { + PlayerList.removeTarget(controller.getUniqueId()); + PlayerList.removeController(target.getUniqueId()); + + final int tickDelay = 200; + + new BukkitRunnable() { + @Override + public void run() { + for (Player player: Bukkit.getOnlinePlayers()) { + player.showPlayer(JavaPlugin.getPlugin(Main.class), controller); + } + + Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); + Team team = scoreboard.getTeam("icuDisableCollision"); + if (team != null && team.hasEntry(controller.getName())) { + team.removeEntry(controller.getName()); + } + + controller.removePotionEffect(PotionEffectType.INVISIBILITY); + controller.sendMessage("You are now visible"); + } + }.runTaskLater(JavaPlugin.getPlugin(Main.class), tickDelay); + + final int seconds = tickDelay / 20; + + controller.sendMessage("You are no longer controlling \"" + target.getName() + "\". You are invisible for " + seconds + " seconds."); + } else { + controller.sendMessage("You are not controlling anyone at the moment"); + } + } + @Override public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { sender.sendMessage("Command has to be run by a player"); - } else { - final Player controller = (Player) sender; + return true; + } - if (args.length == 0) { - controller.sendMessage(ChatColor.RED + "Usage: /" + label + " "); - } else if (args[0].equalsIgnoreCase("control")) { - if (args.length == 1) { - controller.sendMessage(ChatColor.RED + "Usage: /" + label + " control "); - } else { - final Player target = Bukkit.getPlayer(args[1]); + final Player controller = (Player) sender; - if (target != null) { - if (target == controller) { - controller.sendMessage("You are already controlling yourself"); - } else if (PlayerList.getTarget(controller.getUniqueId()) != null) { - controller.sendMessage("You are already controlling \"" + target.getName() + "\""); - } else if (PlayerList.getController(target.getUniqueId()) != null) { - controller.sendMessage("Player \"" + target.getName() + "\" is already being controlled"); - } else if (PlayerList.getTarget(target.getUniqueId()) != null) { - controller.sendMessage("Player \"" + target.getName() + "\" is already controlling another player"); - } else if (!controller.canSee(target)) { - controller.sendMessage("You may not control this player"); - } else { - controller.teleportAsync(target.getLocation()); - - controller.getInventory().setContents(target.getInventory().getContents()); - - PlayerList.setTarget(controller.getUniqueId(), target); - PlayerList.setController(target.getUniqueId(), controller); - - controller.sendMessage("You are now controlling \"" + target.getName() + "\""); - } - } else { - controller.sendMessage("Player \"" + args[1] + "\" not found"); - } - } - } else if (args[0].equalsIgnoreCase("stop")) { - final Player target = PlayerList.getTarget(controller.getUniqueId()); - - if (target != null) { - PlayerList.removeTarget(controller.getUniqueId()); - PlayerList.removeController(target.getUniqueId()); - - final int tickDelay = 200; - - new BukkitRunnable() { - @Override - public void run() { - for (Player player: Bukkit.getOnlinePlayers()) { - player.showPlayer(JavaPlugin.getPlugin(Main.class), controller); - } - - Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); - Team team = scoreboard.getTeam("icuDisableCollision"); - if (team != null && team.hasEntry(controller.getName())) { - team.removeEntry(controller.getName()); - } - - controller.removePotionEffect(PotionEffectType.INVISIBILITY); - controller.sendMessage("You are now visible"); - } - }.runTaskLater(JavaPlugin.getPlugin(Main.class), tickDelay); - - controller.sendMessage("You are no longer controlling \"" + target.getName() + "\". You are invisible for 10 seconds."); - } else { - controller.sendMessage("You are not controlling anyone at the moment"); - } - } + if (args.length == 0) { + controller.sendMessage(ChatColor.RED + "Usage: /" + label + " "); + } else if (args[0].equalsIgnoreCase("control")) { + controlCommand(controller, label, args); + } else if (args[0].equalsIgnoreCase("stop")) { + stopCommand(controller); } return true; }