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

import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.AgableMob;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.schedule.Activity;
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 VillagerPanicTrigger extends Behavior<Villager> {
    public VillagerPanicTrigger() {
        super(ImmutableMap.of());
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final Villager bdp, final long long3) {
        return isHurt(bdp) || hasHostile(bdp);
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        if (isHurt(bdp) || hasHostile(bdp)) {
            final Brain<?> apr6 = bdp.getBrain();
            if (!apr6.isActive(Activity.PANIC)) {
                apr6.<Path>eraseMemory(MemoryModuleType.PATH);
                apr6.<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
                apr6.<PositionTracker>eraseMemory(MemoryModuleType.LOOK_TARGET);
                apr6.<AgableMob>eraseMemory(MemoryModuleType.BREED_TARGET);
                apr6.<LivingEntity>eraseMemory(MemoryModuleType.INTERACTION_TARGET);
            }
            apr6.setActiveActivityIfPossible(Activity.PANIC);
        }
    }
    
    @Override
    protected void tick(final ServerLevel zd, final Villager bdp, final long long3) {
        if (long3 % 100L == 0L) {
            bdp.spawnGolemIfNeeded(long3, 3);
        }
    }
    
    public static boolean hasHostile(final LivingEntity aoy) {
        return aoy.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_HOSTILE);
    }
    
    public static boolean isHurt(final LivingEntity aoy) {
        return aoy.getBrain().hasMemoryValue(MemoryModuleType.HURT_BY);
    }
}
