TF-EssentialsX/Essentials/src/main/java/com/earth2me/essentials/commands/Commandnear.java

148 lines
5.3 KiB
Java
Raw Normal View History

2011-12-01 14:43:16 +00:00
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
2011-12-01 14:43:16 +00:00
import com.earth2me.essentials.User;
import com.google.common.collect.Lists;
import org.bukkit.ChatColor;
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
import java.util.Collections;
import java.util.List;
2018-09-03 06:09:22 +00:00
import java.util.PriorityQueue;
import java.util.Queue;
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 {
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]);
2020-10-03 17:46:05 +00:00
} catch (final NumberFormatException e) {
2015-04-15 04:06:16 +00:00
try {
otherUser = getPlayer(server, user, args, 0);
2020-10-03 17:46:05 +00:00
} catch (final Exception ignored) {
2015-04-15 04:06:16 +00:00
}
}
if (args.length > 1 && otherUser != null) {
try {
radius = Long.parseLong(args[1]);
2020-10-03 17:46:05 +00:00
} catch (final NumberFormatException ignored) {
2015-04-15 04:06:16 +00:00
}
}
}
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(otherUser, radius)));
2015-04-15 04:06:16 +00:00
}
@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]);
2020-10-03 17:46:05 +00:00
} catch (final NumberFormatException ignored) {
2015-04-15 04:06:16 +00:00
}
}
sender.sendMessage(tl("nearbyPlayers", getLocal(otherUser, radius)));
2015-04-15 04:06:16 +00:00
}
private String getLocal(final User user, final long radius) {
2015-04-15 04:06:16 +00:00
final Location loc = user.getLocation();
final World world = loc.getWorld();
final StringBuilder output = new StringBuilder();
final long radiusSquared = radius * radius;
2020-10-03 17:46:05 +00:00
final boolean showHidden = user.canInteractVanished();
2015-04-15 04:06:16 +00:00
2020-10-03 17:46:05 +00:00
final Queue<User> nearbyPlayers = new PriorityQueue<>((o1, o2) -> (int) (o1.getLocation().distanceSquared(loc) - o2.getLocation().distanceSquared(loc)));
2018-09-03 06:09:22 +00:00
2020-10-03 17:46:05 +00:00
for (final 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(", ");
}
2020-10-03 17:46:05 +00:00
final User nearbyPlayer = nearbyPlayers.poll();
if (nearbyPlayer == null) {
continue;
}
output.append(nearbyPlayer.getDisplayName()).append(ChatColor.WHITE + "(" + ChatColor.RED).append((long) nearbyPlayer.getLocation().distance(loc)).append("m" + ChatColor.WHITE + ")");
2018-09-03 06:09:22 +00:00
}
2015-04-15 04:06:16 +00:00
return output.length() > 1 ? output.toString() : tl("none");
}
@Override
2020-10-03 17:46:05 +00:00
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final 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
2020-10-03 17:46:05 +00:00
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final 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
}