// priority: 8
// 生物死亡事件
NativeEvents.onEvent($LivingHurtEvent, event => {
    if (event.entity && event.source.actual) {
        if (event.entity.isPlayer()) {
            let player = event.entity
            let effects = get_all_effect(player);

            effects.forEach(effect => {
                let strategy = all_effect_hurt_strategies[effect.name] || all_effect_hurt_strategies.default;
                strategy(event, player, effect.level, effect.name, effect.efficiency);
            });
        }
    }
})
const all_effect_hurt_strategies = {
    //自主防御
    'autonomous_defense': function (event, player, effectLevel, effectname, efficiency) {
        if(Math.random() < effectLevel / 100){
            let $Utils = Java.loadClass('io.redspace.ironsspellbooks.api.util.Utils')
            $Utils.serverSideInitiateQuickCast(player, 1);
        }
    },
    //冰糖糖衣
    'rock_candy_icing': function (event, player, effectLevel, effectname, efficiency) {
        let ying_yang_count = player.persistentData.getInt(YingYangCount)
        if (ying_yang_count < 30) return
        let random = Math.random()

        let buff_count = 0
        player.potionEffects.active.forEach(ctx => {
            if (ctx.effect.getCategory() == 'BENEFICIAL') {
                buff_count++
            }
        });

        if (effectLevel / 100 - buff_count * 2 / 100 > random) {
            let random_effect = MAODLEFFECTS_BENEFICIAL[Math.floor(Math.random() * MAODLEFFECTS_BENEFICIAL.length)]
            event.entity.potionEffects.add(random_effect, 20 * 30, Math.floor(Math.random * 2))
        }
    },
    //反伤
    'dzzl_counter_injury': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        let amount = event.amount
        simpleAttackEntity(true, null, entity, 'common', amount * effectLevel / 100)
    },
    //魔法抵抗
    'magic_resistence': function (event, player, effectLevel, effectname, efficiency) {
        let type = event.getSource().getType().toString();
        if (type == 'magic') {
            event.amount = event.amount * (1 - effectLevel / 100)
        }
    },
    'default': function (event, player, effectLevel, effectname, efficiency) {
        return;
    }
};