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

import net.minecraft.world.entity.Entity;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.npc.Villager;

public class VillagerCalmDown extends Behavior<Villager> {
    public VillagerCalmDown() {
        super(ImmutableMap.of());
    }
    
    @Override
    protected void start(final ServerLevel xd, final Villager axw, final long long3) {
        final boolean boolean6 = VillagerPanicTrigger.isHurt(axw) || VillagerPanicTrigger.hasHostile(axw) || isCloseToEntityThatHurtMe(axw);
        if (!boolean6) {
            axw.getBrain().<DamageSource>eraseMemory(MemoryModuleType.HURT_BY);
            axw.getBrain().<LivingEntity>eraseMemory(MemoryModuleType.HURT_BY_ENTITY);
            axw.getBrain().updateActivity(xd.getDayTime(), xd.getGameTime());
        }
    }
    
    private static boolean isCloseToEntityThatHurtMe(final Villager axw) {
        return axw.getBrain().<LivingEntity>getMemory(MemoryModuleType.HURT_BY_ENTITY).filter(akz -> akz.distanceToSqr(axw) <= 36.0).isPresent();
    }
}
