// priority: 8
NativeEvents.onEvent($MobEffectEvent$Applicable, event => {
    let player = event.entity
    if (!player.isPlayer()) return;
    if (player.level.clientSide) return

    // 获取buff
    let effectInstance = event.getEffectInstance()
    let buff = effectInstance.getEffect().getDescriptionId();
    let effects = get_all_effect(player);
    effects.forEach(effect => {
        let strategy = all_effect_effect_immunity_strategies[effect.name] || all_effect_effect_immunity_strategies.default;
        strategy(event, player, effect.level, effect.name, buff, effect.efficiency);
    });
})
const all_effect_effect_immunity_strategies = {
    //缓慢免疫
    'slowness_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.slowness') {
            event.setResult(Event$Result.DENY)
        }
    },
    //疲劳免疫
    'mining_fatigue_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.mining_fatigue') {
            event.setResult(Event$Result.DENY)
        }
    },
    //虚弱免疫
    'weakness_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.weakness') {
            event.setResult(Event$Result.DENY)
        }
    },
    //中毒免疫
    'nausea_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.nausea') {
            event.setResult(Event$Result.DENY)
        }
    },
    //不许放逐
    'bad_omen_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.bad_omen') {
            event.setResult(Event$Result.DENY)
        }
    },
    //凋零免疫
    'wither_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.wither') {
            event.setResult(Event$Result.DENY)
        }
    },
    //中毒免疫
    'poison_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.poison') {
            event.setResult(Event$Result.DENY)
        }
    },
    //万法不侵
    'wan_fa_bu_qin_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        event.setResult(Event$Result.DENY)
    },
    //净化
    'cleanse': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (event.getEffectInstance().getEffect().getCategory() == 'HARMFUL') {
            event.getEffectInstance().setDuration(event.getEffectInstance().getDuration() * (1 - effectLevel / 100 * (1 + get_gear_shift(player) / 100)))
        }
    },
    //饥饿免疫
    'hunger_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.hunger') {
            event.setResult(Event$Result.DENY)
        }
    },
    //腐臭免疫
    'rotten_smell_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.born_in_chaos_v1.rotten_smell') {
            event.setResult(Event$Result.DENY)
        }
    },
    //幽匿锻造模板
    'silence_armor_trim_smithing_template': function (event, player, effectLevel, effectname, buff, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 1) return
        player.persistentData.putInt(effectname, player.age)

        let buff_name = buff.slice(7).replace('.', ':');
        if (event.getEffectInstance().getEffect().getCategory() == 'BENEFICIAL') {
            player.potionEffects.add(buff_name, event.getEffectInstance().getDuration(), event.getEffectInstance().getAmplifier() + 1)
        }
    },
    //时效延长
    'amethyst': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (event.getEffectInstance().getEffect().getCategory() == 'BENEFICIAL'){
            event.getEffectInstance().setDuration((event.getEffectInstance().getDuration() + efficiency * 20) * (1 + effectLevel / 100))
            
        }
    },
    //失明免疫
    'blindness_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.blindness') {
            event.setResult(Event$Result.DENY)
        }
    },
    //漂浮免疫
    'levitation_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.levitation') {
            event.setResult(Event$Result.DENY)
        }
    },
    //黑暗免疫
    'darkness_immunity': function (event, player, effectLevel, effectname, buff, efficiency) {
        if (buff == 'effect.minecraft.darkness') {
            event.setResult(Event$Result.DENY)
        }
    },
    'default': function (event, player, effectLevel, effectname, buff, efficiency) {
        return;
    }
};