
import { world, Player, system, ItemStack, MinecraftBlockTypes } from "@minecraft/server";
import { ActionFormData, ModalFormData, MessageFormData } from '@minecraft/server-ui';
console.warn("ff")

/**
 * Represents a container for storing items and equipment.
 */



system.runInterval(() => {
  for (let player of world.getPlayers()) {
    if (!player.hasTag("doben")) continue
    try {
      let inventory = player.getComponent("inventory").container
      let item = inventory.getItem(player.selectedSlot)
      let durability = item.getComponent("durability")
      if (durability.damage + 1 < durability.maxDurability) {
        durability.damage += 1
      } else {
        player.playSound("random.break", player.location)
        item = undefined
      }
      player.removeTag("doben")
      inventory.setItem(player.selectedSlot, item)
    }
    catch { }
  }


})

world.afterEvents.entityHitEntity.subscribe(({
  hitEntity, damagingEntity: player
}) => {
  if (!(player instanceof Player)) return;

  system.run(() => {
    const hand_item = player.getComponent("minecraft:inventory").container.getItem(player.selectedSlot)?.typeId;
    if (!hand_item) return;
    let d = ffff[hand_item]
    if (d) for (let cmd of d) {
      try {
        hitEntity.runCommandAsync(cmd);
      } catch { }
    }
  })
});
const ffff = {

  "iaf:katana_dragon": [

    "damage @s[family=undead] 7"
  ],
  "iaf:scythe_wooden": [

    "damage @s[family=illager] 5"
  ],

  "iaf:scythe_stone": [

    "damage @s[family=illager] 5"
  ],

  "iaf:scythe_iron": [

    "damage @s[family=illager] 5"
  ],

  "iaf:scythe_netherite": [

    "damage @s[family=illager] 5"
  ],

  "iaf:scythe_gold": [

    "damage @s[family=illager] 5"
  ],

  "iaf:scythe_diamond": [

    "damage @s[family=illager] 5"
  ],

  "iaf:katana_wooden": [

    "damage @s[family=undead] 7"
  ],

  "iaf:katana_stone": [

    "damage @s[family=undead] 7"
  ],

  "iaf:katana_iron": [

    "damage @s[family=undead] 7"
  ],

  "iaf:katana_netherite": [

    "damage @s[family=undead] 7"
  ],

  "iaf:katana_gold": [

    "damage @s[family=undead] 7"
  ],

  "iaf:katana_diamond": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_dragon": [
    "damage @s[family=undead] 7"
  ],

  "iaf:saber_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonio] 5",
  ],
  "iaf:saber_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonio] 5",
  ],

  "iaf:saber_wooden": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_stone": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_iron": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_netherite": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_gold": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_diamond": [

    "damage @s[family=undead] 7"
  ],





  "iaf:mace_wooden": [

    "damage @s[family=undead] 7"
  ],

  "iaf:mace_stone": [

    "damage @s[family=undead] 7"
  ],

  "iaf:mace_iron": [

    "damage @s[family=undead] 7"
  ],

  "iaf:mace_netherite": [

    "damage @s[family=undead] 7"
  ],

  "iaf:mace_gold": [

    "damage @s[family=undead] 7"
  ],

  "iaf:mace_diamond": [

    "damage @s[family=undead] 7"
  ],

  "iaf:saber_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:saber_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:saber_lightning_dragon": [
    "summon lightning_bolt",
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 5",
    "damage @s[family=undead] 7",
  ],
  "iaf:saber_dragonsteel_lightning": [
    "summon lightning_bolt",
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 5",
    "damage @s[family=undead] 7",
  ],

  "iaf:mace_dragon": [
    "damage @s[family=undead] 8", "playsound iaf.axe @a[r=8]",
    "effect @s[family=undead] slowness 1 1 true"
  ],

  "iaf:mace_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~", "playsound iaf.axe @a[r=8]",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[family=undead] 8",
    "effect @s[family=undead] slowness 1 1 true",
    "damage @s[type=iaf:dragonio] 5"
  ],
  "iaf:mace_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~", "playsound iaf.axe @a[r=8]",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[family=undead] 8",
    "effect @s[family=undead] slowness 1 1 true",
    "damage @s[type=iaf:dragonio] 5"
  ],
  "iaf:mace_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255", "playsound iaf.axe @a[r=8]",
    "damage @s[family=undead] 8",
    "effect @s[family=undead] slowness 1 1 true",
    "damage @s[type=iaf:dragonfo] 5"
  ],
  "iaf:mace_dragonsteel_ice": [
    "summon iaf:icesw", "playsound iaf.axe @a[r=8]",
    "effect @s slowness 5 255",
    "damage @s[family=undead] 8",
    "effect @s[family=undead] slowness 1 1 true",
    "damage @s[type=iaf:dragonfo] 5"
  ],
  "iaf:mace_lightning_dragon": [
    "summon lightning_bolt",
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 6", "playsound iaf.axe @a[r=8]",
    "damage @s[family=undead] 7"
  ],
  "iaf:mace_dragonsteel_lightning": [
    "summon lightning_bolt",
    "summon lightning_bolt", "playsound iaf.axe @a[r=8]",
    "damage @s[type=iaf:dragonio] 6",
    "damage @s[family=undead] 7"
  ],
  "iaf:spear_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe",
    "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:spear_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:spear_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:spear_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:spear_lightning_dragon": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonfo] 3",
  ],
  "iaf:spear_dragonsteel_lightning": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 3",
  ],

  "iaf:fire_rapier_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:rapier_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:ice_rapier_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:rapier_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:lightning_rapier_dragon": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonfo] 3",
  ],
  "iaf:rapier_dragonsteel_lightning": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 3",
  ],

  "iaf:spear_wooden": [

    "effect @s slowness 2 1 true"
  ],

  "iaf:spear_stone": [

    "effect @s slowness 2 1 true"
  ],

  "iaf:spear_iron": [

    "effect @s slowness 2 1 true"
  ],

  "iaf:spear_netherite": [

    "effect @s slowness 2 1 true"
  ],

  "iaf:spear_gold": [

    "effect @s slowness 2 1 true"
  ],

  "iaf:spear_diamond": [

    "effect @s slowness 2 1 true"
  ],





  "iaf:lance_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:lance_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:lance_lightning_dragon": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonfo] 3",
  ],

  "iaf:lance_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:lance_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:lance_dragonsteel_lightning": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 3",
  ],

  "iaf:longsword_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:longsword_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
  "iaf:longsword_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:longsword_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:longsword_lightning_dragon": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonfo] 3",
  ],
  "iaf:longsword_dragonsteel_lightning": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 3",
  ],
  "iaf:greatsword_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[type=iaf:dragonio] 5",
  ],
  "iaf:greatsword_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[type=iaf:dragonio] 5",
  ],
  "iaf:greatsword_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:greatsword_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:greatsword_lightning_dragon": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonfo] 3",
  ],
  "iaf:greatsword_dragonsteel_lightning": [
    "summon lightning_bolt",
    "damage @s[type=iaf:dragonio] 3",
  ],
  "iaf:katana_fire_dragon": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonio] 5",
  ],
  "iaf:katana_dragonsteel_fire": [
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonio] 5",
  ],
  "iaf:katana_ice_dragon": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:katana_dragonsteel_ice": [
    "summon iaf:icesw",
    "effect @s slowness 5 255",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonfo] 5",
  ],
  "iaf:katana_lightning_dragon": [
    "summon lightning_bolt",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonfo] 3",
  ],
  "iaf:katana_dragonsteel_lightning": [
    "summon lightning_bolt",
    "damage @s[family=undead] 7",
    "damage @s[type=iaf:dragonfo] 3",
  ],
  "iaf:halberd_fire_dragon": [
    "damage @s[type=iaf:dragonio] 5",
    "playsound iaf.axe @a[r=8]",
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe",
    "summon small_fireball ~~~ fire_dragon fire_dragon"
    ,
  ],
  "iaf:halberd_lightning_dragon": [
    "summon lightning_bolt", "playsound iaf.axe @a[r=8]",
    "damage @s[type=iaf:dragonio] 5",
  ],
  "iaf:halberd_ice_dragon": [
    "damage @s[type=iaf:dragonfo] 5",
    "playsound iaf.axe @a[r=8]",
    "summon iaf:icesw",
    "effect @s slowness 5 255",
  ],
  "iaf:halberd_dragonsteel_fire": [
    "damage @s 1 fire", "playsound iaf.axe @a[r=8]",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s[type=iaf:dragonio] 5"
  ],
  "iaf:halberd_dragonsteel_ice": [
    "summon iaf:icesw", "playsound iaf.axe @a[r=8]",
    "effect @s slowness 5 255",
    "damage @s[type=iaf:dragonfo] 5"
  ],
  "iaf:halberd_dragonsteel_lightning": [
    "summon lightning_bolt", "playsound iaf.axe @a[r=8]",
    "damage @s[type=iaf:dragonio] 5"
  ],
    /*gsFD*/"iaf:scythe_fire_dragon": [
    "damage @s[type=iaf:dragonio] 5",
    "damage @s 1 fire", "playsound iaf.axe @a[r=8]",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
  ],
    /*gsID*/ "iaf:scythe_ice_dragon": [
    "damage @s[type=iaf:dragonfo] 5", "playsound iaf.axe @a[r=8]",
    "summon iaf:icesw",
  ],
    /*gsLD*/ "iaf:scythe_lightning_dragon": [
    "summon lightning_bolt", "playsound iaf.axe @a[r=8]",
    "summon lightning_bolt",
    "summon iaf:damagehl_for_iaf ~~~ fireL",
    "summon iaf:damagehl_for_iaf ~~~ iceL",
  ],
    /*gDsFD*/ "iaf:scythe_dragonsteel_fire": [
    "damage @s[type=iaf:dragonio] 5",
    "damage @s 1 fire", "playsound iaf.axe @a[r=8]",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe", "summon small_fireball ~~~ fire_dragon",
    "damage @s 1 fire",
    "particle minecraft:lava_particle ~~~",
    "playsound mob.blaze.breathe",
  ],
    /*gDsID*/ "iaf:scythe_dragonsteel_ice": [
    "damage @s[type=iaf:dragonfo] 5", "playsound iaf.axe @a[r=8]",
    "summon iaf:icesw",
  ],
    /*gDsLD*/ "iaf:scythe_dragonsteel_lightning": [
    "summon lightning_bolt",
    "summon lightning_bolt", "playsound iaf.axe @a[r=8]",
    "summon iaf:damagehl_for_iaf ~~~ fireL",
    "summon iaf:damagehl_for_iaf ~~~ iceL",
  ],
    /*gDLS*/ "iaf:dragonsteel_lightning_sword": [
    "summon lightning_bolt",
    "summon lightning_bolt", "playsound iaf.axe @a[r=8]",
    "summon iaf:damagehl_for_iaf ~~~ fireL",
    "summon iaf:damagehl_for_iaf ~~~ iceL",
  ],
    /*gDIS*/ "iaf:dragonsteel_ice_sword": [
    "damage @s[type=iaf:dragonfo] 5",
    "effect @s slowness 5 255",
    "summon iaf:icesw",
  ],
    /*gDFS*/ "iaf:dragonsteel_fire_sword": [
    "damage @s[type=iaf:dragonio] 5", "summon small_fireball ~~~ fire_dragon",
  ],

  "iaf:longsword_wooden": [


  ],

  "iaf:longsword_stone": [


  ],

  "iaf:longsword_iron": [


  ],

  "iaf:longsword_netherite": [


  ],

  "iaf:longsword_gold": [


  ],

  "iaf:longsword_diamond": [


  ],


  "iaf:halberd_wooden": [


  ],

  "iaf:halberd_stone": [


  ],

  "iaf:halberd_iron": [


  ],

  "iaf:halberd_netherite": [


  ],

  "iaf:halberd_gold": [


  ],

  "iaf:halberd_diamond": [


  ],




  "iaf:rapier_wooden": [


  ],

  "iaf:rapier_stone": [


  ],

  "iaf:rapier_iron": [


  ],

  "iaf:rapier_netherite": [


  ],

  "iaf:rapier_gold": [


  ],

  "iaf:rapier_diamond": [


  ],










    /*gFBS*/ "iaf:flamed_dragon_bone_sword": [
    "damage @s[type=iaf:dragonio] 5", "summon small_fireball ~~~ fire_dragon",
  ],
    /*gLBS*/ "iaf:lightning_dragon_bone_sword": [
    "summon lightning_bolt",
    "summon lightning_bolt",
    "summon iaf:damagehl_for_iaf ~~~ fireL",
    "summon iaf:damagehl_for_iaf ~~~ iceL",
  ],
    /*gIcBS*/ "iaf:iced_dragon_bone_sword": [
    "damage @s[type=iaf:dragonfo] 5",
    "summon iaf:icesw",
    "effect @s slowness 5 255",
  ],
    /*gFIN*/ "iaf:hipocampus_slapper": [
    "effect @s slowness 5 2",
    "effect @s nausea 5 2"
  ]
};


