summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandnear.java
blob: 846534baf9b3da08fae0c4546846e84fa24bfd6e (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
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");
	}

	@Override
	protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		long maxRadius = ess.getSettings().getChatRadius();
		
		if (maxRadius == 0)
		{
			maxRadius = 200;
		}
		
		long radius = maxRadius;
		
		User otherUser = null;

		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)
			{
				try
				{
					radius = Long.parseLong(args[1]);
				}
				catch (NumberFormatException e)
				{
				}
			}
		}
		
		radius = Math.abs(radius);
		
		if (radius > maxRadius && !user.isAuthorized("essentials.near.maxexempt"))
		{
			user.sendMessage(tl("radiusTooBig", maxRadius));
			radius = maxRadius;
		}
		
		if (otherUser == null || !user.isAuthorized("essentials.near.others"))
		{
			otherUser = user;
		}
		user.sendMessage(tl("nearbyPlayers", getLocal(server, otherUser, radius)));
	}

	@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)
	{
		final Location loc = user.getLocation();
		final World world = loc.getWorld();
		final StringBuilder output = new StringBuilder();
		final long radiusSquared = radius * radius;
		boolean showHidden = user.isAuthorized("essentials.vanish.interact");

		for (Player onlinePlayer : server.getOnlinePlayers())
		{
			final User player = ess.getUser(onlinePlayer);
			if (!player.equals(user) && (!player.isHidden() || showHidden))
			{
				final Location playerLoc = player.getLocation();
				if (playerLoc.getWorld() != world)
				{
					continue;
				}

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