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

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

public class PlayerSensor extends Sensor<LivingEntity> {
    private static final TargetingConditions TARGETING;
    
    @Override
    public Set<MemoryModuleType<?>> requires() {
        return ImmutableSet.of(MemoryModuleType.NEAREST_PLAYERS, (MemoryModuleType<List<Player>>)MemoryModuleType.NEAREST_VISIBLE_PLAYER, (MemoryModuleType<List<Player>>)MemoryModuleType.NEAREST_VISIBLE_TARGETABLE_PLAYER);
    }
    
    @Override
    protected void doTick(final ServerLevel zd, final LivingEntity aoy) {
        final List<Player> list4 = zd.players().stream().filter(EntitySelector.NO_SPECTATORS).filter(ze -> aoy.distanceToSqr(ze) < 256.0).sorted(Comparator.comparingDouble(aoy::distanceToSqr)).collect(Collectors.toList());
        final Brain<?> apr5 = aoy.getBrain();
        apr5.<List<Player>>setMemory(MemoryModuleType.NEAREST_PLAYERS, list4);
        final List<Player> list5 = list4.stream().filter(bec -> PlayerSensor.TARGETING.test(aoy, bec)).collect(Collectors.toList());
        apr5.<Player>setMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER, list5.isEmpty() ? null : list5.get(0));
        final Optional<Player> optional7 = list5.stream().filter(EntitySelector.ATTACK_ALLOWED).findFirst();
        apr5.<Player>setMemory(MemoryModuleType.NEAREST_VISIBLE_TARGETABLE_PLAYER, optional7);
    }
    
    static {
        TARGETING = new TargetingConditions().range(16.0).allowSameTeam().allowNonAttackable();
    }
}
