import { system } from "@minecraft/server"

system.beforeEvents.startup.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent("largemeals:roasted_mutton_rack0", {
        onPlayerInteract: result => {
            let player = result.player
            let item = player.getComponent("minecraft:equippable").getEquipment("Mainhand")

            //Если предмет в руке недействителен (нет предмета), код ниже не выполняется.
            if (!item) return

            //query.get_equipped_item_name('main_hand') == 'bowl'
            if (item.typeId == "minecraft:bowl") {

                // "set_block": {
                //     "block_type": "brewinandchewin:fiery_fondue_pot_level1"
                // },
                result.block.setType("largemeals:roasted_mutton_rack1")

                // "spawn_loot": {
                // 	"table": "loot_tables/fiery_fondue.json"
                // },
                result.dimension.runCommand(`loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "rack"`)

                //decrement_stack
                if (item.amount == 1)
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined)
                else {
                    item.amount--
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item)
                }
            }
        }
    })
    initEvent.blockComponentRegistry.registerCustomComponent("largemeals:roasted_mutton_rack1", {
        //Делает то же самое, что и max:bowl.
        onPlayerInteract: result => {
            let player = result.player
            let item = player.getComponent("minecraft:equippable").getEquipment("Mainhand")

            //Если предмет в руке недействителен (нет предмета), код ниже не выполняется.
            if (!item) return

            //query.get_equipped_item_name('main_hand') == 'bowl'
            if (item.typeId == "minecraft:bowl") {

                // "set_block": {
                //     "block_type": "brewinandchewin:fiery_fondue_pot_level2"
                // },
                result.block.setType("largemeals:roasted_mutton_rack2")

                // "spawn_loot": {
                // 	"table": "loot_tables/fiery_fondue.json"
                // },
                result.dimension.runCommand(`loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "rack"`)

                //decrement_stack
                if (item.amount == 1)
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined)
                else {
                    item.amount--
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item)
                }
            }
        }
    })
    initEvent.blockComponentRegistry.registerCustomComponent("largemeals:roasted_mutton_rack2", {
        //Делает то же самое, что и max:bowl.
        onPlayerInteract: result => {
            let player = result.player
            let item = player.getComponent("minecraft:equippable").getEquipment("Mainhand")

            //Если предмет в руке недействителен (нет предмета), код ниже не выполняется.
            if (!item) return

            //query.get_equipped_item_name('main_hand') == 'bowl'
            if (item.typeId == "minecraft:bowl") {

                // "set_block": {
                //     "block_type": "minecraft:cauldron"
                // },
                result.block.setType("largemeals:roasted_mutton_rack3")

                // "spawn_loot": {
                // 	"table": "loot_tables/fiery_fondue.json"
                // },
                result.dimension.runCommand(`loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "rack"`)

                //decrement_stack
                if (item.amount == 1)
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined)
                else {
                    item.amount--
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item)
                }
            }
        }
    })
    initEvent.blockComponentRegistry.registerCustomComponent("largemeals:roasted_mutton_rack3", {
        //Делает то же самое, что и max:bowl.
        onPlayerInteract: result => {
            let player = result.player
            let item = player.getComponent("minecraft:equippable").getEquipment("Mainhand")

            //Если предмет в руке недействителен (нет предмета), код ниже не выполняется.
            if (!item) return

            //query.get_equipped_item_name('main_hand') == 'bowl'
            if (item.typeId == "minecraft:bowl") {

                // "set_block": {
                //     "block_type": "minecraft:cauldron"
                // },
                result.block.setType("largemeals:plate_rack")

                // "spawn_loot": {
                // 	"table": "loot_tables/fiery_fondue.json"
                // },
                result.dimension.runCommand(`loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "rack"`)

                //decrement_stack
                if (item.amount == 1)
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined)
                else {
                    item.amount--
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item)
                }
            }
          }
        })
        initEvent.blockComponentRegistry.registerCustomComponent("largemeals:plate_rack", {
            onPlayerInteract: result => {
                let player = result.player;
                result.dimension.runCommand(`loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "plate"`)
                result.block.setType("minecraft:air");
            }
        })
})
