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.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.core.SectionPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
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 speed;
    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.speed = float1;
        this.closeEnoughDistance = integer;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final Villager axw) {
        return !xd.isVillage(new BlockPos(axw));
    }
    
    @Override
    protected void start(final ServerLevel xd, final Villager axw, final long long3) {
        final PoiManager asr6 = xd.getPoiManager();
        final int integer7 = asr6.sectionsToVillage(SectionPos.of(new BlockPos(axw)));
        Vec3 cvl8 = null;
        for (int integer8 = 0; integer8 < 5; ++integer8) {
            final Vec3 cvl9 = RandomPos.getLandPos(axw, 15, 7, fk -> -xd.sectionsToVillage(SectionPos.of(fk)));
            if (cvl9 != null) {
                final int integer9 = asr6.sectionsToVillage(SectionPos.of(new BlockPos(cvl9)));
                if (integer9 < integer7) {
                    cvl8 = cvl9;
                    break;
                }
                if (integer9 == integer7) {
                    cvl8 = cvl9;
                }
            }
        }
        if (cvl8 != null) {
            axw.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(cvl8, this.speed, this.closeEnoughDistance));
        }
    }
}