system.runInterval(() => {
  for (let player of world.getPlayers()) {
    try {
      const container = player.getComponent("minecraft:inventory").container;
      if (Array.from({ length: container.size }, (v, i) => container.getItem(i)).some(it => it?.typeId === "minecraft:totem_of_undying"))
        player.runCommandAsync(`tag @s add luka`);
      else player.runCommandAsync(`tag @s remove luka`);
    } catch (e) {
      console.warn(e, e.stack);
    }
  }

}, 10);

system.runInterval(() => {
  for (let player of world.getPlayers()) {
    try {
      const container = player.getComponent("minecraft:inventory").container;
      if (Array.from({ length: container.size }, (v, i) => container.getItem(i)).some(it => it?.typeId === "minecraft:arrow"))
        player.runCommandAsync(`scoreboard players set @s bow 1`);
      else player.runCommandAsync(`scoreboard players set @s bow 0`);
    } catch (e) {
      console.warn(e, e.stack);
    }
  }
}, 10);
function createItemGUI(s, d) {
  const ui = new ActionFormData().title(`${d.it} Offhand`).button('OK', 'textures/items/diamond')

  ui.show(s).then(r => {
    if (r.selection === 0) {
      [`execute at @s[hasitem={location=slot.weapon.mainhand,item=${d.item.id}}] as @s run replaceitem entity @s slot.weapon.offhand 0 keep ${d.item.id}`,
      `execute at @s[hasitem={item=${d.item.id}}] as @s run clear @s ${d.item.id} 0 1`,
        `playsound random.levelup`,
      `w @s \n§9Lưu ý : Bạn chỉ có thể trang bị ${d.item.vietnameseName} vào tay trái NẾU không mang bất kỳ vật dụng nào trên tay trái \n§9Note: You can only fit the ${d.item.id.replace('_', ' ')} in your left hand IF you are not carrying any items in your left hand`].forEach(cmd => s.runCommandAsync(cmd))
    }
  })
}

