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



system.beforeEvents.watchdogTerminate.subscribe(data => data.cancel = true);

function getScore(player, objective) {
  try {
    const s = world.scoreboard.getObjective(objective);
    if (!s) world.scoreboard.addObjective(objective, objective);
    return s.getScore(player.scoreboardIdentity);
  } catch {
    return 0;
  }
 }

world.afterEvents.itemUse.subscribe(ev => {
    for (const player of world.getPlayers()){
     
      let inv = player.getComponent( 'inventory' ).container;
      const itemStack = inv.getItem(player.selectedSlot);
      if (itemStack?.typeId === 'edx:copper_spear_i') {
        
        player.runCommand(`scoreboard players set @s Durability_copper ${itemStack?.getComponent("durability").damage}`);
        player.removeTag("copper_spear");
      };
      if (itemStack?.typeId === 'edx:tin_spear_i') {
        
        player.runCommand(`scoreboard players set @s Durability_tin ${itemStack?.getComponent("durability").damage}`);
        player.removeTag("tin_spear");
      };
      if (itemStack?.typeId === 'edx:iron_spear_i') {
       
        player.runCommand(`scoreboard players set @s Durability_iron ${itemStack?.getComponent("durability").damage}`);
        player.removeTag("iron_spear");
      };
      if (itemStack?.typeId === 'edx:platinium_spear_i') {
        
        player.runCommand(`scoreboard players set @s Durability_platinium ${itemStack?.getComponent("durability").damage}`);
        player.removeTag("platinium_spear");
      };
      if (itemStack?.typeId === 'edx:crimson_nickel_spear_i') {
        
        player.runCommand(`scoreboard players set @s Durability_cnickel ${itemStack?.getComponent("durability").damage}`);
        player.removeTag("cnickel_spear");
      };
      if (itemStack?.typeId === 'edx:warped_nickel_spear_i') {
        
        player.runCommand(`scoreboard players set @s Durability_wnickel ${itemStack?.getComponent("durability").damage}`);
        player.removeTag("wnickel_spear");
      };
    }




    
   
  })

  world.afterEvents.itemReleaseUse.subscribe(ev => {
    for (const player of world.getPlayers()){
      player.runCommand(`scoreboard players operation @e[r=4,c=1,family=spear,type=edx:copper_spear] Durability_copper = @s Durability_copper`)
      player.runCommand(`scoreboard players operation @e[r=4,c=1,family=spear,type=edx:tin_spear] Durability_tin = @s Durability_tin`)
      player.runCommand(`scoreboard players operation @e[r=4,c=1,family=spear,type=edx:iron_spear] Durability_iron = @s Durability_iron`)
      player.runCommand(`scoreboard players operation @e[r=4,c=1,family=spear,type=edx:platinium_spear] Durability_platinium = @s Durability_platinium`)
      player.runCommand(`scoreboard players operation @e[r=4,c=1,family=spear,type=edx:crimson_nickel_spear] Durability_cnickel = @s Durability_cnickel`)
      player.runCommand(`scoreboard players operation @e[r=4,c=1,family=spear,type=edx:warped_nickel_spear] Durability_wnickel = @s Durability_wnickel`)
      
    }
  })
  


  let e = system.runInterval(data => {
    for (const player of world.getPlayers()){
      var container = player.getComponent('inventory').container;
      var newItem =  new ItemStack(ItemTypes?.get("edx:copper_spear_i"));
      var newItem2 =  new ItemStack(ItemTypes?.get("edx:tin_spear_i"));
      var newItem3 =  new ItemStack(ItemTypes?.get("edx:iron_spear_i"));
      var newItem4 =  new ItemStack(ItemTypes?.get("edx:platinium_spear_i"));
      var newItem5 =  new ItemStack(ItemTypes?.get("edx:crimson_nickel_spear_i"));
      var newItem6 =  new ItemStack(ItemTypes?.get("edx:warped_nickel_spear_i"));




    if (player.hasTag("copper_spear") &&  getScore( player, 'Durability_copper') < 50 ) {
      player.removeTag("copper_spear")
      var durabilitycopper = getScore( player, 'Durability_copper') + 1;
      newItem.getComponent("durability").damage =  durabilitycopper;
      container.addItem( newItem);
    }
    if (player.hasTag("tin_spear") &&  getScore( player, 'Durability_tin') < 75 ) {
      player.removeTag("tin_spear")
      var durabilitytin = getScore( player, 'Durability_tin') + 1;
      newItem2.getComponent("durability").damage =  durabilitytin;
      container.addItem( newItem2);
    }
    if (player.hasTag("iron_spear") &&  getScore( player, 'Durability_iron') < 100 ) {
      player.removeTag("iron_spear")
      var durabilityiron = getScore( player, 'Durability_iron') + 1;
      newItem3.getComponent("durability").damage =  durabilityiron;
      container.addItem( newItem3);
    }
    if (player.hasTag("platinium_spear") &&  getScore( player, 'Durability_platinium') < 150 ) {
      player.removeTag("platinium_spear")
      var durabilityplatinium = getScore( player, 'Durability_platinium') + 1;
      newItem4.getComponent("durability").damage =  durabilityplatinium;
      container.addItem( newItem4);
    }
    if (player.hasTag("cnickel_spear") &&  getScore( player, 'Durability_cnickel') < 200 ) {
      player.removeTag("cnickel_spear")
      var durabilitycnickel = getScore( player, 'Durability_cnickel') + 1;
      newItem5.getComponent("durability").damage =  durabilitycnickel;
      container.addItem( newItem5);
    }
    if (player.hasTag("wnickel_spear") &&  getScore( player, 'Durability_wnickel') < 200 ) {
      player.removeTag("wnickel_spear")
      var durabilitywnickel = getScore( player, 'Durability_wnickel') + 1;
      newItem6.getComponent("durability").damage =  durabilitywnickel;
      container.addItem( newItem6);
    }
    if (player.hasTag("copper_spear") &&  getScore( player, 'Durability_copper') == 50 ) {
      player.removeTag("copper_spear")
      player.runCommand(`give @s edx:broken_copper_spear`);
      player.runCommand(`playsound random.break @s`);
    }
    if (player.hasTag("tin_spear") &&  getScore( player, 'Durability_tin') == 75 ) {
      player.removeTag("tin_spear")
      player.runCommand(`give @s edx:broken_tin_spear`);
      player.runCommand(`playsound random.break @s`);
    }
    if (player.hasTag("iron_spear") &&  getScore( player, 'Durability_iron') == 100 ) {
      player.removeTag("iron_spear")
      player.runCommand(`give @s edx:broken_iron_spear`);
      player.runCommand(`playsound random.break @s`);
    }
    if (player.hasTag("platinium_spear") &&  getScore( player, 'Durability_platinium') == 150 ) {
      player.removeTag("platinium_spear")
      player.runCommand(`give @s edx:broken_platinium_spear`);
      player.runCommand(`playsound random.break @s`);
    }
    if (player.hasTag("cnickel_spear") &&  getScore( player, 'Durability_cnickel') == 200 ) {
      player.removeTag("cnickel_spear")
      player.runCommand(`give @s edx:broken_crimson_nickel_spear`);
      player.runCommand(`playsound random.break @s`);
    }
    if (player.hasTag("wnickel_spear") &&  getScore( player, 'Durability_wnickel') == 200 ) {
      player.removeTag("wnickel_spear")
      player.runCommand(`give @s edx:broken_warped_nickel_spear`);
      player.runCommand(`playsound random.break @s`);
    }
  }
}
)










