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

import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.Entity;
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.Predicate;
import net.minecraft.world.entity.LivingEntity;

public class GoToWantedItem<E extends LivingEntity> extends Behavior<E> {
    private final Predicate<E> predicate;
    private final int maxDistToWalk;
    private final float speedModifier;
    
    public GoToWantedItem(final float float1, final boolean boolean2, final int integer) {
        this(aoy -> true, float1, boolean2, integer);
    }
    
    public GoToWantedItem(final Predicate<E> predicate, final float float2, final boolean boolean3, final int integer) {
        super(ImmutableMap.<MemoryModuleType<PositionTracker>, MemoryStatus>of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<PositionTracker>)MemoryModuleType.WALK_TARGET, boolean3 ? MemoryStatus.REGISTERED : MemoryStatus.VALUE_ABSENT, (MemoryModuleType<PositionTracker>)MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM, MemoryStatus.VALUE_PRESENT));
        this.predicate = predicate;
        this.maxDistToWalk = integer;
        this.speedModifier = float2;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final E aoy) {
        return this.predicate.test(aoy) && this.getClosestLovedItem(aoy).closerThan(aoy, this.maxDistToWalk);
    }
    
    @Override
    protected void start(final ServerLevel zd, final E aoy, final long long3) {
        BehaviorUtils.setWalkAndLookTargetMemories(aoy, this.getClosestLovedItem(aoy), this.speedModifier, 0);
    }
    
    private ItemEntity getClosestLovedItem(final E aoy) {
        return aoy.getBrain().<ItemEntity>getMemory(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM).get();
    }
}
