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

import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import java.util.Iterator;
import net.minecraft.world.entity.ai.Brain;
import com.google.common.collect.Lists;
import net.minecraft.world.entity.monster.piglin.Piglin;
import java.util.Optional;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.LivingEntity;
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.monster.hoglin.Hoglin;

public class HoglinSpecificSensor extends Sensor<Hoglin> {
    @Override
    public Set<MemoryModuleType<?>> requires() {
        return ImmutableSet.of(MemoryModuleType.VISIBLE_LIVING_ENTITIES, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_REPELLENT, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLIN, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_VISIBLE_ADULT_HOGLINS, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT, new MemoryModuleType[0]);
    }
    
    @Override
    protected void doTick(final ServerLevel zd, final Hoglin bcx) {
        final Brain<?> apr4 = bcx.getBrain();
        apr4.<BlockPos>setMemory(MemoryModuleType.NEAREST_REPELLENT, this.findNearestRepellent(zd, bcx));
        Optional<Piglin> optional5 = Optional.<Piglin>empty();
        int integer6 = 0;
        final List<Hoglin> list7 = Lists.newArrayList();
        final List<LivingEntity> list8 = apr4.<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).orElse(Lists.newArrayList());
        for (final LivingEntity aoy10 : list8) {
            if (aoy10 instanceof Piglin && !aoy10.isBaby()) {
                ++integer6;
                if (!optional5.isPresent()) {
                    optional5 = Optional.<Piglin>of(aoy10);
                }
            }
            if (aoy10 instanceof Hoglin && !aoy10.isBaby()) {
                list7.add((Hoglin)aoy10);
            }
        }
        apr4.<Piglin>setMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLIN, optional5);
        apr4.<List<Hoglin>>setMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_HOGLINS, list7);
        apr4.<Integer>setMemory(MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT, integer6);
        apr4.<Integer>setMemory(MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT, list7.size());
    }
    
    private Optional<BlockPos> findNearestRepellent(final ServerLevel zd, final Hoglin bcx) {
        return BlockPos.findClosestMatch(bcx.blockPosition(), 8, 4, fu -> zd.getBlockState(fu).is(BlockTags.HOGLIN_REPELLENTS));
    }
}
