import * as mc from "@minecraft/server";
const { world } = mc;

world.afterEvents.worldLoad.subscribe(() => {
    world.afterEvents.itemCompleteUse.subscribe(result => {

        let itemStack = result.itemStack;

        let items = [
            { effect: "regeneration", duration: 5, amplifier: 0, itemTypeId: "largemeals:hearty_lunch" },
            { effect: "regeneration", duration: 5, amplifier: 0, itemTypeId: "largemeals:rice_pudding" },
        ];
        for (let item of items) {
            if (itemStack.typeId === item.itemTypeId) {
                let effect = item.effect;
                let duration = item.duration;
                let amplifier = item.amplifier;

                if (item.weight) {
                    let weight = item.weight;
                    let random = Math.random() * 100;
                    if (random < weight) {
                        result.source.addEffect(effect, duration * 20, { amplifier: amplifier });
                    }
                } else {
                    if (!item.remove) result.source.addEffect(effect, duration * 20, { amplifier: amplifier });
                }

                if (item.remove) {
                    item.effect.forEach(effect => {
                        result.source.removeEffect(effect);
                    })
                }
            }
        }
    });
});
