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

import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
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.Mob;

public class SetWalkTargetFromAttackTargetIfTargetOutOfReach extends Behavior<Mob> {
    private final float speedModifier;
    
    public SetWalkTargetFromAttackTargetIfTargetOutOfReach(final float float1) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)MemoryModuleType.ATTACK_TARGET, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<WalkTarget>)MemoryModuleType.VISIBLE_LIVING_ENTITIES, MemoryStatus.REGISTERED));
        this.speedModifier = float1;
    }
    
    @Override
    protected void start(final ServerLevel zd, final Mob aoz, final long long3) {
        final LivingEntity aoy6 = aoz.getBrain().<LivingEntity>getMemory(MemoryModuleType.ATTACK_TARGET).get();
        if (BehaviorUtils.canSee(aoz, aoy6) && BehaviorUtils.isWithinAttackRange(aoz, aoy6, 1)) {
            this.clearWalkTarget(aoz);
        }
        else {
            this.setWalkAndLookTarget(aoz, aoy6);
        }
    }
    
    private void setWalkAndLookTarget(final LivingEntity aoy1, final LivingEntity aoy2) {
        final Brain apr4 = aoy1.getBrain();
        apr4.<EntityTracker>setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(aoy2, true));
        final WalkTarget awr5 = new WalkTarget(new EntityTracker(aoy2, false), this.speedModifier, 0);
        apr4.<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, awr5);
    }
    
    private void clearWalkTarget(final LivingEntity aoy) {
        aoy.getBrain().<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
    }
}
