--- a/net/minecraft/commands/arguments/selector/EntitySelector.java
+++ b/net/minecraft/commands/arguments/selector/EntitySelector.java
@@ -3,6 +3,7 @@
 import com.google.common.collect.Lists;
 import com.mojang.brigadier.exceptions.CommandSyntaxException;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.UUID;
 import java.util.function.BiConsumer;
@@ -23,11 +24,11 @@
 import net.minecraft.world.phys.Vec3;
 
 public class EntitySelector {
+
     public static final int INFINITE = Integer.MAX_VALUE;
-    public static final BiConsumer<Vec3, List<? extends Entity>> ORDER_ARBITRARY = (pos, entities) -> {
+    public static final BiConsumer<Vec3, List<? extends Entity>> ORDER_ARBITRARY = (vec3d, list) -> {
     };
     private static final EntityTypeTest<Entity, ?> ANY_TYPE = new EntityTypeTest<Entity, Entity>() {
-        @Override
         public Entity tryCast(Entity obj) {
             return obj;
         }
@@ -54,21 +55,7 @@
     private final EntityTypeTest<Entity, ?> type;
     private final boolean usesSelector;
 
-    public EntitySelector(
-        int count,
-        boolean includesNonPlayers,
-        boolean localWorldOnly,
-        Predicate<Entity> basePredicate,
-        MinMaxBounds.Doubles distance,
-        Function<Vec3, Vec3> positionOffset,
-        @Nullable AABB box,
-        BiConsumer<Vec3, List<? extends Entity>> sorter,
-        boolean senderOnly,
-        @Nullable String playerName,
-        @Nullable UUID uuid,
-        @Nullable EntityType<?> type,
-        boolean usesAt
-    ) {
+    public EntitySelector(int count, boolean includesNonPlayers, boolean localWorldOnly, Predicate<Entity> basePredicate, MinMaxBounds.Doubles distance, Function<Vec3, Vec3> positionOffset, @Nullable AABB box, BiConsumer<Vec3, List<? extends Entity>> sorter, boolean senderOnly, @Nullable String playerName, @Nullable UUID uuid, @Nullable EntityType<?> type, boolean usesAt) {
         this.maxResults = count;
         this.includesEntities = includesNonPlayers;
         this.worldLimited = localWorldOnly;
@@ -80,7 +67,7 @@
         this.currentEntity = senderOnly;
         this.playerName = playerName;
         this.entityUUID = uuid;
-        this.type = (EntityTypeTest<Entity, ?>)(type == null ? ANY_TYPE : type);
+        this.type = (EntityTypeTest) (type == null ? EntitySelector.ANY_TYPE : type);
         this.usesSelector = usesAt;
     }
 
@@ -105,7 +92,7 @@
     }
 
     private void checkPermissions(CommandSourceStack source) throws CommandSyntaxException {
-        if (this.usesSelector && !source.hasPermission(2)) {
+        if (!source.bypassSelectorPermissions && (this.usesSelector && !source.hasPermission(2, "minecraft.command.selector"))) { // CraftBukkit // Paper - add bypass for selector perms
             throw EntityArgument.ERROR_SELECTORS_NOT_ALLOWED.create();
         }
     }
@@ -113,17 +100,20 @@
     public Entity findSingleEntity(CommandSourceStack source) throws CommandSyntaxException {
         this.checkPermissions(source);
         List<? extends Entity> list = this.findEntities(source);
+
         if (list.isEmpty()) {
             throw EntityArgument.NO_ENTITIES_FOUND.create();
         } else if (list.size() > 1) {
             throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.create();
         } else {
-            return list.get(0);
+            return (Entity) list.get(0);
         }
     }
 
     public List<? extends Entity> findEntities(CommandSourceStack source) throws CommandSyntaxException {
-        return this.findEntitiesRaw(source).stream().filter(entity -> entity.getType().isEnabled(source.enabledFeatures())).toList();
+        return this.findEntitiesRaw(source).stream().filter((entity) -> {
+            return entity.getType().isEnabled(source.enabledFeatures());
+        }).toList();
     }
 
     private List<? extends Entity> findEntitiesRaw(CommandSourceStack source) throws CommandSyntaxException {
@@ -131,113 +121,147 @@
         if (!this.includesEntities) {
             return this.findPlayers(source);
         } else if (this.playerName != null) {
-            ServerPlayer serverPlayer = source.getServer().getPlayerList().getPlayerByName(this.playerName);
-            return (List<? extends Entity>)(serverPlayer == null ? Collections.emptyList() : Lists.newArrayList(serverPlayer));
+            ServerPlayer entityplayer = source.getServer().getPlayerList().getPlayerByName(this.playerName);
+
+            return (List) (entityplayer == null ? Collections.emptyList() : Lists.newArrayList(new ServerPlayer[]{entityplayer}));
         } else if (this.entityUUID != null) {
-            for (ServerLevel serverLevel : source.getServer().getAllLevels()) {
-                Entity entity = serverLevel.getEntity(this.entityUUID);
-                if (entity != null) {
-                    return Lists.newArrayList(entity);
+            Iterator iterator = source.getServer().getAllLevels().iterator();
+
+            Entity entity;
+
+            do {
+                if (!iterator.hasNext()) {
+                    return Collections.emptyList();
                 }
-            }
 
-            return Collections.emptyList();
+                ServerLevel worldserver = (ServerLevel) iterator.next();
+
+                entity = worldserver.getEntity(this.entityUUID);
+            } while (entity == null);
+
+            return Lists.newArrayList(new Entity[]{entity});
         } else {
-            Vec3 vec3 = this.position.apply(source.getPosition());
-            Predicate<Entity> predicate = this.getPredicate(vec3);
+            Vec3 vec3d = (Vec3) this.position.apply(source.getPosition());
+            Predicate<Entity> predicate = this.getPredicate(vec3d);
+
             if (this.currentEntity) {
-                return (List<? extends Entity>)(source.getEntity() != null && predicate.test(source.getEntity())
-                    ? Lists.newArrayList(source.getEntity())
-                    : Collections.emptyList());
+                return (List) (source.getEntity() != null && predicate.test(source.getEntity()) ? Lists.newArrayList(new Entity[]{source.getEntity()}) : Collections.emptyList());
             } else {
                 List<Entity> list = Lists.newArrayList();
+
                 if (this.isWorldLimited()) {
-                    this.addEntities(list, source.getLevel(), vec3, predicate);
+                    this.addEntities(list, source.getLevel(), vec3d, predicate);
                 } else {
-                    for (ServerLevel serverLevel2 : source.getServer().getAllLevels()) {
-                        this.addEntities(list, serverLevel2, vec3, predicate);
+                    Iterator iterator1 = source.getServer().getAllLevels().iterator();
+
+                    while (iterator1.hasNext()) {
+                        ServerLevel worldserver1 = (ServerLevel) iterator1.next();
+
+                        this.addEntities(list, worldserver1, vec3d, predicate);
                     }
                 }
 
-                return this.sortAndLimit(vec3, list);
+                return this.sortAndLimit(vec3d, list);
             }
         }
     }
 
     private void addEntities(List<Entity> entities, ServerLevel world, Vec3 pos, Predicate<Entity> predicate) {
         int i = this.getResultLimit();
+
         if (entities.size() < i) {
             if (this.aabb != null) {
                 world.getEntities(this.type, this.aabb.move(pos), predicate, entities, i);
             } else {
                 world.getEntities(this.type, predicate, entities, i);
             }
+
         }
     }
 
     private int getResultLimit() {
-        return this.order == ORDER_ARBITRARY ? this.maxResults : Integer.MAX_VALUE;
+        return this.order == EntitySelector.ORDER_ARBITRARY ? this.maxResults : Integer.MAX_VALUE;
     }
 
     public ServerPlayer findSinglePlayer(CommandSourceStack source) throws CommandSyntaxException {
         this.checkPermissions(source);
         List<ServerPlayer> list = this.findPlayers(source);
+
         if (list.size() != 1) {
             throw EntityArgument.NO_PLAYERS_FOUND.create();
         } else {
-            return list.get(0);
+            return (ServerPlayer) list.get(0);
         }
     }
 
     public List<ServerPlayer> findPlayers(CommandSourceStack source) throws CommandSyntaxException {
         this.checkPermissions(source);
+        ServerPlayer entityplayer;
+
         if (this.playerName != null) {
-            ServerPlayer serverPlayer = source.getServer().getPlayerList().getPlayerByName(this.playerName);
-            return (List<ServerPlayer>)(serverPlayer == null ? Collections.emptyList() : Lists.newArrayList(serverPlayer));
+            entityplayer = source.getServer().getPlayerList().getPlayerByName(this.playerName);
+            return (List) (entityplayer == null ? Collections.emptyList() : Lists.newArrayList(new ServerPlayer[]{entityplayer}));
         } else if (this.entityUUID != null) {
-            ServerPlayer serverPlayer2 = source.getServer().getPlayerList().getPlayer(this.entityUUID);
-            return (List<ServerPlayer>)(serverPlayer2 == null ? Collections.emptyList() : Lists.newArrayList(serverPlayer2));
+            entityplayer = source.getServer().getPlayerList().getPlayer(this.entityUUID);
+            return (List) (entityplayer == null ? Collections.emptyList() : Lists.newArrayList(new ServerPlayer[]{entityplayer}));
         } else {
-            Vec3 vec3 = this.position.apply(source.getPosition());
-            Predicate<Entity> predicate = this.getPredicate(vec3);
+            Vec3 vec3d = (Vec3) this.position.apply(source.getPosition());
+            Predicate<Entity> predicate = this.getPredicate(vec3d);
+
             if (this.currentEntity) {
-                if (source.getEntity() instanceof ServerPlayer serverPlayer3 && predicate.test(serverPlayer3)) {
-                    return Lists.newArrayList(serverPlayer3);
+                Entity entity = source.getEntity();
+
+                if (entity instanceof ServerPlayer) {
+                    ServerPlayer entityplayer1 = (ServerPlayer) entity;
+
+                    if (predicate.test(entityplayer1)) {
+                        return Lists.newArrayList(new ServerPlayer[]{entityplayer1});
+                    }
                 }
 
                 return Collections.emptyList();
             } else {
                 int i = this.getResultLimit();
-                List<ServerPlayer> list;
+                Object object;
+
                 if (this.isWorldLimited()) {
-                    list = source.getLevel().getPlayers(predicate, i);
+                    object = source.getLevel().getPlayers(predicate, i);
                 } else {
-                    list = Lists.newArrayList();
+                    object = Lists.newArrayList();
+                    Iterator iterator = source.getServer().getPlayerList().getPlayers().iterator();
 
-                    for (ServerPlayer serverPlayer4 : source.getServer().getPlayerList().getPlayers()) {
-                        if (predicate.test(serverPlayer4)) {
-                            list.add(serverPlayer4);
-                            if (list.size() >= i) {
-                                return list;
+                    while (iterator.hasNext()) {
+                        ServerPlayer entityplayer2 = (ServerPlayer) iterator.next();
+
+                        if (predicate.test(entityplayer2)) {
+                            ((List) object).add(entityplayer2);
+                            if (((List) object).size() >= i) {
+                                return (List) object;
                             }
                         }
                     }
                 }
 
-                return this.sortAndLimit(vec3, list);
+                return this.sortAndLimit(vec3d, (List) object);
             }
         }
     }
 
     private Predicate<Entity> getPredicate(Vec3 pos) {
         Predicate<Entity> predicate = this.predicate;
+
         if (this.aabb != null) {
-            AABB aABB = this.aabb.move(pos);
-            predicate = predicate.and(entity -> aABB.intersects(entity.getBoundingBox()));
+            AABB axisalignedbb = this.aabb.move(pos);
+
+            predicate = predicate.and((entity) -> {
+                return axisalignedbb.intersects(entity.getBoundingBox());
+            });
         }
 
         if (!this.range.isAny()) {
-            predicate = predicate.and(entity -> this.range.matchesSqr(entity.distanceToSqr(pos)));
+            predicate = predicate.and((entity) -> {
+                return this.range.matchesSqr(entity.distanceToSqr(pos));
+            });
         }
 
         return predicate;
