Add /locate|where|find command to find other players in other servers

This commit is contained in:
libraryaddict 2015-02-13 20:57:46 +13:00
parent 876005b3e3
commit 0cd0c61200
7 changed files with 239 additions and 3 deletions

View File

@ -463,7 +463,7 @@ public class MessageManager extends MiniClientPlugin<ClientMessage>
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<ClientMessage>
// 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<ClientMessage>
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();

View File

@ -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<Teleporter> teleportList = new LinkedList<Teleporter>();
private NautHashMap<String, LinkedList<Location>> _tpHistory = new NautHashMap<String, LinkedList<Location>>();
private NautHashMap<UUID, BukkitRunnable> _failedRedisLocates = new NautHashMap<UUID, BukkitRunnable>();
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

View File

@ -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<Teleport>
{
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]);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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))