From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Thu, 13 May 2021 16:18:29 -0500
Subject: [PATCH] Hide hidden players from entity selector


diff --git a/src/main/java/net/minecraft/commands/arguments/selector/EntitySelector.java b/src/main/java/net/minecraft/commands/arguments/selector/EntitySelector.java
index 0fb8f32427843f4bfd90ab88ecb3ab3e4a4fda31..f99f1ba7f912a2fd503e12c446b342a8d04356a0 100644
--- a/src/main/java/net/minecraft/commands/arguments/selector/EntitySelector.java
+++ b/src/main/java/net/minecraft/commands/arguments/selector/EntitySelector.java
@@ -184,10 +184,10 @@ public class EntitySelector {
 
         if (this.playerName != null) {
             entityplayer = commandlistenerwrapper.getServer().getPlayerList().getPlayerByName(this.playerName);
-            return (List) (entityplayer == null ? Collections.emptyList() : Lists.newArrayList(new ServerPlayer[]{entityplayer}));
+            return entityplayer == null || !canSee(commandlistenerwrapper, entityplayer) ? Collections.emptyList() : Lists.newArrayList(entityplayer); // Purpur
         } else if (this.entityUUID != null) {
             entityplayer = commandlistenerwrapper.getServer().getPlayerList().getPlayer(this.entityUUID);
-            return (List) (entityplayer == null ? Collections.emptyList() : Lists.newArrayList(new ServerPlayer[]{entityplayer}));
+            return entityplayer == null || !canSee(commandlistenerwrapper, entityplayer) ? Collections.emptyList() : Lists.newArrayList(entityplayer); // Purpur
         } else {
             Vec3 vec3d = (Vec3) this.position.apply(commandlistenerwrapper.getPosition());
             Predicate<Entity> predicate = this.getPredicate(vec3d);
@@ -197,7 +197,7 @@ public class EntitySelector {
                     ServerPlayer entityplayer1 = (ServerPlayer) commandlistenerwrapper.getEntity();
 
                     if (predicate.test(entityplayer1)) {
-                        return Lists.newArrayList(new ServerPlayer[]{entityplayer1});
+                        return !canSee(commandlistenerwrapper, entityplayer1) ? Collections.emptyList() : Lists.newArrayList(entityplayer1); // Purpur
                     }
                 }
 
@@ -207,6 +207,7 @@ public class EntitySelector {
 
                 if (this.isWorldLimited()) {
                     object = commandlistenerwrapper.getLevel().getPlayers(predicate);
+                    ((List) object).removeIf(entityplayer3 -> !canSee(commandlistenerwrapper, (ServerPlayer) entityplayer3)); // Purpur
                 } else {
                     object = Lists.newArrayList();
                     Iterator iterator = commandlistenerwrapper.getServer().getPlayerList().getPlayers().iterator();
@@ -214,7 +215,7 @@ public class EntitySelector {
                     while (iterator.hasNext()) {
                         ServerPlayer entityplayer2 = (ServerPlayer) iterator.next();
 
-                        if (predicate.test(entityplayer2)) {
+                        if (predicate.test(entityplayer2) && canSee(commandlistenerwrapper, entityplayer2)) { // Purpur
                             ((List) object).add(entityplayer2);
                         }
                     }
@@ -256,4 +257,10 @@ public class EntitySelector {
     public static Component joinNames(List<? extends Entity> list) {
         return ComponentUtils.formatList(list, Entity::getDisplayName);
     }
+
+    // Purpur start
+    private boolean canSee(CommandSourceStack sender, ServerPlayer target) {
+        return !net.pl3x.purpur.PurpurConfig.hideHiddenPlayersFromEntitySelector || !(sender.getEntity() instanceof ServerPlayer player) || player.getBukkitEntity().canSee(target.getBukkitEntity());
+    }
+    // Purpur end
 }
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
index 5052879dd910180918052d9cc00df63e398479ee..0496d20d982815cc1c56051331ffb51e3d9d0038 100644
--- a/src/main/java/net/pl3x/purpur/PurpurConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -356,6 +356,7 @@ public class PurpurConfig {
     public static String commandTPSBarTextColorLow = "<gradient:#ff5555:#aa0000><text></gradient>";
     public static int commandTPSBarTickInterval = 20;
     public static boolean commandGamemodeRequiresPermission = false;
+    public static boolean hideHiddenPlayersFromEntitySelector = false;
     private static void commandSettings() {
         commandTPSBarTitle = getString("settings.command.tpsbar.title", commandTPSBarTitle);
         commandTPSBarProgressOverlay = BossBar.Overlay.valueOf(getString("settings.command.tpsbar.overlay", commandTPSBarProgressOverlay.name()));
@@ -368,6 +369,7 @@ public class PurpurConfig {
         commandTPSBarTextColorLow = getString("settings.command.tpsbar.text-color.low", commandTPSBarTextColorLow);
         commandTPSBarTickInterval = getInt("settings.command.tpsbar.tick-interval", commandTPSBarTickInterval);
         commandGamemodeRequiresPermission = getBoolean("settings.command.gamemode.requires-specific-permission", commandGamemodeRequiresPermission);
+        hideHiddenPlayersFromEntitySelector = getBoolean("settings.command.hide-hidden-players-from-entity-selector", hideHiddenPlayersFromEntitySelector);
     }
 
     public static boolean barrelSixRows = false;
