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

import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import java.util.function.BiPredicate;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;

public class DismountOrSkipMounting<E extends LivingEntity, T extends Entity> extends Behavior<E> {
    private final int maxWalkDistToRideTarget;
    private final BiPredicate<E, Entity> dontRideIf;
    
    public DismountOrSkipMounting(final int integer, final BiPredicate<E, Entity> biPredicate) {
        super(ImmutableMap.<MemoryModuleType<Entity>, MemoryStatus>of(MemoryModuleType.RIDE_TARGET, MemoryStatus.REGISTERED));
        this.maxWalkDistToRideTarget = integer;
        this.dontRideIf = biPredicate;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final E aoy) {
        final Entity aom4 = aoy.getVehicle();
        final Entity aom5 = aoy.getBrain().<Entity>getMemory(MemoryModuleType.RIDE_TARGET).orElse(null);
        if (aom4 == null && aom5 == null) {
            return false;
        }
        final Entity aom6 = (aom4 == null) ? aom5 : aom4;
        return !this.isVehicleValid(aoy, aom6) || this.dontRideIf.test(aoy, aom6);
    }
    
    private boolean isVehicleValid(final E aoy, final Entity aom) {
        return aom.isAlive() && aom.closerThan(aoy, this.maxWalkDistToRideTarget) && aom.level == aoy.level;
    }
    
    @Override
    protected void start(final ServerLevel zd, final E aoy, final long long3) {
        aoy.stopRiding();
        aoy.getBrain().<Entity>eraseMemory(MemoryModuleType.RIDE_TARGET);
    }
}
