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.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.PathfinderMob;

public class VillageBoundRandomStroll extends Behavior<PathfinderMob> {
    private final float speed;
    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.speed = float1;
        this.maxXyDist = integer2;
        this.maxYDist = integer3;
    }
    
    @Override
    protected void start(final ServerLevel xd, final PathfinderMob alg, final long long3) {
        final BlockPos fk6 = new BlockPos(alg);
        if (xd.isVillage(fk6)) {
            this.setRandomPos(alg);
        }
        else {
            final SectionPos gd7 = SectionPos.of(fk6);
            final SectionPos gd8 = BehaviorUtils.findSectionClosestToVillage(xd, gd7, 2);
            if (gd8 != gd7) {
                this.setTargetedPos(alg, gd8);
            }
            else {
                this.setRandomPos(alg);
            }
        }
    }
    
    private void setTargetedPos(final PathfinderMob alg, final SectionPos gd) {
        final Optional<Vec3> optional4 = Optional.<Vec3>ofNullable(RandomPos.getPosTowards(alg, this.maxXyDist, this.maxYDist, new Vec3(gd.center())));
        alg.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, optional4.<WalkTarget>map(cvl -> new WalkTarget(cvl, this.speed, 0)));
    }
    
    private void setRandomPos(final PathfinderMob alg) {
        final Optional<Vec3> optional3 = Optional.<Vec3>ofNullable(RandomPos.getLandPos(alg, this.maxXyDist, this.maxYDist));
        alg.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, optional3.<WalkTarget>map(cvl -> new WalkTarget(cvl, this.speed, 0)));
    }
}
