summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandnear.java
blob: 3daf5ce22e00a50e83388705f2f1a2e4cd347994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.ess3.commands;

import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class Commandnear extends EssentialsCommand
{
	@Override
	protected void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		long radius = ess.getSettings().getData().getCommands().getNear().getDefaultRadius();

		IUser otherUser = null;

		if (args.length > 0)
		{
			try
			{
				otherUser = ess.getUserMap().matchUserExcludingHidden(args[0], user.getPlayer());
			}
			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)
				{
				}
			}
		}
		if (otherUser == null || Permissions.NEAR_OTHERS.isAuthorized(user))
		{
			user.sendMessage(_("Players nearby: {0}", getLocal(otherUser == null ? user : otherUser, radius)));
		}
		else
		{
			user.sendMessage(_("You do not have access to that command."));
		}
	}

	@Override
	protected void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length == 0)
		{
			throw new NotEnoughArgumentsException();
		}
		final IUser otherUser = ess.getUserMap().matchUser(args[0], false);
		long radius = ess.getSettings().getData().getCommands().getNear().getDefaultRadius();
		if (args.length > 1)
		{
			try
			{
				radius = Long.parseLong(args[1]);
			}
			catch (NumberFormatException e)
			{
			}
		}
		sender.sendMessage(_("Players nearby: {0}", getLocal(otherUser, radius)));
	}

	private String getLocal(final IUser user, final long radius)
	{
		final Location loc = user.getPlayer().getLocation();
		final World world = loc.getWorld();
		final StringBuilder output = new StringBuilder();
		final long radiusSquared = radius * radius;
		Player userPlayer = user.getPlayer();

		for (Player onlinePlayer : server.getOnlinePlayers())
		{
			if (!onlinePlayer.getName().equals(user.getName()) && userPlayer.canSee(onlinePlayer))
			{
				final Location playerLoc = onlinePlayer.getLocation();
				if (playerLoc.getWorld() != world)
				{
					continue;
				}

				final long delta = (long)playerLoc.distanceSquared(loc);
				if (delta < radiusSquared)
				{
					if (output.length() > 0)
					{
						output.append(", ");
					}
					output.append(onlinePlayer.getDisplayName()).append("§f(§4").append((long)Math.sqrt(delta)).append("m§f)");
				}
			}
		}
		return output.length() > 1 ? output.toString() : _("none");
	}
}