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

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.pathfinder.Path;
import java.util.stream.Stream;
import java.util.function.Predicate;
import net.minecraft.network.protocol.game.DebugPackets;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import net.minecraft.core.GlobalPos;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.PathfinderMob;

public class AcquirePoi extends Behavior<PathfinderMob> {
    private final PoiType poiType;
    private final MemoryModuleType<GlobalPos> memoryType;
    private final boolean onlyIfAdult;
    private long lastUpdate;
    private final Long2LongMap batchCache;
    private int triedCount;
    
    public AcquirePoi(final PoiType asu, final MemoryModuleType<GlobalPos> arl, final boolean boolean3) {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(arl, MemoryStatus.VALUE_ABSENT));
        this.batchCache = (Long2LongMap)new Long2LongOpenHashMap();
        this.poiType = asu;
        this.memoryType = arl;
        this.onlyIfAdult = boolean3;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final PathfinderMob alg) {
        return (!this.onlyIfAdult || !alg.isBaby()) && xd.getGameTime() - this.lastUpdate >= 20L;
    }
    
    @Override
    protected void start(final ServerLevel xd, final PathfinderMob alg, final long long3) {
        this.triedCount = 0;
        this.lastUpdate = xd.getGameTime() + xd.getRandom().nextInt(20);
        final PoiManager asr6 = xd.getPoiManager();
        final long long4;
        final Predicate<BlockPos> predicate7 = fk -> {
            long4 = fk.asLong();
            if (this.batchCache.containsKey(long4)) {
                return false;
            }
            else if (++this.triedCount >= 5) {
                return false;
            }
            else {
                this.batchCache.put(long4, this.lastUpdate + 40L);
                return true;
            }
        };
        final Stream<BlockPos> stream8 = asr6.findAll(this.poiType.getPredicate(), predicate7, new BlockPos(alg), 48, PoiManager.Occupancy.HAS_SPACE);
        final Path cqq9 = alg.getNavigation().createPath(stream8, this.poiType.getValidRange());
        if (cqq9 != null && cqq9.canReach()) {
            final BlockPos fk3 = cqq9.getTarget();
            final PoiManager poiManager;
            final BlockPos blockPos;
            asr6.getType(fk3).ifPresent(asu -> {
                poiManager.take(this.poiType.getPredicate(), fk2 -> fk2.equals(blockPos), blockPos, 1);
                alg.getBrain().<GlobalPos>setMemory(this.memoryType, GlobalPos.of(xd.getDimension().getType(), blockPos));
                DebugPackets.sendPoiTicketCountPacket(xd, blockPos);
            });
        }
        else if (this.triedCount < 5) {
            this.batchCache.long2LongEntrySet().removeIf(entry -> entry.getLongValue() < this.lastUpdate);
        }
    }
}
