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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.CampfireBlock;
import net.minecraft.world.level.block.Blocks;
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 net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.minecraft.world.entity.monster.WitherSkeleton;
import net.minecraft.world.entity.monster.piglin.PiglinAi;
import net.minecraft.world.entity.EntitySelector;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.monster.piglin.Piglin;
import net.minecraft.world.entity.monster.hoglin.Hoglin;
import net.minecraft.world.entity.Mob;
import java.util.Optional;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
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.LivingEntity;

public class PiglinSpecificSensor extends Sensor<LivingEntity> {
    @Override
    public Set<MemoryModuleType<?>> requires() {
        return ImmutableSet.of(MemoryModuleType.VISIBLE_LIVING_ENTITIES, MemoryModuleType.LIVING_ENTITIES, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_VISIBLE_NEMESIS, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_TARGETABLE_PLAYER_NOT_WEARING_GOLD, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_PLAYER_HOLDING_WANTED_ITEM, (MemoryModuleType<List<LivingEntity>>)MemoryModuleType.NEAREST_VISIBLE_HUNTABLE_HOGLIN, MemoryModuleType.NEAREST_VISIBLE_BABY_HOGLIN, MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS, MemoryModuleType.NEAREST_ADULT_PIGLINS, MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT, MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT, MemoryModuleType.NEAREST_REPELLENT);
    }
    
    @Override
    protected void doTick(final ServerLevel zd, final LivingEntity aoy) {
        final Brain<?> apr4 = aoy.getBrain();
        apr4.<BlockPos>setMemory(MemoryModuleType.NEAREST_REPELLENT, findNearestRepellent(zd, aoy));
        Optional<Mob> optional5 = Optional.<Mob>empty();
        Optional<Hoglin> optional6 = Optional.<Hoglin>empty();
        Optional<Hoglin> optional7 = Optional.<Hoglin>empty();
        Optional<Piglin> optional8 = Optional.<Piglin>empty();
        Optional<LivingEntity> optional9 = Optional.<LivingEntity>empty();
        Optional<Player> optional10 = Optional.<Player>empty();
        Optional<Player> optional11 = Optional.<Player>empty();
        int integer12 = 0;
        final List<Piglin> list13 = Lists.newArrayList();
        final List<Piglin> list14 = Lists.newArrayList();
        final List<LivingEntity> list15 = apr4.<List<LivingEntity>>getMemory(MemoryModuleType.VISIBLE_LIVING_ENTITIES).orElse(ImmutableList.of());
        for (final LivingEntity aoy2 : list15) {
            if (aoy2 instanceof Hoglin) {
                final Hoglin bcx18 = (Hoglin)aoy2;
                if (bcx18.isBaby() && !optional7.isPresent()) {
                    optional7 = Optional.<Hoglin>of(bcx18);
                }
                else {
                    if (!bcx18.isAdult()) {
                        continue;
                    }
                    ++integer12;
                    if (optional6.isPresent() || !bcx18.canBeHunted()) {
                        continue;
                    }
                    optional6 = Optional.<Hoglin>of(bcx18);
                }
            }
            else if (aoy2 instanceof Piglin) {
                final Piglin bdc18 = (Piglin)aoy2;
                if (bdc18.isBaby() && !optional8.isPresent()) {
                    optional8 = Optional.<Piglin>of(bdc18);
                }
                else {
                    if (!bdc18.isAdult()) {
                        continue;
                    }
                    list13.add(bdc18);
                }
            }
            else if (aoy2 instanceof Player) {
                final Player bec18 = (Player)aoy2;
                if (!optional10.isPresent() && EntitySelector.ATTACK_ALLOWED.test(aoy2) && !PiglinAi.isWearingGold(bec18)) {
                    optional10 = Optional.<Player>of(bec18);
                }
                if (optional11.isPresent() || bec18.isSpectator() || !PiglinAi.isPlayerHoldingLovedItem(bec18)) {
                    continue;
                }
                optional11 = Optional.<Player>of(bec18);
            }
            else if (!optional5.isPresent() && (aoy2 instanceof WitherSkeleton || aoy2 instanceof WitherBoss)) {
                optional5 = Optional.<Mob>of(aoy2);
            }
            else {
                if (optional9.isPresent() || !PiglinAi.isZombified(aoy2.getType())) {
                    continue;
                }
                optional9 = Optional.<LivingEntity>of(aoy2);
            }
        }
        final List<LivingEntity> list16 = apr4.<List<LivingEntity>>getMemory(MemoryModuleType.LIVING_ENTITIES).orElse(ImmutableList.of());
        for (final LivingEntity aoy3 : list16) {
            if (aoy3 instanceof Piglin && ((Piglin)aoy3).isAdult()) {
                list14.add((Piglin)aoy3);
            }
        }
        apr4.<Mob>setMemory(MemoryModuleType.NEAREST_VISIBLE_NEMESIS, optional5);
        apr4.<Hoglin>setMemory(MemoryModuleType.NEAREST_VISIBLE_HUNTABLE_HOGLIN, optional6);
        apr4.<Hoglin>setMemory(MemoryModuleType.NEAREST_VISIBLE_BABY_HOGLIN, optional7);
        apr4.<Piglin>setMemory(MemoryModuleType.NEAREST_VISIBLE_BABY_PIGLIN, optional8);
        apr4.<LivingEntity>setMemory(MemoryModuleType.NEAREST_VISIBLE_ZOMBIFIED, optional9);
        apr4.<Player>setMemory(MemoryModuleType.NEAREST_TARGETABLE_PLAYER_NOT_WEARING_GOLD, optional10);
        apr4.<Player>setMemory(MemoryModuleType.NEAREST_PLAYER_HOLDING_WANTED_ITEM, optional11);
        apr4.<List<Piglin>>setMemory(MemoryModuleType.NEAREST_ADULT_PIGLINS, list14);
        apr4.<List<Piglin>>setMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS, list13);
        apr4.<Integer>setMemory(MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT, list13.size());
        apr4.<Integer>setMemory(MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT, integer12);
    }
    
    private static Optional<BlockPos> findNearestRepellent(final ServerLevel zd, final LivingEntity aoy) {
        return BlockPos.findClosestMatch(aoy.blockPosition(), 8, 4, fu -> isValidRepellent(zd, fu));
    }
    
    private static boolean isValidRepellent(final ServerLevel zd, final BlockPos fu) {
        final BlockState cfj3 = zd.getBlockState(fu);
        final boolean boolean4 = cfj3.is(BlockTags.PIGLIN_REPELLENTS);
        if (boolean4 && cfj3.is(Blocks.SOUL_CAMPFIRE)) {
            return CampfireBlock.isLitCampfire(cfj3);
        }
        return boolean4;
    }
}
