From 3395bab928f03c1825b4e15372a7115f1090cdc6 Mon Sep 17 00:00:00 2001 From: pop4959 Date: Sun, 2 Sep 2018 23:09:22 -0700 Subject: [PATCH] Sort /near by nearest player. --- .../essentials/commands/Commandnear.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandnear.java b/Essentials/src/com/earth2me/essentials/commands/Commandnear.java index 45a9d4fa2..4081983cb 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandnear.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandnear.java @@ -7,8 +7,12 @@ import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.World; +import javafx.util.Pair; import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.PriorityQueue; +import java.util.Queue; import static com.earth2me.essentials.I18n.tl; @@ -83,6 +87,8 @@ public class Commandnear extends EssentialsCommand { final long radiusSquared = radius * radius; boolean showHidden = user.canInteractVanished(); + Queue> playerDistances = new PriorityQueue<>(Comparator.comparingLong(Pair::getValue)); + for (User player : ess.getOnlineUsers()) { if (!player.equals(user) && (!player.isHidden(user.getBase()) || showHidden || user.getBase().canSee(player.getBase()))) { final Location playerLoc = player.getLocation(); @@ -92,13 +98,19 @@ public class Commandnear extends EssentialsCommand { 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)"); + playerDistances.offer(new Pair<>(player, delta)); } } } + + while (!playerDistances.isEmpty()) { + if (output.length() > 0) { + output.append(", "); + } + Pair playerDistance = playerDistances.poll(); + output.append(playerDistance.getKey().getDisplayName()).append("§f(§4").append((long) Math.sqrt(playerDistance.getValue())).append("m§f)"); + } + return output.length() > 1 ? output.toString() : tl("none"); }