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

import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.AgableMob;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.Position;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.ai.village.poi.PoiType;
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.core.BlockPos;
import java.util.Optional;
import net.minecraft.world.entity.LivingEntity;

public class LocateHidingPlace extends Behavior<LivingEntity> {
    private final float speedModifier;
    private final int radius;
    private final int closeEnoughDist;
    private Optional<BlockPos> currentPos;
    
    public LocateHidingPlace(final int integer1, final float float2, final int integer3) {
        super(ImmutableMap.<MemoryModuleType<WalkTarget>, MemoryStatus>of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<WalkTarget>)MemoryModuleType.HOME, MemoryStatus.REGISTERED, (MemoryModuleType<WalkTarget>)MemoryModuleType.HIDING_PLACE, MemoryStatus.REGISTERED));
        this.currentPos = Optional.<BlockPos>empty();
        this.radius = integer1;
        this.speedModifier = float2;
        this.closeEnoughDist = integer3;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final LivingEntity aoy) {
        final Optional<BlockPos> optional4 = zd.getPoiManager().find(ayc -> ayc == PoiType.HOME, fu -> true, aoy.blockPosition(), this.closeEnoughDist + 1, PoiManager.Occupancy.ANY);
        if (optional4.isPresent() && optional4.get().closerThan(aoy.position(), this.closeEnoughDist)) {
            this.currentPos = optional4;
        }
        else {
            this.currentPos = Optional.<BlockPos>empty();
        }
        return true;
    }
    
    @Override
    protected void start(final ServerLevel zd, final LivingEntity aoy, final long long3) {
        final Brain<?> apr6 = aoy.getBrain();
        Optional<BlockPos> optional7 = this.currentPos;
        if (!optional7.isPresent()) {
            optional7 = zd.getPoiManager().getRandom(ayc -> ayc == PoiType.HOME, fu -> true, PoiManager.Occupancy.ANY, aoy.blockPosition(), this.radius, aoy.getRandom());
            if (!optional7.isPresent()) {
                final Optional<GlobalPos> optional8 = apr6.<GlobalPos>getMemory(MemoryModuleType.HOME);
                if (optional8.isPresent()) {
                    optional7 = Optional.<BlockPos>of(optional8.get().pos());
                }
            }
        }
        if (optional7.isPresent()) {
            apr6.<Path>eraseMemory(MemoryModuleType.PATH);
            apr6.<PositionTracker>eraseMemory(MemoryModuleType.LOOK_TARGET);
            apr6.<AgableMob>eraseMemory(MemoryModuleType.BREED_TARGET);
            apr6.<LivingEntity>eraseMemory(MemoryModuleType.INTERACTION_TARGET);
            apr6.<GlobalPos>setMemory(MemoryModuleType.HIDING_PLACE, GlobalPos.of(zd.dimension(), optional7.get()));
            if (!optional7.get().closerThan(aoy.position(), this.closeEnoughDist)) {
                apr6.<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(optional7.get(), this.speedModifier, this.closeEnoughDist));
            }
        }
    }
}
