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 speedModifier;
    
    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.speedModifier = float1;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        final Player bec4 = bdp.getTradingPlayer();
        return bdp.isAlive() && bec4 != null && !bdp.isInWater() && !bdp.hurtMarked && bdp.distanceToSqr(bec4) <= 16.0 && bec4.containerMenu != null;
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final Villager bdp, final long long3) {
        return this.checkExtraStartConditions(zd, bdp);
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        this.followPlayer(bdp);
    }
    
    @Override
    protected void stop(final ServerLevel zd, final Villager bdp, final long long3) {
        final Brain<?> apr6 = bdp.getBrain();
        apr6.<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
        apr6.<PositionTracker>eraseMemory(MemoryModuleType.LOOK_TARGET);
    }
    
    @Override
    protected void tick(final ServerLevel zd, final Villager bdp, final long long3) {
        this.followPlayer(bdp);
    }
    
    @Override
    protected boolean timedOut(final long long1) {
        return false;
    }
    
    private void followPlayer(final Villager bdp) {
        final Brain<?> apr3 = bdp.getBrain();
        apr3.<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new EntityTracker(bdp.getTradingPlayer(), false), this.speedModifier, 2));
        apr3.<EntityTracker>setMemory((MemoryModuleType<EntityTracker>)MemoryModuleType.LOOK_TARGET, new EntityTracker(bdp.getTradingPlayer(), true));
    }
}
