Updated /near and /getpos command, added new argument playername

Test #1214
This commit is contained in:
snowleo 2011-12-07 10:03:23 +01:00
parent e8eb1974b8
commit 67a3a55f5a
3 changed files with 109 additions and 23 deletions

View file

@ -3,6 +3,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.CommandSender;
public class Commandgetpos extends EssentialsCommand public class Commandgetpos extends EssentialsCommand
@ -11,15 +12,44 @@ public class Commandgetpos extends EssentialsCommand
{ {
super("getpos"); super("getpos");
} }
@Override @Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{ {
final Location coords = user.getLocation(); if (args.length > 0 && user.isAuthorized("essentials.getpos.others"))
user.sendMessage("§7X: " + coords.getBlockX() + " (+East <-> -West)"); {
user.sendMessage("§7Y: " + coords.getBlockY() + " (+Up <-> -Down)"); final User otherUser = getPlayer(server, args, 0);
user.sendMessage("§7Z: " + coords.getBlockZ() + " (+South <-> -North)"); outputPosition(user, otherUser.getLocation(), user.getLocation());
user.sendMessage("§7Yaw: " + (coords.getYaw() + 180 + 360) % 360 + " (Rotation)"); }
user.sendMessage("§7Pitch: " + coords.getPitch() + " (Head angle)"); else
{
outputPosition(user, user.getLocation(), null);
}
}
@Override
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
final User user = getPlayer(server, args, 0);
outputPosition(sender, user.getLocation(), null);
}
//TODO: Translate
private void outputPosition(final CommandSender sender, final Location coords, final Location distance)
{
sender.sendMessage("§7World: " + coords.getWorld().getName());
sender.sendMessage("§7X: " + coords.getBlockX() + " (+East <-> -West)");
sender.sendMessage("§7Y: " + coords.getBlockY() + " (+Up <-> -Down)");
sender.sendMessage("§7Z: " + coords.getBlockZ() + " (+South <-> -North)");
sender.sendMessage("§7Yaw: " + (coords.getYaw() + 180 + 360) % 360 + " (Rotation)");
sender.sendMessage("§7Pitch: " + coords.getPitch() + " (Head angle)");
if (distance != null && coords.getWorld().equals(distance.getWorld()))
{
sender.sendMessage("§7Distance: " + coords.distance(distance));
}
} }
} }

View file

@ -1,11 +1,11 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -15,41 +15,97 @@ public class Commandnear extends EssentialsCommand
{ {
super("near"); super("near");
} }
@Override @Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{ {
long radius = 100; long radius = 100;
User otherUser = null;
if (args.length > 0) if (args.length > 0)
{ {
try try
{ {
radius = Long.parseLong(args[0]); otherUser = getPlayer(server, args, 0);
}
catch (Exception ex)
{
try
{
radius = Long.parseLong(args[0]);
}
catch (NumberFormatException e)
{
}
}
}
if (args.length > 1 && otherUser != null)
{
try
{
radius = Long.parseLong(args[1]);
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
} }
} }
user.sendMessage(_("nearbyPlayers", getLocal(server, user, radius))); if (otherUser == null || user.isAuthorized("essentials.near.others"))
{
user.sendMessage(_("nearbyPlayers", getLocal(server, otherUser == null ? user : otherUser, radius)));
}
else
{
user.sendMessage(_("noAccessCommand"));
}
} }
private String getLocal(final Server server, final User user, long radius) @Override
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
User otherUser = null;
if (args.length > 0)
{
otherUser = getPlayer(server, args, 0);
}
else
{
throw new NotEnoughArgumentsException();
}
long radius = 100;
if (args.length > 1)
{
try
{
radius = Long.parseLong(args[1]);
}
catch (NumberFormatException e)
{
}
}
sender.sendMessage(_("nearbyPlayers", getLocal(server, otherUser, radius)));
}
private String getLocal(final Server server, final User user, final long radius)
{ {
final Location loc = user.getLocation(); final Location loc = user.getLocation();
final World world = loc.getWorld(); final World world = loc.getWorld();
final StringBuilder output = new StringBuilder(); final StringBuilder output = new StringBuilder();
radius *= radius; final long radiusSquared = radius * radius;
for (Player onlinePlayer : server.getOnlinePlayers()) for (Player onlinePlayer : server.getOnlinePlayers())
{ {
final User player = ess.getUser(onlinePlayer); final User player = ess.getUser(onlinePlayer);
if (!player.equals(user) && !player.isHidden()) if (!player.equals(user) && !player.isHidden())
{ {
final Location playerLoc = player.getLocation(); final Location playerLoc = player.getLocation();
if (playerLoc.getWorld() != world) { continue; } if (playerLoc.getWorld() != world)
{
continue;
}
final long delta = (long)playerLoc.distanceSquared(loc); final long delta = (long)playerLoc.distanceSquared(loc);
if (delta < radius) if (delta < radiusSquared)
{ {
if (output.length() > 0) if (output.length() > 0)
{ {

View file

@ -115,8 +115,8 @@ commands:
usage: /<command> [player] usage: /<command> [player]
aliases: [gm,creative,creativemode,egamemode,ecreative,ecreativemode,egm] aliases: [gm,creative,creativemode,egamemode,ecreative,ecreativemode,egm]
getpos: getpos:
description: Get your current coordinates. description: Get your current coordinates or those of a player.
usage: /<command> usage: /<command> [player]
aliases: [coords,egetpos,whereami,ewhereami] aliases: [coords,egetpos,whereami,ewhereami]
gc: gc:
description: Reports garbage collection info; useful to developers. description: Reports garbage collection info; useful to developers.
@ -219,8 +219,8 @@ commands:
usage: /<command> <player> [datediff] usage: /<command> <player> [datediff]
aliases: [emute] aliases: [emute]
near: near:
description: Lists the players near by. description: Lists the players near by or around a player
usage: /<command> [radius] usage: /<command> [playername] [radius]
aliases: [nearby,enear,enearby] aliases: [nearby,enear,enearby]
nick: nick:
description: Change your nickname or that of another player. description: Change your nickname or that of another player.