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.util.RandomPos;
import java.util.Optional;
import net.minecraft.core.Position;
import java.util.Objects;
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.core.GlobalPos;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.PathfinderMob;

public class StrollAroundPoi extends Behavior<PathfinderMob> {
    private final MemoryModuleType<GlobalPos> memoryType;
    private long nextOkStartTime;
    private final int maxDistanceFromPoi;
    
    public StrollAroundPoi(final MemoryModuleType<GlobalPos> arl, final int integer) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)arl, MemoryStatus.VALUE_PRESENT));
        this.memoryType = arl;
        this.maxDistanceFromPoi = integer;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final PathfinderMob alg) {
        final Optional<GlobalPos> optional4 = alg.getBrain().<GlobalPos>getMemory(this.memoryType);
        return optional4.isPresent() && Objects.equals(xd.getDimension().getType(), optional4.get().dimension()) && optional4.get().pos().closerThan(alg.position(), this.maxDistanceFromPoi);
    }
    
    @Override
    protected void start(final ServerLevel xd, final PathfinderMob alg, final long long3) {
        if (long3 > this.nextOkStartTime) {
            final Optional<Vec3> optional6 = Optional.<Vec3>ofNullable(RandomPos.getLandPos(alg, 8, 6));
            alg.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, optional6.<WalkTarget>map(cvl -> new WalkTarget(cvl, 0.4f, 1)));
            this.nextOkStartTime = long3 + 180L;
        }
    }
}
