TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandnear.java

131 lines
2.9 KiB
Java
Raw Normal View History

2011-12-01 14:43:16 +00:00
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
2011-12-01 14:43:16 +00:00
import com.earth2me.essentials.User;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
public class Commandnear extends EssentialsCommand
{
public Commandnear()
{
super("near");
}
2011-12-01 14:43:16 +00:00
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
2013-07-15 22:15:35 +00:00
long maxRadius = ess.getSettings().getChatRadius();
if (maxRadius == 0)
2013-07-15 09:20:04 +00:00
{
2013-07-15 22:15:35 +00:00
maxRadius = 200;
2013-07-15 09:20:04 +00:00
}
2013-07-15 22:15:35 +00:00
long radius = maxRadius;
User otherUser = null;
2011-12-01 14:43:16 +00:00
if (args.length > 0)
{
try
{
radius = Long.parseLong(args[0]);
}
catch (NumberFormatException e)
{
try
{
otherUser = getPlayer(server, user, args, 0);
}
catch (Exception ex)
{
}
}
if (args.length > 1 && otherUser != null)
2011-12-01 14:43:16 +00:00
{
try
{
radius = Long.parseLong(args[1]);
}
catch (NumberFormatException e)
{
}
2011-12-01 14:43:16 +00:00
}
}
2013-07-15 09:20:04 +00:00
2014-02-16 01:15:01 +00:00
radius = Math.abs(radius);
2013-07-15 22:15:35 +00:00
if (radius > maxRadius && !user.isAuthorized("essentials.near.maxexempt"))
2013-07-15 09:20:04 +00:00
{
user.sendMessage(tl("radiusTooBig", maxRadius));
2013-07-20 23:04:04 +00:00
radius = maxRadius;
2013-07-15 09:20:04 +00:00
}
if (otherUser == null || !user.isAuthorized("essentials.near.others"))
{
otherUser = user;
}
user.sendMessage(tl("nearbyPlayers", getLocal(server, otherUser, radius)));
2011-12-01 14:43:16 +00:00
}
@Override
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
{
if (args.length == 0)
{
throw new NotEnoughArgumentsException();
}
final User otherUser = getPlayer(server, args, 0, true, false);
long radius = 200;
if (args.length > 1)
{
try
{
radius = Long.parseLong(args[1]);
}
catch (NumberFormatException e)
{
}
}
sender.sendMessage(tl("nearbyPlayers", getLocal(server, otherUser, radius)));
}
private String getLocal(final Server server, final User user, final long radius)
2011-12-01 14:43:16 +00:00
{
final Location loc = user.getLocation();
final World world = loc.getWorld();
2011-12-01 14:43:16 +00:00
final StringBuilder output = new StringBuilder();
final long radiusSquared = radius * radius;
boolean showHidden = user.isAuthorized("essentials.vanish.interact");
2011-12-01 14:43:16 +00:00
for (Player onlinePlayer : server.getOnlinePlayers())
{
final User player = ess.getUser(onlinePlayer);
if (!player.equals(user) && (!player.isHidden() || showHidden))
2011-12-01 14:43:16 +00:00
{
2011-12-01 15:15:45 +00:00
final Location playerLoc = player.getLocation();
if (playerLoc.getWorld() != world)
{
continue;
}
final long delta = (long)playerLoc.distanceSquared(loc);
if (delta < radiusSquared)
2011-12-01 14:43:16 +00:00
{
if (output.length() > 0)
{
output.append(", ");
}
output.append(player.getDisplayName()).append("§f(§4").append((long)Math.sqrt(delta)).append("m§f)");
2011-12-01 14:43:16 +00:00
}
}
}
return output.length() > 1 ? output.toString() : tl("none");
2011-12-01 14:43:16 +00:00
}
}