package net.minecraft.world.entity;

import javax.annotation.Nullable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import com.google.common.base.Predicates;
import net.minecraft.world.scores.Team;
import java.util.function.Predicate;

public final class EntitySelector {
    public static final Predicate<Entity> ENTITY_STILL_ALIVE;
    public static final Predicate<LivingEntity> LIVING_ENTITY_STILL_ALIVE;
    public static final Predicate<Entity> ENTITY_NOT_BEING_RIDDEN;
    public static final Predicate<Entity> CONTAINER_ENTITY_SELECTOR;
    public static final Predicate<Entity> NO_CREATIVE_OR_SPECTATOR;
    public static final Predicate<Entity> NO_SPECTATORS;
    
    public static Predicate<Entity> withinDistance(final double double1, final double double2, final double double3, final double double4) {
        final double double5 = double4 * double4;
        final double n;
        return akq -> akq != null && akq.distanceToSqr(double1, double2, double3) <= n;
    }
    
    public static Predicate<Entity> pushableBy(final Entity akq) {
        final Team cwn2 = akq.getTeam();
        final Team.CollisionRule a3 = (cwn2 == null) ? Team.CollisionRule.ALWAYS : cwn2.getCollisionRule();
        if (a3 == Team.CollisionRule.NEVER) {
            return Predicates.alwaysFalse();
        }
        Team cwn3;
        Team.CollisionRule a4;
        final Team team;
        boolean boolean7;
        final Team.CollisionRule collisionRule;
        return EntitySelector.NO_SPECTATORS.and(akq4 -> {
            if (!akq4.isPushable()) {
                return false;
            }
            else if (akq.level.isClientSide && (!(akq4 instanceof Player) || !akq4.isLocalPlayer())) {
                return false;
            }
            else {
                cwn3 = akq4.getTeam();
                a4 = ((cwn3 == null) ? Team.CollisionRule.ALWAYS : cwn3.getCollisionRule());
                if (a4 == Team.CollisionRule.NEVER) {
                    return false;
                }
                else {
                    boolean7 = (team != null && team.isAlliedTo(cwn3));
                    return ((collisionRule != Team.CollisionRule.PUSH_OWN_TEAM && a4 != Team.CollisionRule.PUSH_OWN_TEAM) || !boolean7) && ((collisionRule != Team.CollisionRule.PUSH_OTHER_TEAMS && a4 != Team.CollisionRule.PUSH_OTHER_TEAMS) || boolean7);
                }
            }
        });
    }
    
    public static Predicate<Entity> notRiding(final Entity akq) {
        return akq2 -> {
            while (akq2.isPassenger()) {
                akq2 = akq2.getVehicle();
                if (akq2 == akq) {
                    return false;
                }
            }
            return true;
        };
    }
    
    static {
        ENTITY_STILL_ALIVE = Entity::isAlive;
        LIVING_ENTITY_STILL_ALIVE = LivingEntity::isAlive;
        ENTITY_NOT_BEING_RIDDEN = (akq -> akq.isAlive() && !akq.isVehicle() && !akq.isPassenger());
        CONTAINER_ENTITY_SELECTOR = (akq -> akq instanceof Container && akq.isAlive());
        NO_CREATIVE_OR_SPECTATOR = (akq -> !(akq instanceof Player) || (!akq.isSpectator() && !akq.isCreative()));
        NO_SPECTATORS = (akq -> !akq.isSpectator());
    }
    
    public static class MobCanWearArmourEntitySelector implements Predicate<Entity> {
        private final ItemStack itemStack;
        
        public MobCanWearArmourEntitySelector(final ItemStack ben) {
            this.itemStack = ben;
        }
        
        @Override
        public boolean test(@Nullable final Entity akq) {
            if (!akq.isAlive()) {
                return false;
            }
            if (!(akq instanceof LivingEntity)) {
                return false;
            }
            final LivingEntity akz3 = (LivingEntity)akq;
            return akz3.canTakeItem(this.itemStack);
        }
    }
}
