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

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('besmc_change_grabpack_rb:trigger', {
    onUse: e => { e.source.runCommand("function grapack_red_blu_giv"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:bunzo_bunny', {
    onUse: e => { e.source.runCommand("function morph_bunzo_bunny"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:pj_pug_a_pillar', {
    onUse: e => { e.source.runCommand("function morph_pj_pug_a_pillar"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:pj_pug_a_pillar_remove', {
    onUse: e => { e.source.runCommand("function morph_pj_pug_a_pillar_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:pj_pug_a_pillar_scream', {
    onUse: e => { e.source.runCommand("function morph_pj_pug_a_pillar_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:bunzo_bunny_remove', {
    onUse: e => { e.source.runCommand("function morph_bunzo_bunny_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:dogday', {
    onUse: e => { e.source.runCommand("function morph_dogday"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:dogday_remove', {
    onUse: e => { e.source.runCommand("function morph_dogday_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:dogday_scream', {
    onUse: e => { e.source.runCommand("function morph_dogday_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:bunzo_bunny_scream', {
    onUse: e => { e.source.runCommand("function morph_bunzo_bunny_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:harley_soyer', {
    onUse: e => { e.source.runCommand("function morph_harley_soyer"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:harley_soyer_remove', {
    onUse: e => { e.source.runCommand("function morph_harley_soyer_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:harley_soyer_scream', {
    onUse: e => { e.source.runCommand("function morph_harley_soyer_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:bot_harley_soyer', {
    onUse: e => { e.source.runCommand("function morph_bot_harley_soyer"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:bot_harley_soyer_remove', {
    onUse: e => { e.source.runCommand("function morph_bot_harley_soyer_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:bot_harley_soyer_scream', {
    onUse: e => { e.source.runCommand("function morph_bot_harley_soyer_scream"); },
  });
  -
    initEvent.itemComponentRegistry.registerCustomComponent('morph:miss_delight', {
      onUse: e => { e.source.runCommand("function morph_miss_delight"); },
    });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:miss_delight_remove', {
    onUse: e => { e.source.runCommand("function morph_miss_delight_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:miss_delight_scream', {
    onUse: e => { e.source.runCommand("function morph_miss_delight_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:mommy_long_legs_hallucination_remove', {
    onUse: e => { e.source.runCommand("function morph_mommy_long_legs_hallucination_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:mommy_long_legs_hallucination_scream', {
    onUse: e => { e.source.runCommand("function morph_mommy_long_legs_hallucination_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:mommy_long_legs_hallucination', {
    onUse: e => { e.source.runCommand("function morph_mommy_long_legs_hallucination"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:mommy_long_legs_remove', {
    onUse: e => { e.source.runCommand("function morph_mommy_long_legs_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:mommy_long_legs_scream', {
    onUse: e => { e.source.runCommand("function morph_mommy_long_legs_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:mommy_long_legs', {
    onUse: e => { e.source.runCommand("function morph_mommy_long_legs"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:catnap_hallucination_remove', {
    onUse: e => { e.source.runCommand("function morph_catnap_hallucination_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('mebesm:catnap_smokee', {
    onUse: e => { e.source.runCommand("function catnap_smokee"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('mebesm:catnap_smokee_kill', {
    onUse: e => { e.source.runCommand("function catnap_smokee_kill"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:catnap_hallucination_scream', {
    onUse: e => { e.source.runCommand("function morph_catnap_hallucination_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:catnap_hallucination', {
    onUse: e => { e.source.runCommand("function morph_catnap_hallucination"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:catnap_remove', {
    onUse: e => { e.source.runCommand("function morph_catnap_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:catnap_scream', {
    onUse: e => { e.source.runCommand("function morph_catnap_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:catnap', {
    onUse: e => { e.source.runCommand("function morph_catnap"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:doey', {
    onUse: e => { e.source.runCommand("function morph_doey"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:third_person', {
    onUse: e => { e.source.runCommand("function morph_third_person"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:third_person_front', {
    onUse: e => { e.source.runCommand("function morph_third_person_front"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:first_person', {
    onUse: e => { e.source.runCommand("function morph_first_person"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:doey_remove', {
    onUse: e => { e.source.runCommand("function morph_doey_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:doey_scream', {
    onUse: e => { e.source.runCommand("function morph_doey_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:monster_doey', {
    onUse: e => { e.source.runCommand("function morph_monster_doey"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:monster_doey_remove', {
    onUse: e => { e.source.runCommand("function morph_monster_doey_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:monster_doey_scream', {
    onUse: e => { e.source.runCommand("function morph_monster_doey_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:boxy_boo', {
    onUse: e => { e.source.runCommand("function morph_boxy_boo"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:boxy_boo_remove', {
    onUse: e => { e.source.runCommand("function morph_boxy_boo_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:boxy_boo_scream', {
    onUse: e => { e.source.runCommand("function morph_boxy_boo_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:box_boo', {
    onUse: e => { e.source.runCommand("function morph_box_boo"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:box_boo_remove', {
    onUse: e => { e.source.runCommand("function morph_box_boo_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:yarnaby', {
    onUse: e => { e.source.runCommand("function morph_yarnaby"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:yarnaby_remove', {
    onUse: e => { e.source.runCommand("function morph_yarnaby_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:yarnaby_scream', {
    onUse: e => { e.source.runCommand("function morph_yarnaby_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:nightmare_huggy', {
    onUse: e => { e.source.runCommand("function morph_nightmare_huggy"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:nightmare_huggy_remove', {
    onUse: e => { e.source.runCommand("function morph_nightmare_huggy_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:nightmare_huggy_scream', {
    onUse: e => { e.source.runCommand("function morph_nightmare_huggy_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:pianosaurus', {
    onUse: e => { e.source.runCommand("function morph_pianosaurus"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:pianosaurus_remove', {
    onUse: e => { e.source.runCommand("function morph_pianosaurus_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:pianosaurus_scream', {
    onUse: e => { e.source.runCommand("function morph_pianosaurus_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:huggy_wuggy', {
    onUse: e => { e.source.runCommand("function morph_huggy_wuggy"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:huggy_wuggy_remove', {
    onUse: e => { e.source.runCommand("function morph_huggy_wuggy_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:huggy_wuggy_scream', {
    onUse: e => { e.source.runCommand("function morph_huggy_wuggy_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:huggy_wuggy_statue', {
    onUse: e => { e.source.runCommand("function morph_huggy_wuggy_statue"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:huggy_wuggy_remove_statue', {
    onUse: e => { e.source.runCommand("function morph_huggy_wuggy_remove_statue"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:kissy_missy', {
    onUse: e => { e.source.runCommand("function morph_kissy_missy"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:kissy_missy_remove', {
    onUse: e => { e.source.runCommand("function morph_kissy_missy_remove"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:kissy_missy_scream', {
    onUse: e => { e.source.runCommand("function morph_kissy_missy_scream"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:kissy_missy_statue', {
    onUse: e => { e.source.runCommand("function morph_kissy_missy_statue"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('morph:kissy_missy_remove_statue', {
    onUse: e => { e.source.runCommand("function morph_kissy_missy_remove_statue"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_clear_grabpack:trigger', {
    onUse: e => { e.source.runCommand("function grapack_clears"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_change_grabpack_gb:trigger', {
    onUse: e => { e.source.runCommand("function grapack_gren_blu_giv"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_limon:trigger', {
    onUse: e => { e.source.runCommand("function Limon"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_change_grabpack_pb:trigger', {
    onUse: e => { e.source.runCommand("function grapack_purple_blu_giv"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_kill_sign:trigger', {
    onUse: e => {
      durabilityOnChanged(e.itemStack, e.source, false);
      e.source.runCommand("kill @e[c=1, type=!player]");
      e.source.runCommand("playsound mob.besmc_huggy_wuggy.throw @a ~ ~ ~ 1 1.2");
    },

    onHitEntity: e => { durabilityOnChanged(e.itemStack, e.source, true); e.attackingEntity.runCommand("playsound mob.besmc_huggy_wuggy.throw @a[r=10] ~ ~ ~ 1 1"); },
    onMineBlock: e => { durabilityOnChanged(e.itemStack, e.source, false); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_close_door:trigger', {
    onUse: e => { e.source.runCommand("function close_door"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_change_grabpack_ob:trigger', {
    onUse: e => { e.source.runCommand("function grapack_orange_blu_giv"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_change_grabpack_omb:trigger', {
    onUse: e => { e.source.runCommand("function grapack_omni_blu_giv"); },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_blue_p:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_blue:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_red_and_blue:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_purple_and_blue:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_omni:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_red:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_omni_and_blue:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_purple:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_blue_om:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_gren:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_blue_o:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_blue_g:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_blue_r:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_gren_active:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_gren_and_blue:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent('besmc_grabpack_orange:trigger', {
    onUse: e => {
      const equippable = e.source.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem()) return;
      mainhand.amount++;
    },
  });
});
