From b3313b399b451ce1ed5d4b467be7f0197de375be Mon Sep 17 00:00:00 2001 From: mathiascode Date: Sun, 13 Dec 2020 18:39:52 +0200 Subject: [PATCH] Allow controller to chat as the target Closes #2 --- pom.xml | 4 +-- .../pw/kaboom/icontrolu/ControlPlayer.java | 29 ++++++++++++------- .../kaboom/icontrolu/commands/CommandIcu.java | 2 ++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index a973742..2e8a34c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,8 +5,8 @@ master - 1.5 - 1.5 + 1.6 + 1.6 true UTF-8 diff --git a/src/main/java/pw/kaboom/icontrolu/ControlPlayer.java b/src/main/java/pw/kaboom/icontrolu/ControlPlayer.java index e17a136..ed2d0a7 100644 --- a/src/main/java/pw/kaboom/icontrolu/ControlPlayer.java +++ b/src/main/java/pw/kaboom/icontrolu/ControlPlayer.java @@ -104,16 +104,23 @@ class ControlPlayer implements Listener { final Player player = event.getPlayer(); if (PlayerList.getController(player.getUniqueId()) != null) { - event.setCancelled(true); - } + if (event.getMessage().startsWith("§iControlUChat§")) { + final int prefixLength = "§iControlUChat§".length(); - if (PlayerList.getTarget(player.getUniqueId()) != null) { + event.setMessage( + event.getMessage().substring(prefixLength) + ); + } else { + event.setCancelled(true); + } + + } else if (PlayerList.getTarget(player.getUniqueId()) != null) { final Player target = PlayerList.getTarget(player.getUniqueId()); new BukkitRunnable() { @Override public void run() { - target.chat(event.getMessage()); + target.chat("§iControlUChat§" + event.getMessage()); // Add prefix to prevent messages from being cancelled } }.runTask(JavaPlugin.getPlugin(Main.class)); @@ -163,9 +170,8 @@ class ControlPlayer implements Listener { if (PlayerList.getController(player.getUniqueId()) != null) { event.setCancelled(true); - } - if ((event.getAction() == Action.LEFT_CLICK_AIR + } else if ((event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) && PlayerList.getTarget(player.getUniqueId()) != null) { final Player target = PlayerList.getTarget(player.getUniqueId()); @@ -192,9 +198,11 @@ class ControlPlayer implements Listener { final Player player = event.getPlayer(); for (Player otherPlayer: Bukkit.getOnlinePlayers()) { - /* Target disconnects */ if (PlayerList.getController(player.getUniqueId()) != null && PlayerList.getController(player.getUniqueId()).equals(otherPlayer)) { + /* + Target disconnects + */ PlayerList.removeTarget(otherPlayer.getUniqueId()); PlayerList.removeController(player.getUniqueId()); @@ -222,11 +230,12 @@ class ControlPlayer implements Listener { }.runTaskLater(JavaPlugin.getPlugin(Main.class), tickDelay); otherPlayer.sendMessage("The player you were controlling has disconnected. You are invisible for 10 seconds."); - } - /* Controller disconnects */ - if (PlayerList.getTarget(player.getUniqueId()) != null + } else if (PlayerList.getTarget(player.getUniqueId()) != null && PlayerList.getTarget(player.getUniqueId()).equals(otherPlayer)) { + /* + Controller disconnects + */ PlayerList.removeTarget(player.getUniqueId()); PlayerList.removeController(otherPlayer.getUniqueId()); } diff --git a/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java b/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java index c2373d9..d8a9a1d 100644 --- a/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java +++ b/src/main/java/pw/kaboom/icontrolu/commands/CommandIcu.java @@ -24,9 +24,11 @@ public final class CommandIcu implements CommandExecutor { controller.sendMessage(ChatColor.RED + "Usage: /" + label + " control "); } else { Player target = Bukkit.getPlayer(args[1]); + if (target == null && args[1].matches("([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})")) { target = Bukkit.getPlayer(UUID.fromString(args[1])); } + if (target != null) { if (target == controller) { controller.sendMessage("You are already controlling yourself");