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

import net.minecraft.world.entity.LivingEntity;
import java.util.Optional;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraft.core.BlockPos;
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.PathfinderMob;

public class VillageBoundRandomStroll extends Behavior<PathfinderMob> {
    private final float speedModifier;
    private final int maxXyDist;
    private final int maxYDist;
    
    public VillageBoundRandomStroll(final float float1) {
        this(float1, 10, 7);
    }
    
    public VillageBoundRandomStroll(final float float1, final int integer2, final int integer3) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT));
        this.speedModifier = float1;
        this.maxXyDist = integer2;
        this.maxYDist = integer3;
    }
    
    @Override
    protected void start(final ServerLevel zd, final PathfinderMob apg, final long long3) {
        final BlockPos fu6 = apg.blockPosition();
        if (zd.isVillage(fu6)) {
            this.setRandomPos(apg);
        }
        else {
            final SectionPos go7 = SectionPos.of(fu6);
            final SectionPos go8 = BehaviorUtils.findSectionClosestToVillage(zd, go7, 2);
            if (go8 != go7) {
                this.setTargetedPos(apg, go8);
            }
            else {
                this.setRandomPos(apg);
            }
        }
    }
    
    private void setTargetedPos(final PathfinderMob apg, final SectionPos go) {
        final Optional<Vec3> optional4 = Optional.<Vec3>ofNullable(RandomPos.getPosTowards(apg, this.maxXyDist, this.maxYDist, Vec3.atBottomCenterOf(go.center())));
        apg.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, optional4.map(dem -> new WalkTarget(dem, this.speedModifier, 0)));
    }
    
    private void setRandomPos(final PathfinderMob apg) {
        final Optional<Vec3> optional3 = Optional.<Vec3>ofNullable(RandomPos.getLandPos(apg, this.maxXyDist, this.maxYDist));
        apg.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, optional3.map(dem -> new WalkTarget(dem, this.speedModifier, 0)));
    }
}
