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

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Entity;
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 LookAndFollowTradingPlayerSink extends Behavior<Villager> {
    private final float speed;
    
    public LookAndFollowTradingPlayerSink(final float float1) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED), Integer.MAX_VALUE);
        this.speed = float1;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final Villager axw) {
        final Player ayj4 = axw.getTradingPlayer();
        return axw.isAlive() && ayj4 != null && !axw.isInWater() && !axw.hurtMarked && axw.distanceToSqr(ayj4) <= 16.0 && ayj4.containerMenu != null;
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel xd, final Villager axw, final long long3) {
        return this.checkExtraStartConditions(xd, axw);
    }
    
    @Override
    protected void start(final ServerLevel xd, final Villager axw, final long long3) {
        this.followPlayer(axw);
    }
    
    @Override
    protected void stop(final ServerLevel xd, final Villager axw, final long long3) {
        final Brain<?> alp6 = axw.getBrain();
        alp6.<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
        alp6.<PositionWrapper>eraseMemory(MemoryModuleType.LOOK_TARGET);
    }
    
    @Override
    protected void tick(final ServerLevel xd, final Villager axw, final long long3) {
        this.followPlayer(axw);
    }
    
    @Override
    protected boolean timedOut(final long long1) {
        return false;
    }
    
    private void followPlayer(final Villager axw) {
        final EntityPosWrapper amg3 = new EntityPosWrapper(axw.getTradingPlayer());
        final Brain<?> alp4 = axw.getBrain();
        alp4.<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(amg3, this.speed, 2));
        alp4.<PositionWrapper>setMemory(MemoryModuleType.LOOK_TARGET, amg3);
    }
}
