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

system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent("mm:deplete_durability", {
        onMineBlock(e, p) {
            const tagToBreak = p.params.tag
            var item = e.itemStack;
            const player = e.source
            const equipment = player.getComponent("minecraft:equippable")
            const block = e.minedBlockPermutation;
            var duraDamage = 0
            if (block.hasTag(tagToBreak)) {
                duraDamage = 1
            } else {
                duraDamage = 2
            }
            const durability = item.getComponent("minecraft:durability")
            if (!durability) return;

            if (durability.damage + duraDamage < durability.maxDurability) {
                durability.damage += duraDamage;
                equipment.setEquipment("Mainhand", item)
            } else {
                equipment.setEquipment("Mainhand", undefined)
                source.dimension.playSound("random.break", source.location, { volume: 1, pitch: 1 })

            }

        }
    })
})