// priority: 9
// 生物死亡事件
//参考脆骨症夜鸢大佬的代码
//不清楚优化会不会更好，但是写起来方便一点
function deathevent(event) {
    let player = event.source.player;
    if (!player) return;

    let item = player.getMainHandItem();
    if (!item.item instanceof $ModularItem) return;

    let effects = getAllEffects(item);
    effects.forEach(effect => {
        let strategy = tetraDeathStrategies[effect.name] || tetraDeathStrategies.default;
        strategy(event, player, effect.level, item, effect.name, effect.efficiency);
    });
};
const tetraDeathStrategies = {
    //修格斯
    'siggs': function (event, player, effectLevel, item, effectname, efficiency) {
        if (Math.random() > 0.01 * effectLevel) return
        if (player.hasEffect('kubejs:ju_hua')) {
            // 获取已有的效果
            let effect = player.getEffect('kubejs:ju_hua')
            // 获取效果等级
            let amplifier = effect.getAmplifier()
            player.tell(amplifier)
            if (amplifier < effectLevel - 1) {
                player.potionEffects.add('kubejs:ju_hua', 20 * 10, amplifier + 1)
            }
        } else {
            player.potionEffects.add('kubejs:ju_hua', 20 * 10, 0)
        }
    },
    //击杀实体回血
    'forbidden_treatment': function (event, player, effectLevel, item, effectname, efficiency) {
        let health = player.health
        player.setHealth(health + effectLevel);
    },
    //击杀实体，增加冷却
    'cooling_add': function (event, player, effectLevel, item, effectname, efficiency) {
        let offitem = player.getOffHandItem();
        offitem.addCooldown(2)
        player.getOffHandItem().addCooldown(2)
    },
    //击杀实体，清除主手，副手物品冷却
    'cooling_remove': function (event, player, effectLevel, item, effectname, efficiency) {
        let offitem = player.getOffHandItem();
        player.getServer().scheduleInTicks(2, () => {
            player.addItemCooldown(item, 0)
            player.addItemCooldown(offitem, 0)
        })
    },
    //招魂球效果
    'orbofthe_summoner': function (event, player, effectLevel, item, effectname, efficiency) {
        player.give(Item.of('born_in_chaos_v1:ethereal_spirit').withCount(1));
    },
    'default': function (event, player, effectLevel, item, effectname, efficiency) {
        return
    }
}