world.afterEvents.entityHitEntity.subscribe(({
  hitEntity, damagingEntity: player
}) => {
  if (player.typeId != "minecraft:player") return
  if (player.getComponent("minecraft:inventory").container.getItem(player.selectedSlot)?.getTags().some(item => /^(minecraft:)(is_)axe$/.test(item))) {
    world.playSound("iaf.blunt", player.location)
  }
});

world.afterEvents.entityHitEntity.subscribe(({
  hitEntity, damagingEntity: player
}) => {
  if (player.typeId != "minecraft:player") return
  if (player.getComponent("minecraft:inventory").container.getItem(player.selectedSlot)?.getTags().some(item => /^(minecraft:)(is_)sword$/.test(item))) {
    world.playSound("iaf.sword", player.location)
  }
});
world.afterEvents.itemUse.subscribe(e => {
  const player = e.source, idwos = e.itemStack.typeId.split(':')[1];

  switch (e.itemStack.typeId) {
    case "minecraft:torch":
      createItemGUI(player, { it: 'Torch', item: { id: idwos, vietnameseName: 'đuốc' } });
      break
    case "minecraft:soul_torch":
      createItemGUI(player, { it: 'Soul Torch', item: { id: idwos, vietnameseName: 'đuốc linh hồn' } });
      break
    case "minecraft:lantern":
      createItemGUI(player, { it: 'Lantern', item: { id: idwos, vietnameseName: 'đèn lồng' } });
      break
    case "minecraft:soul_lantern":
      createItemGUI(player, { it: 'Soul Lantern', item: { id: idwos, vietnameseName: 'đèn linh hồn' } });
      break
  }
})