import { system, world } from "@minecraft/server";

system.beforeEvents.startup.subscribe(initEvent => {
    initEvent.itemComponentRegistry.registerCustomComponent('rustic:milk_on_consume', {
      onConsume: e =>  {
        const allEfects = e.source.getEffects()
        const player = e.source

        if (!allEfects) return;

        const randomEffect = Math.floor(Math.random() * allEfects.length);

        const inebriationProperty = player.getDynamicProperty("rustic:inebriation")
        
        if ((allEfects[randomEffect].typeId == "nausea" || allEfects[randomEffect].typeId == "slowness" || allEfects[randomEffect].typeId == "blindness") && inebriationProperty > 2) return;
        if (allEfects[randomEffect].typeId == "saturation"){
            const playerDisplay = entity.onScreenDisplay
            const worldScoreboard = world.scoreboard
            const fullStomachProperty = entity.getDynamicProperty("rustic:full_stomach")
            const fullStomachObjective = worldScoreboard.getObjective("full_stomach")
            if (fullStomachProperty != 0){
                player.setDynamicProperty("rustic:full_stomach",0)
                fullStomachObjective.setScore(player,0)
                playerDisplay.setTitle("empty")
            }
        }
        player.removeEffect(allEfects[randomEffect].typeId)
      }
    });
});