--- 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;
         }
@@ -66,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;
     }
 
@@ -91,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
             throw EntityArgument.ERROR_SELECTORS_NOT_ALLOWED.create();
         }
     }
@@ -99,12 +100,13 @@
     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);
         }
     }
 
@@ -119,39 +121,54 @@
         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);
@@ -163,68 +180,79 @@
     }
 
     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) {
-                    ServerPlayer serverPlayer3 = (ServerPlayer)source.getEntity();
-                    if (predicate.test(serverPlayer3)) {
-                        return Lists.newArrayList(serverPlayer3);
+                    ServerPlayer entityplayer1 = (ServerPlayer) source.getEntity();
+
+                    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);
+            AABB axisalignedbb = this.aabb.move(pos);
+
             predicate = predicate.and((entity) -> {
-                return aABB.intersects(entity.getBoundingBox());
+                return axisalignedbb.intersects(entity.getBoundingBox());
             });
         }
 
