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

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.core.SectionPos;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.npc.Villager;

public class GoToClosestVillage extends Behavior<Villager> {
    private final float speedModifier;
    private final int closeEnoughDistance;
    
    public GoToClosestVillage(final float float1, final int integer) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT));
        this.speedModifier = float1;
        this.closeEnoughDistance = integer;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        return !zd.isVillage(bdp.blockPosition());
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        final PoiManager axz6 = zd.getPoiManager();
        final int integer7 = axz6.sectionsToVillage(SectionPos.of(bdp.blockPosition()));
        Vec3 dem8 = null;
        for (int integer8 = 0; integer8 < 5; ++integer8) {
            final Vec3 dem9 = RandomPos.getLandPos(bdp, 15, 7, fu -> -zd.sectionsToVillage(SectionPos.of(fu)));
            if (dem9 != null) {
                final int integer9 = axz6.sectionsToVillage(SectionPos.of(new BlockPos(dem9)));
                if (integer9 < integer7) {
                    dem8 = dem9;
                    break;
                }
                if (integer9 == integer7) {
                    dem8 = dem9;
                }
            }
        }
        if (dem8 != null) {
            bdp.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(dem8, this.speedModifier, this.closeEnoughDistance));
        }
    }
}
