package net.minecraft.world.entity.ai.sensing;

import com.google.common.collect.ImmutableSet;
import java.util.Set;
import net.minecraft.world.entity.ai.Brain;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Predicate;
import java.util.List;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import java.util.function.ToDoubleFunction;
import java.util.Comparator;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.entity.LivingEntity;

public class NearestLivingEntitySensor extends Sensor<LivingEntity> {
    private static final TargetingConditions TARGETING;
    
    @Override
    protected void doTick(final ServerLevel xd, final LivingEntity akz) {
        final List<LivingEntity> list4 = xd.<LivingEntity>getEntitiesOfClass(LivingEntity.class, akz.getBoundingBox().inflate(16.0, 16.0, 16.0), akz2 -> akz2 != akz && akz2.isAlive());
        list4.sort(Comparator.comparingDouble(akz::distanceToSqr));
        final Brain<?> alp5 = akz.getBrain();
        alp5.<List<LivingEntity>>setMemory(MemoryModuleType.LIVING_ENTITIES, list4);
        alp5.<List<LivingEntity>>setMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES, list4.stream().filter(akz2 -> NearestLivingEntitySensor.TARGETING.test(akz, akz2)).filter(akz::canSee).collect(Collectors.toList()));
    }
    
    @Override
    public Set<MemoryModuleType<?>> requires() {
        return ImmutableSet.of(MemoryModuleType.LIVING_ENTITIES, MemoryModuleType.VISIBLE_LIVING_ENTITIES);
    }
    
    static {
        TARGETING = new TargetingConditions().range(16.0).allowSameTeam().allowNonAttackable().allowUnseeable();
    }
}
