package net.minecraft.world.entity;

import javax.annotation.Nullable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;
import net.minecraft.world.Difficulty;
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> ATTACK_ALLOWED;
    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 aom -> aom != null && aom.distanceToSqr(double1, double2, double3) <= n;
    }
    
    public static Predicate<Entity> pushableBy(final Entity aom) {
        final Team dfo2 = aom.getTeam();
        final Team.CollisionRule a3 = (dfo2 == null) ? Team.CollisionRule.ALWAYS : dfo2.getCollisionRule();
        if (a3 == Team.CollisionRule.NEVER) {
            return Predicates.alwaysFalse();
        }
        Team dfo3;
        Team.CollisionRule a4;
        final Team team;
        boolean boolean7;
        final Team.CollisionRule collisionRule;
        return EntitySelector.NO_SPECTATORS.and(aom4 -> {
            if (!aom4.isPushable()) {
                return false;
            }
            else if (aom.level.isClientSide && (!(aom4 instanceof Player) || !aom4.isLocalPlayer())) {
                return false;
            }
            else {
                dfo3 = aom4.getTeam();
                a4 = ((dfo3 == null) ? Team.CollisionRule.ALWAYS : dfo3.getCollisionRule());
                if (a4 == Team.CollisionRule.NEVER) {
                    return false;
                }
                else {
                    boolean7 = (team != null && team.isAlliedTo(dfo3));
                    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 aom) {
        return aom2 -> {
            while (aom2.isPassenger()) {
                aom2 = aom2.getVehicle();
                if (aom2 == aom) {
                    return false;
                }
            }
            return true;
        };
    }
    
    static {
        ENTITY_STILL_ALIVE = Entity::isAlive;
        LIVING_ENTITY_STILL_ALIVE = LivingEntity::isAlive;
        ENTITY_NOT_BEING_RIDDEN = (aom -> aom.isAlive() && !aom.isVehicle() && !aom.isPassenger());
        CONTAINER_ENTITY_SELECTOR = (aom -> aom instanceof Container && aom.isAlive());
        NO_CREATIVE_OR_SPECTATOR = (aom -> !(aom instanceof Player) || (!aom.isSpectator() && !aom.isCreative()));
        ATTACK_ALLOWED = (aom -> !(aom instanceof Player) || (!aom.isSpectator() && !aom.isCreative() && aom.level.getDifficulty() != Difficulty.PEACEFUL));
        NO_SPECTATORS = (aom -> !aom.isSpectator());
    }
    
    public static class MobCanWearArmorEntitySelector implements Predicate<Entity> {
        private final ItemStack itemStack;
        
        public MobCanWearArmorEntitySelector(final ItemStack bki) {
            this.itemStack = bki;
        }
        
        @Override
        public boolean test(@Nullable final Entity aom) {
            if (!aom.isAlive()) {
                return false;
            }
            if (!(aom instanceof LivingEntity)) {
                return false;
            }
            final LivingEntity aoy3 = (LivingEntity)aom;
            return aoy3.canTakeItem(this.itemStack);
        }
    }
}
