        import { world, system } from "@minecraft/server";
            
            export class ItemManager {
                static init(){
                    function durabilityOnChanged(item, player, isHitEntity = false) {
    let level = item.getComponent("minecraft:enchantable")?.getEnchantment("unbreaking")?.level;

    function durability() {
        let durability = item.getComponent("minecraft:durability");

        const t = Math.floor(Math.random() * 100);

        if (t < durability.getDamageChance()) {
            if (!isHitEntity) durability.damage += 1;
            if (durability.damage >= durability.maxDurability) {
                player.playSound("random.break");
                if (!isHitEntity) player.getComponent('equippable').setEquipment('Mainhand', undefined)
            } else {
                if (!isHitEntity) player.getComponent('equippable').setEquipment('Mainhand', item)
            }
        } else {
            if (!isHitEntity) return;
            durability.damage -= 1;
            if (!isHitEntity) player.getComponent('equippable').setEquipment('Mainhand', item);
        }
    }

    const t = Math.floor(Math.random() * 10)
    if (level === 1 && t > 8) return;
    else if (level === 2 && t > 6) return;
    else if (level === 3 && t > 4) return;
    else durability();
}

                    world.beforeEvents.worldInitialize.subscribe(initEvent => { initEvent.itemComponentRegistry.registerCustomComponent('pa_macahuitl_chalchiuh:trigger', {
  onUse: e => { durabilityOnChanged(e.itemStack, e.source, false);
e.source.runCommand("function Golpe_de_rayo"); },
  
  onHitEntity: e => { durabilityOnChanged(e.itemStack, e.source, true); e.attackingEntity.runCommand("function macahuitl_de_jade_p1"); },
  onMineBlock: e => { durabilityOnChanged(e.itemStack, e.source, false); },
});
 });
                }   
            }
        