import { world, system, EntityComponentTypes, EquipmentSlot } from "@minecraft/server";

world.afterEvents.entityHurt.subscribe(hurt => {
  try {
    let mob = hurt.hurtEntity;
    let player = hurt.damageSource.damagingEntity;

    if (!player || !player.hasComponent(EntityComponentTypes.Equippable)) return;

    let equipment = player.getComponent(EntityComponentTypes.Equippable);
    let mainHandItem = equipment.getEquipment(EquipmentSlot.Mainhand);

    const sweep = (radius, damage) => {
      player.runCommandAsync(`tag @s add sweeper`);
      player.runCommandAsync(`tag @e[r=${radius},family=!inanimate,family=!tamed,tag=!sweeper,type=!item] add sweeped`);
      player.runCommandAsync(`execute @s[tag=sweeper] ~~~ damage @e[r=${radius},tag=sweeped,tag=!sweeper] ${damage} entity_attack entity @s`);
      player.runCommandAsync(`particle plus:sweep ^ ^1.2 ^2.3`);
      player.runCommandAsync(`playsound random.sweep @a[r=5] ~~~ 1 1`);
      player.runCommandAsync(`tag @e[tag=sweeped] remove sweeped`);
      player.runCommandAsync(`tag @s remove sweeper`);
    };

    let id = mainHandItem.typeId;

    if (id.includes("_dragon_sword")) {
      sweep(4, 3);
      player.runCommandAsync(`effect @s weakness 1 255 true`);
    } else if (
      //id.includes("") ||
      id.includes("_sword_") ||
      id.includes("sword_") ||
      id.includes("_sword")
    ) {
      sweep(4, 2);
    } else if (id.includes("plus_")) {
      sweep(4, 1);
    } else if (id.includes("_dragon_axe")) {
      let random = Math.floor(Math.random() * 5);
      if (random == 0) {
        player.runCommandAsync(`playsound damage.fallbig @a[r=5] ~~~ 0.5`);
        player.runCommandAsync(`effect @e[name="${mob.nameTag}"] nausea 10 1 true`);
        player.runCommandAsync(`playsound random.anvil_land @a[r=5] ~~~ 0.5`);
        player.runCommandAsync(`effect @e[name="${mob.nameTag}"] slowness 3 6 true`);
        player.runCommandAsync(`effect @s weakness 2 255 true`);
      }
    } else if (id.includes("axe_") || id.includes("_axe_") || id.includes("_axe")) {
      if (
        ["minecraft:zombie", "minecraft:husk", "minecraft:drowned", "minecraft:zombie_pigman", "minecraft:skeleton", "minecraft:stray", "minecraft:bogged"].includes(mob.typeId)
      ) {
        player.runCommandAsync(`damage @e[name="${mob.nameTag}"] 5`);
      }
    } else if (id.includes("plus_")) {
      let random = Math.floor(Math.random() * 4);
      if (random == 0) {
        player.runCommandAsync(`effect @s resistance 5 1 true`);
      }
    } else if (id.includes("plus_")) {
      player.runCommandAsync(`effect @s weakness 1 255 true`);
    }
  } catch (e) {
    // console.warn(e);
  }
});
//mẫu ở đây