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

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 java.util.Optional;
import net.minecraft.core.Vec3i;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.server.level.ServerLevel;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
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 it.unimi.dsi.fastutil.longs.Long2LongMap;
import net.minecraft.world.entity.LivingEntity;

public class SetClosestHomeAsWalkTarget extends Behavior<LivingEntity> {
    private final float speedModifier;
    private final Long2LongMap batchCache;
    private int triedCount;
    private long lastUpdate;
    
    public SetClosestHomeAsWalkTarget(final float float1) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<WalkTarget>)MemoryModuleType.HOME, MemoryStatus.VALUE_ABSENT));
        this.batchCache = (Long2LongMap)new Long2LongOpenHashMap();
        this.speedModifier = float1;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final LivingEntity aoy) {
        if (zd.getGameTime() - this.lastUpdate < 20L) {
            return false;
        }
        final PathfinderMob apg4 = (PathfinderMob)aoy;
        final PoiManager axz5 = zd.getPoiManager();
        final Optional<BlockPos> optional6 = axz5.findClosest(PoiType.HOME.getPredicate(), aoy.blockPosition(), 48, PoiManager.Occupancy.ANY);
        return optional6.isPresent() && optional6.get().distSqr(apg4.blockPosition()) > 4.0;
    }
    
    @Override
    protected void start(final ServerLevel zd, final LivingEntity aoy, final long long3) {
        this.triedCount = 0;
        this.lastUpdate = zd.getGameTime() + zd.getRandom().nextInt(20);
        final PathfinderMob apg6 = (PathfinderMob)aoy;
        final PoiManager axz7 = zd.getPoiManager();
        final long long4;
        final Predicate<BlockPos> predicate8 = fu -> {
            long4 = fu.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> stream9 = axz7.findAll(PoiType.HOME.getPredicate(), predicate8, aoy.blockPosition(), 48, PoiManager.Occupancy.ANY);
        final Path czf10 = apg6.getNavigation().createPath(stream9, PoiType.HOME.getValidRange());
        if (czf10 != null && czf10.canReach()) {
            final BlockPos fu2 = czf10.getTarget();
            final Optional<PoiType> optional12 = axz7.getType(fu2);
            if (optional12.isPresent()) {
                aoy.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(fu2, this.speedModifier, 1));
                DebugPackets.sendPoiTicketCountPacket(zd, fu2);
            }
        }
        else if (this.triedCount < 5) {
            this.batchCache.long2LongEntrySet().removeIf(entry -> entry.getLongValue() < this.lastUpdate);
        }
    }
}
