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

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.core.SerializableLong;
import net.minecraft.core.Position;
import java.util.Objects;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import net.minecraft.core.GlobalPos;
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.world.entity.npc.Villager;

public class WorkAtPoi extends Behavior<Villager> {
    private long lastCheck;
    
    public WorkAtPoi() {
        super(ImmutableMap.<MemoryModuleType<GlobalPos>, MemoryStatus>of(MemoryModuleType.JOB_SITE, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<GlobalPos>)MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED));
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel xd, final Villager axw) {
        if (xd.getGameTime() - this.lastCheck < 300L) {
            return false;
        }
        if (xd.random.nextInt(2) != 0) {
            return false;
        }
        this.lastCheck = xd.getGameTime();
        final GlobalPos fr4 = axw.getBrain().<GlobalPos>getMemory(MemoryModuleType.JOB_SITE).get();
        return Objects.equals(fr4.dimension(), xd.getDimension().getType()) && fr4.pos().closerThan(axw.position(), 1.73);
    }
    
    @Override
    protected void start(final ServerLevel xd, final Villager axw, final long long3) {
        final Brain<Villager> alp6 = axw.getBrain();
        alp6.<SerializableLong>setMemory(MemoryModuleType.LAST_WORKED_AT_POI, SerializableLong.of(long3));
        alp6.<GlobalPos>getMemory(MemoryModuleType.JOB_SITE).ifPresent(fr -> alp6.<BlockPosWrapper>setMemory((MemoryModuleType<BlockPosWrapper>)MemoryModuleType.LOOK_TARGET, new BlockPosWrapper(fr.pos())));
        axw.playWorkSound();
        if (axw.shouldRestock()) {
            axw.restock();
        }
    }
}
