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

import net.minecraft.world.entity.LivingEntity;
import java.util.List;
import net.minecraft.world.entity.ai.Brain;
import java.util.Optional;
import java.util.function.Predicate;
import net.minecraft.world.entity.Entity;
import java.util.function.ToDoubleFunction;
import java.util.Comparator;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.item.ItemEntity;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import java.util.Set;
import net.minecraft.world.entity.Mob;

public class NearestItemSensor extends Sensor<Mob> {
    @Override
    public Set<MemoryModuleType<?>> requires() {
        return ImmutableSet.of(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM);
    }
    
    @Override
    protected void doTick(final ServerLevel zd, final Mob aoz) {
        final Brain<?> apr4 = aoz.getBrain();
        final List<ItemEntity> list5 = zd.<ItemEntity>getEntitiesOfClass(ItemEntity.class, aoz.getBoundingBox().inflate(8.0, 4.0, 8.0), bbg -> true);
        list5.sort(Comparator.comparingDouble(aoz::distanceToSqr));
        final Optional<ItemEntity> optional6 = list5.stream().filter(bbg -> aoz.wantsToPickUp(bbg.getItem())).filter(bbg -> bbg.closerThan(aoz, 9.0)).filter(aoz::canSee).findFirst();
        apr4.<ItemEntity>setMemory(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM, optional6);
    }
}
