
//不同维度生物给护甲穿透和保护穿透
EntityEvents.spawned(event => {
    let entity = event.entity
    let dimension = event.entity.level.getDimension()
    if (entity.isPlayer() || !entity || !entity.isLiving()) return
    //一阶段百分之30保护穿透,百分之30护甲穿透
    if (dimension == 'minecraft:overworld') {
        if (entity.attributes.hasAttribute('attributeslib:prot_shred')) {
            entity.modifyAttribute('attributeslib:prot_shred', 'stages_attribute', 0.3, 'addition')
        }
        if (entity.attributes.hasAttribute('attributeslib:armor_shred')) {
            entity.modifyAttribute('attributeslib:armor_shred', 'stages_attribute', 0.3, 'addition')
        }
    }
    //二阶段50%保护穿透,百分之120护甲穿透
    if (dimension == 'minecraft:the_nether' ||
        dimension == 'pasterdream:dyedream_world' ||
        dimension == 'pasterdream:lamp_shadow_world' ||
        dimension == 'pasterdream:wind_journey_world' ||
        dimension == 'pasterdream:aaroncos_arena_world' ||
        dimension == 'undergarden:undergarden'
    ) {
        if (entity.attributes.hasAttribute('attributeslib:prot_shred')) {
            entity.modifyAttribute('attributeslib:prot_shred', 'stages_attribute', 0.5, 'addition')
        }
        if (entity.attributes.hasAttribute('attributeslib:armor_shred')) {
            entity.modifyAttribute('attributeslib:armor_shred', 'stages_attribute', 1.2, 'addition')
        }
    }
    //三阶段60%保护穿透,百分之140护甲穿透
    if (dimension == 'pasterdream:wind_journey_world' || dimension == 'minecraft:the_end' || dimension == "twilightforest:twilight_forest") {
        if (entity.attributes.hasAttribute('attributeslib:prot_shred')) {
            entity.modifyAttribute('attributeslib:prot_shred', 'stages_attribute', 0.6, 'addition')
        }
        if (entity.attributes.hasAttribute('attributeslib:armor_shred')) {
            entity.modifyAttribute('attributeslib:armor_shred', 'stages_attribute', 1.4, 'addition')
        }
    }



})
// 生物受伤回血系统，用来替换tick回血
NativeEvents.onEvent($LivingHurtEvent, event => {
    let entity = event.entity;
    let entityName = entity.type;


    if (!stage_boss_2_1.includes(entityName) && !stage_boss_2_2.includes(entityName) && !stage_boss_3.includes(entityName)) return


    if (!entity.hasEffect('kubejs:cd')) {
        entity.potionEffects.add('kubejs:cd', 20 * 10, 0);

        for (let i = 1; i <= 10; i++) {
            stage_health(i, entity)
        }
    }
});
function stage_health(i, entity) {
    let entityName = entity.type
    entity.getServer().scheduleInTicks(20 * i, () => {
        // 检查生物是否还存在
        if (!entity.isAlive()) return;
        // 根据生物类型确定回血量
        let healAmount = 0;
        if (entityAttributeModifiers.hasOwnProperty(entityName)) {
            healAmount = entityAttributeModifiers[entityName]
        } else if (stage_boss_2_1.includes(entityName)) {
            healAmount = 20;
        } else if (stage_boss_2_2.includes(entityName)) {
            healAmount = 30;
        } else if (stage_boss_3.includes(entityName)) {
            healAmount = 70;
        } else {
            return;
        }
        let health = entity.getHealth()
        let maxhealth = entity.getMaxHealth()

        let canHeal = true
        if (health + healAmount > maxhealth * 0.75 && health < maxhealth * 0.75) {
            canHeal = false
        }
        if (health + healAmount > maxhealth * 0.25 && health < maxhealth * 0.25) {
            canHeal = false
        }
        if (canHeal) {
            entity.heal(healAmount);
        }
    });
}

//生物生成事件
EntityEvents.spawned(event => {
    //获取生成的实体
    let entity = event.entity
    if (entity.type == 'alexscaves:magnetron') {
        //entity.setAttributeBaseValue('attributeslib:healing_received',0)
    }
})
//玩家死亡时，周围生物回血效率降低
EntityEvents.death(event => {
    let player = event.entity
    if (player.type == 'minecraft:player') {
        // 获取周围实体
        let entityList = getLivingWithinRadius(player.getLevel(), player.position(), 16)
        // 遍历实体列表
        entityList.forEach(entity => {
            if (entity.isLiving() && !entity.isPlayer() && entity.attributes.hasAttribute('attributeslib:healing_received')) {
                let amount = -100 * (entity.getMaxHealth() - entity.getHealth()) / entity.getMaxHealth()

                entity.modifyAttribute('attributeslib:healing_received', 'player_death_attribute', amount / 100, 'addition')
            }
        })
    }
})

