2011-12-01 14:43:16 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2011-12-01 14:43:16 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2017-06-11 00:17:43 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2011-12-01 14:43:16 +00:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.World;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2017-06-11 00:17:43 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2018-09-03 06:09:22 +00:00
|
|
|
import java.util.PriorityQueue;
|
|
|
|
import java.util.Queue;
|
2017-06-11 00:17:43 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2015-05-17 00:36:57 +00:00
|
|
|
long maxRadius = ess.getSettings().getNearRadius();
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
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.canInteractVanished();
|
|
|
|
|
2018-09-04 23:53:01 +00:00
|
|
|
Queue<User> nearbyPlayers = new PriorityQueue<>((o1, o2) -> (int) (o1.getLocation().distanceSquared(loc) - o2.getLocation().distanceSquared(loc)));
|
2018-09-03 06:09:22 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
for (User player : ess.getOnlineUsers()) {
|
2020-03-29 20:44:33 +00:00
|
|
|
if (!player.equals(user) && !player.isAuthorized("essentials.near.exclude") && (!player.isHidden(user.getBase()) || showHidden || user.getBase().canSee(player.getBase()))) {
|
2015-04-15 04:06:16 +00:00
|
|
|
final Location playerLoc = player.getLocation();
|
|
|
|
if (playerLoc.getWorld() != world) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final long delta = (long) playerLoc.distanceSquared(loc);
|
|
|
|
if (delta < radiusSquared) {
|
2018-09-04 23:53:01 +00:00
|
|
|
nearbyPlayers.offer(player);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 06:09:22 +00:00
|
|
|
|
2018-09-04 23:53:01 +00:00
|
|
|
while (!nearbyPlayers.isEmpty()) {
|
2018-09-03 06:09:22 +00:00
|
|
|
if (output.length() > 0) {
|
|
|
|
output.append(", ");
|
|
|
|
}
|
2018-09-04 23:53:01 +00:00
|
|
|
User nearbyPlayer = nearbyPlayers.poll();
|
2019-02-27 03:54:19 +00:00
|
|
|
output.append(nearbyPlayer.getDisplayName()).append("§f(§4").append((long) nearbyPlayer.getLocation().distance(loc)).append("m§f)");
|
2018-09-03 06:09:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
return output.length() > 1 ? output.toString() : tl("none");
|
|
|
|
}
|
2017-06-11 00:17:43 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) {
|
|
|
|
if (user.isAuthorized("essentials.near.others")) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
return getPlayers(server, user);
|
|
|
|
} else if (args.length == 2) {
|
|
|
|
return Lists.newArrayList(Integer.toString(ess.getSettings().getNearRadius()));
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (args.length == 1) {
|
|
|
|
return Lists.newArrayList(Integer.toString(ess.getSettings().getNearRadius()));
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
return getPlayers(server, sender);
|
|
|
|
} else if (args.length == 2) {
|
|
|
|
return Lists.newArrayList(Integer.toString(ess.getSettings().getNearRadius()));
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2011-12-01 14:43:16 +00:00
|
|
|
}
|