//world.afterEvents.itemUse.subscribe(ev => {
 //     var copper = "edx:broken_copper_spear"
  //    var tin = "edx:broken_tin_spear"
  //     var iron = "edx:broken_iron_spear"
  //     var platinium = "edx:broken_platinium_spear"
  //     var cnickel = "edx:broken_crimson_nickel_spear"
  //     var wnickel = "edx:broken_warped_nickel_spear"
  //   for (const player of world.getPlayers()){
     
  //     let inv = player.getComponent( 'inventory' ).container
  //     const itemStack = inv.getItem(player.selectedSlot);
  //     if (itemStack?.typeId === 'edx:copper_spear_i') {
  //       var container = null
  //       var oldItem = null
  //     var newItem = null
  //       var container = player.getComponent('inventory').container
  //       var newItem =  new ItemStack(ItemTypes?.get("edx:copper_spear_i"));
  //       var oldItem = container?.getItem(player.selectedSlot)
  //       player.removeTag("copper_spear")
  //     }
  //     if (itemStack?.typeId === 'edx:tin_spear_i') {
  //       var container = null
  //       var oldItem2 = null
  //     var newItem2 = null
  //       var container = player.getComponent('inventory').container
  //       var newItem2 =  new ItemStack(ItemTypes?.get("edx:tin_spear_i"));
  //       var oldItem2 = container?.getItem(player.selectedSlot)
  //       player.removeTag("tin_spear")
  //     }
  //     if (itemStack?.typeId === 'edx:iron_spear_i') {
  //       var container = null
  //       var oldItem3 = null
  //     var newItem3 = null
  //       var container = player.getComponent('inventory').container
  //       var newItem3 =  new ItemStack(ItemTypes?.get("edx:iron_spear_i"));
  //       var oldItem3 = container?.getItem(player.selectedSlot)
  //       player.removeTag("iron_spear")
  //     }
  //     if (itemStack?.typeId === 'edx:platinium_spear_i') {
  //       var container = null
  //       var oldItem4 = null
  //     var newItem4 = null
  //       var container = player.getComponent('inventory').container
  //       var newItem4 =  new ItemStack(ItemTypes?.get("edx:platinium_spear_i"));
  //       var oldItem4 = container?.getItem(player.selectedSlot)
  //       player.removeTag("platinium_spear")
  //     }
  //     if (itemStack?.typeId === 'edx:crimson_nickel_spear_i') {
  //       var container = null
  //       var oldItem5 = null
  //     var newItem5 = null
  //       var container = player.getComponent('inventory').container
  //       var newItem5 =  new ItemStack(ItemTypes?.get("edx:crimson_nickel_spear_i"));
  //       var oldItem5 = container?.getItem(player.selectedSlot)
  //       player.removeTag("crimson_nickel_spear")
  //     }
  //     if (itemStack?.typeId === 'edx:warped_nickel_spear_i') {
  //       var container = null
  //       var oldItem6 = null
  //     var newItem6 = null
  //       var container = player.getComponent('inventory').container
  //       var newItem6 =  new ItemStack(ItemTypes?.get("edx:warped_nickel_spear_i"));
  //       var oldItem6 = container?.getItem(player.selectedSlot) 
  //       player.removeTag("warped_nickel_spear")
  //     }

  //     let e = system.runInterval(data => {
            
  //           if (player.hasTag("copper_spear") && itemStack?.typeId === 'edx:copper_spear_i' && itemStack?.getComponent("durability").damage < 50 ) {
  //             player.removeTag("copper_spear")
  //             newItem.getComponent("durability").damage = oldItem.getComponent("durability").damage + 1;
  //             container.setItem(player.selectedSlot, newItem);
  //             player.runCommand(`say ${itemStack?.getComponent("durability").damage}`)
  //             if (!player.hasTag("copper_spear")) {
  //                 system.clearRun(e)
  //           }
  //           };
  //           if (player.hasTag("tin_spear") && itemStack?.typeId === 'edx:tin_spear_i' && itemStack?.getComponent("durability").damage < 75  ) {
  //             player.removeTag("tin_spear")
  //             newItem2.getComponent("durability").damage = oldItem2.getComponent("durability").damage + 1;
  //             container.setItem(player.selectedSlot, newItem2);
  //             player.runCommand(`say ${itemStack?.getComponent("durability").damage}`)
  //             if (!player.hasTag("tin_spear")) {
  //                 system.clearRun(e)
  //           }
  //           };
  //           if (player.hasTag("iron_spear") && itemStack?.typeId === 'edx:iron_spear_i' && itemStack?.getComponent("durability").damage < 100  ) {
  //             player.removeTag("iron_spear")
  //             newItem3.getComponent("durability").damage = oldItem3.getComponent("durability").damage + 1;
  //             container.setItem(player.selectedSlot, newItem3);
  //             player.runCommand(`say ${itemStack?.getComponent("durability").damage}`)
  //             if (!player.hasTag("iron_spear")) {
  //                 system.clearRun(e)
  //           }
  //           };
  //           if (player.hasTag("platinium_spear") && itemStack?.typeId === 'edx:platinium_spear_i' && itemStack?.getComponent("durability").damage < 150  ) {
  //             player.removeTag("platinium_spear")
  //             newItem4.getComponent("durability").damage = oldItem4.getComponent("durability").damage + 1;
  //             container.setItem(player.selectedSlot, newItem4);
  //             player.runCommand(`say ${itemStack?.getComponent("durability").damage}`)
  //             if (!player.hasTag("platinium_spear")) {
  //                 system.clearRun(e)
  //           }
  //           };
  //           if (player.hasTag("crimson_nickel_spear") && itemStack?.typeId === 'edx:crimson_nickel_spear_i' && itemStack?.getComponent("durability").damage < 200  ) {
  //             player.removeTag("crimson_nickel_spear")
  //             newItem5.getComponent("durability").damage = oldItem5.getComponent("durability").damage + 1;
  //             container.setItem(player.selectedSlot, newItem5);
  //             player.runCommand(`say ${itemStack?.getComponent("durability").damage}`)
  //             if (!player.hasTag("crimson_nickel_spear")) {
  //                 system.clearRun(e)
  //           }
  //           };
  //           if (player.hasTag("warped_nickel_spear") && itemStack?.typeId === 'edx:warped_nickel_spear_i' && itemStack?.getComponent("durability").damage < 200 ) {
  //             player.removeTag("warped_nickel_spear")
  //             newItem6.getComponent("durability").damage = oldItem6.getComponent("durability").damage + 1;
  //             container.setItem(player.selectedSlot, newItem6);
  //             player.runCommand(`say ${itemStack?.getComponent("durability").damage}`)
  //             if (!player.hasTag("warped_nickel_spear")) {
  //                 system.clearRun(e)
  //           }
  //         };
  //       }
  //       )

  //     let a = system.runInterval(data => {
  //     if (player.hasTag("copper_spear") && itemStack?.typeId === 'edx:copper_spear_i' && itemStack?.getComponent("durability").damage == 50 ) {
  //       player.removeTag("copper_spear")
  //       container.setItem(player.selectedSlot, copper);
  //       if (!player.hasTag("copper_spear")) {
  //           system.clearRun(a)
  //     }
  //     };
  //     if (player.hasTag("tin_spear") && itemStack?.typeId === 'edx:tin_spear_i' && itemStack?.getComponent("durability").damage == 75  ) {
  //       player.removeTag("tin_spear")
  //       container.setItem(player.selectedSlot, tin);
  //       if (!player.hasTag("tin_spear")) {
  //           system.clearRun(a)
  //     }
  //     };
  //     if (player.hasTag("iron_spear") && itemStack?.typeId === 'edx:iron_spear_i' && itemStack?.getComponent("durability").damage == 100  ) {
  //       player.removeTag("iron_spear")
  //       container.setItem(player.selectedSlot, iron);
  //       if (!player.hasTag("iron_spear")) {
  //           system.clearRun(a)
  //     }
  //     };
  //     if (player.hasTag("platinium_spear") && itemStack?.typeId === 'edx:platinium_spear_i' && itemStack?.getComponent("durability").damage == 150  ) {
  //       player.removeTag("platinium_spear")
  //       container.setItem(player.selectedSlot, platinium);
  //       if (!player.hasTag("platinium_spear")) {
  //           system.clearRun(a)
  //     }
  //     };
  //     if (player.hasTag("crimson_nickel_spear") && itemStack?.typeId === 'edx:crimson_nickel_spear_i' && itemStack?.getComponent("durability").damage == 200  ) {
  //       player.removeTag("crimson_nickel_spear")
  //       container.setItem(player.selectedSlot, cnickel);
  //       if (!player.hasTag("crimson_nickel_spear")) {
  //           system.clearRun(a)
  //     }
  //     };
  //     if (player.hasTag("warped_nickel_spear") && itemStack?.typeId === 'edx:warped_nickel_spear_i' && itemStack?.getComponent("durability").damage == 200 ) {
  //       player.removeTag("warped_nickel_spear")
  //       container.setItem(player.selectedSlot, wnickel);
  //       if (!player.hasTag("warped_nickel_spear")) {
  //           system.clearRun(a)
  //     }
  //   };
  //   })
  
  
  
  // }



  //   })
