From 0cd0c6120013d144f5927cd2c424479f9de401c8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 13 Feb 2015 20:57:46 +1300 Subject: [PATCH] Add /locate|where|find command to find other players in other servers --- .../mineplex/core/message/MessageManager.java | 8 +- .../src/mineplex/core/teleport/Teleport.java | 78 +++++++++++++++++++ .../core/teleport/command/LocateCommand.java | 29 +++++++ .../core/teleport/redis/RedisLocate.java | 40 ++++++++++ .../teleport/redis/RedisLocateCallback.java | 43 ++++++++++ .../teleport/redis/RedisLocateHandler.java | 41 ++++++++++ .../mineplex/serverdata/ServerCommand.java | 3 + 7 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/teleport/command/LocateCommand.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocate.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateCallback.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateHandler.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java b/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java index 88991803e..52d8bc5d9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java @@ -463,7 +463,7 @@ public class MessageManager extends MiniClientPlugin if (adminMessage || canSenderMessageThem(sender, playerTarget)) { // Construct the command to send to redis - final RedisMessage globalMessage = new RedisMessage(_serverName, + RedisMessage globalMessage = new RedisMessage(_serverName, sender.getName(), @@ -475,13 +475,15 @@ public class MessageManager extends MiniClientPlugin // Include the sender's rank if this is a admin message. So we can format the receivers chat. adminMessage ? F.rank(_clientManager.Get(sender).GetRank()) : null); + + final UUID uuid = globalMessage.getUUID(); // A backup for the rare case where the message fails to deliver. Server doesn't respond BukkitRunnable runnable = new BukkitRunnable() { public void run() { - _messageTimeouts.remove(globalMessage.getUUID()); + _messageTimeouts.remove(uuid); // Inform the player that the message failed to deliver UtilPlayer.message( @@ -495,7 +497,7 @@ public class MessageManager extends MiniClientPlugin runnable.runTaskLater(GetPlugin(), 40); // The key is the UUID its trading between servers - _messageTimeouts.put(globalMessage.getUUID(), runnable); + _messageTimeouts.put(uuid, runnable); // Time to send the message! globalMessage.publish(); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/teleport/Teleport.java b/Plugins/Mineplex.Core/src/mineplex/core/teleport/Teleport.java index d46f11d03..7ef1af74b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/teleport/Teleport.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/teleport/Teleport.java @@ -1,40 +1,118 @@ package mineplex.core.teleport; import java.util.LinkedList; +import java.util.UUID; import mineplex.core.MiniPlugin; +import mineplex.core.teleport.command.LocateCommand; import mineplex.core.teleport.command.TeleportCommand; import mineplex.core.teleport.event.MineplexTeleportEvent; +import mineplex.core.teleport.redis.RedisLocate; +import mineplex.core.teleport.redis.RedisLocateCallback; +import mineplex.core.teleport.redis.RedisLocateHandler; import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.UpdateType; import mineplex.core.account.event.ClientUnloadEvent; +import mineplex.core.common.jsonchat.ChildJsonMessage; +import mineplex.core.common.jsonchat.ClickEvent; +import mineplex.core.common.jsonchat.HoverEvent; +import mineplex.core.common.jsonchat.JsonMessage; +import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilWorld; +import mineplex.serverdata.ServerCommandManager; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; public class Teleport extends MiniPlugin { private LinkedList teleportList = new LinkedList(); private NautHashMap> _tpHistory = new NautHashMap>(); + private NautHashMap _failedRedisLocates = new NautHashMap(); + private String _serverName; public Teleport(JavaPlugin plugin) { super("Teleport", plugin); + + _serverName = GetPlugin().getConfig().getString("serverstatus.name"); + + RedisLocateHandler locateHandler = new RedisLocateHandler(this); + + ServerCommandManager.getInstance().registerCommandType("RedisLocate", RedisLocate.class, locateHandler); + ServerCommandManager.getInstance().registerCommandType("RedisLocateCallback", RedisLocateCallback.class, locateHandler); } @Override public void AddCommands() { addCommand(new TeleportCommand(this)); + addCommand(new LocateCommand(this)); + } + + public void handleLocateCallback(RedisLocateCallback callback) + { + BukkitRunnable runnable = _failedRedisLocates.remove(callback.getUUID()); + + if (runnable != null) + { + runnable.cancel(); + } + + Player player = Bukkit.getPlayerExact(callback.getTarget()); + + if (player != null) + { + ChildJsonMessage message = new JsonMessage("").extra(C.mHead + "Locate" + "> " + C.mBody + "Located [" + C.mElem + + callback.getPlayer() + C.mBody + "] at "); + + message.add(C.cBlue + callback.getServer()).click(ClickEvent.RUN_COMMAND, + "/server " + callback.getServer()); + + message.hover(HoverEvent.SHOW_TEXT, "Teleport to " + callback.getServer()); + + message.sendToPlayer(player); + } + } + + public void locatePlayer(final Player player, final String target) + { + Player targetPlayer = Bukkit.getPlayerExact(target); + + if (targetPlayer != null) + { + UtilPlayer.message(player, F.main("Locate", C.mBody + " [" + C.mElem + target + C.mBody + "] is in the same server!")); + return; + } + + RedisLocate locate = new RedisLocate(_serverName, player.getName(), target); + final UUID uuid = locate.getUUID(); + + BukkitRunnable runnable = new BukkitRunnable() + { + + @Override + public void run() + { + _failedRedisLocates.remove(uuid); + UtilPlayer.message(player, F.main("Locate", C.mBody + "Failed to locate [" + C.mElem + target + C.mBody + "].")); + } + + }; + + _failedRedisLocates.put(uuid, runnable); + runnable.runTaskLater(_plugin, 40); + + locate.publish(); } @EventHandler diff --git a/Plugins/Mineplex.Core/src/mineplex/core/teleport/command/LocateCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/teleport/command/LocateCommand.java new file mode 100644 index 000000000..c2ef276c7 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/teleport/command/LocateCommand.java @@ -0,0 +1,29 @@ +package mineplex.core.teleport.command; + +import org.bukkit.entity.Player; + +import mineplex.core.command.CommandBase; +import mineplex.core.common.Rank; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; +import mineplex.core.teleport.Teleport; + +public class LocateCommand extends CommandBase +{ + public LocateCommand(Teleport plugin) + { + super(plugin, Rank.HELPER, "locate", "where", "find"); + } + + @Override + public void Execute(Player caller, String[] args) + { + if (args == null || args.length == 0) + { + UtilPlayer.message(caller, F.main("Locate", "Player argument missing.")); + return; + } + + Plugin.locatePlayer(caller, args[0]); + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocate.java b/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocate.java new file mode 100644 index 000000000..b2d62a2c2 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocate.java @@ -0,0 +1,40 @@ +package mineplex.core.teleport.redis; + +import java.util.UUID; + +import mineplex.serverdata.ServerCommand; + +public class RedisLocate extends ServerCommand +{ + private String _sender; + private String _sendingServer; + private String _target; + private UUID _uuid = UUID.randomUUID(); + + public RedisLocate(String sendingServer, String sender, String target) + { + _sender = sender; + _target = target; + _sendingServer = sendingServer; + } + + public String getSender() + { + return _sender; + } + + public String getServer() + { + return _sendingServer; + } + + public String getTarget() + { + return _target; + } + + public UUID getUUID() + { + return _uuid; + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateCallback.java b/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateCallback.java new file mode 100644 index 000000000..511c90496 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateCallback.java @@ -0,0 +1,43 @@ +package mineplex.core.teleport.redis; + +import java.util.UUID; + +import mineplex.serverdata.ServerCommand; + +public class RedisLocateCallback extends ServerCommand +{ + private String _player; + private String _server; + private String _target; + private UUID _uuid; + + public RedisLocateCallback(RedisLocate command, String server) + { + _uuid = command.getUUID(); + _target = command.getSender(); + _player = command.getTarget(); + _server = server; + + setTargetServers(command.getServer()); + } + + public String getPlayer() + { + return _player; + } + + public String getServer() + { + return _server; + } + + public String getTarget() + { + return _target; + } + + public UUID getUUID() + { + return _uuid; + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateHandler.java b/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateHandler.java new file mode 100644 index 000000000..daa472f20 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/teleport/redis/RedisLocateHandler.java @@ -0,0 +1,41 @@ +package mineplex.core.teleport.redis; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import mineplex.core.teleport.Teleport; +import mineplex.serverdata.CommandCallback; +import mineplex.serverdata.ServerCommand; + +public class RedisLocateHandler implements CommandCallback +{ + private Teleport _plugin; + private String _serverName; + + public RedisLocateHandler(Teleport plugin) + { + _plugin = plugin; + _serverName = _plugin.GetPlugin().getConfig().getString("serverstatus.name"); + } + + @Override + public void run(ServerCommand command) + { + if (command instanceof RedisLocate) + { + RedisLocate locate = (RedisLocate) command; + + Player target = Bukkit.getPlayerExact(locate.getTarget()); + + if (target != null) + { + RedisLocateCallback callback = new RedisLocateCallback(locate, _serverName); + callback.publish(); + } + } + else if (command instanceof RedisLocateCallback) + { + _plugin.handleLocateCallback((RedisLocateCallback) command); + } + } + +} diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommand.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommand.java index 73f5bfe35..2bfd1b8ee 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommand.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommand.java @@ -45,6 +45,9 @@ public abstract class ServerCommand */ public boolean isTargetServer(String serverName) { + if (getTargetServers().length == 0) + return true; + for (String targetServer : getTargetServers()) { if (targetServer.equalsIgnoreCase(serverName))