From 67a3a55f5a396c399697aa42053d378bc5e65d05 Mon Sep 17 00:00:00 2001 From: snowleo Date: Wed, 7 Dec 2011 10:03:23 +0100 Subject: [PATCH] Updated /near and /getpos command, added new argument playername Test #1214 --- .../essentials/commands/Commandgetpos.java | 44 ++++++++-- .../essentials/commands/Commandnear.java | 80 ++++++++++++++++--- Essentials/src/plugin.yml | 8 +- 3 files changed, 109 insertions(+), 23 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgetpos.java b/Essentials/src/com/earth2me/essentials/commands/Commandgetpos.java index b79df021c..c61702e59 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgetpos.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgetpos.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.User; import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.command.CommandSender; public class Commandgetpos extends EssentialsCommand @@ -11,15 +12,44 @@ public class Commandgetpos extends EssentialsCommand { super("getpos"); } - + @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - final Location coords = user.getLocation(); - user.sendMessage("§7X: " + coords.getBlockX() + " (+East <-> -West)"); - user.sendMessage("§7Y: " + coords.getBlockY() + " (+Up <-> -Down)"); - user.sendMessage("§7Z: " + coords.getBlockZ() + " (+South <-> -North)"); - user.sendMessage("§7Yaw: " + (coords.getYaw() + 180 + 360) % 360 + " (Rotation)"); - user.sendMessage("§7Pitch: " + coords.getPitch() + " (Head angle)"); + if (args.length > 0 && user.isAuthorized("essentials.getpos.others")) + { + final User otherUser = getPlayer(server, args, 0); + outputPosition(user, otherUser.getLocation(), user.getLocation()); + } + 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)); + } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandnear.java b/Essentials/src/com/earth2me/essentials/commands/Commandnear.java index 2730256ef..18fb798af 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandnear.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandnear.java @@ -1,11 +1,11 @@ package com.earth2me.essentials.commands; - import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.World; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -15,41 +15,97 @@ public class Commandnear extends EssentialsCommand { super("near"); } - + @Override protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { long radius = 100; + User otherUser = null; + if (args.length > 0) { 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) { } } - 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 World world = loc.getWorld(); + final World world = loc.getWorld(); final StringBuilder output = new StringBuilder(); - radius *= radius; - + final long radiusSquared = radius * radius; + for (Player onlinePlayer : server.getOnlinePlayers()) { final User player = ess.getUser(onlinePlayer); if (!player.equals(user) && !player.isHidden()) { final Location playerLoc = player.getLocation(); - if (playerLoc.getWorld() != world) { continue; } + if (playerLoc.getWorld() != world) + { + continue; + } - final long delta = (long)playerLoc.distanceSquared(loc); - if (delta < radius) + final long delta = (long)playerLoc.distanceSquared(loc); + if (delta < radiusSquared) { if (output.length() > 0) { diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 64e5fb2cb..5fa22dd8f 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -115,8 +115,8 @@ commands: usage: / [player] aliases: [gm,creative,creativemode,egamemode,ecreative,ecreativemode,egm] getpos: - description: Get your current coordinates. - usage: / + description: Get your current coordinates or those of a player. + usage: / [player] aliases: [coords,egetpos,whereami,ewhereami] gc: description: Reports garbage collection info; useful to developers. @@ -219,8 +219,8 @@ commands: usage: / [datediff] aliases: [emute] near: - description: Lists the players near by. - usage: / [radius] + description: Lists the players near by or around a player + usage: / [playername] [radius] aliases: [nearby,enear,enearby] nick: description: Change your nickname or that of another player.