 

import * as mc from "@minecraft/server";
const { world, system, EquipmentSlot } = mc;

system.runInterval(() => {
  const overworld = world.getDimension("overworld");


  // === SIREN EFFECT ===
  const sirens = overworld.getEntities({
    type: "iaf:siren",
    families: ["quyenruhaha"]
  });

  for (const siren of sirens) {
    const players = overworld.getPlayers({
      location: siren.location,
      maxDistance: 35,
    });

    for (const player of players) {
      if (player.getGameMode() === "creative") continue;

      const dx = siren.location.x - player.location.x;
      const dz = siren.location.z - player.location.z;
      const distance = Math.sqrt(dx * dx + dz * dz);

      if (distance < 3) continue;

      const step = 0.23;
      const newX = player.location.x + (dx / distance) * step;
      const newZ = player.location.z + (dz / distance) * step;
      const newY = player.location.y;

      player.runCommandAsync(`tp @s[tag=!earplugs____] ${newX} ${newY} ${newZ}`);

      if (!player.hasTag("hypnotized")) {
        player.addTag("hypnotized");
        player.runCommandAsync(`camera @s[tag=!earplugs____] fade time 0.6 0.14 0.6 color 189 0 0`);
        system.runTimeout(() => {
          player.removeTag("hypnotized");
        }, 40);
      }

      if (!player.hasTag("hypnotized2")) {
        player.addTag("hypnotized2");
        player.runCommandAsync(`playsound iaf.siren.song @s[tag=!earplugs____] ~~~ 12`);

        system.runTimeout(() => {
          player.removeTag("hypnotized2");
        }, 201);
      }
    }
  }
}, 5); // Chạy mỗi 5 ticks (0.25 giây)

world.afterEvents.worldInitialize.subscribe(() => {
  world.sendMessage("§3§l Ice and Fire: Reflourished §8- §9Abyssal Reign §3(Part 1)");
  world.sendMessage("§7Created by: §aTruncatemol§7, §aPTL§7, §aTrứng Dái Nhỏ (Kim Bảo)§7, §aHieze Gaming");
  world.sendMessage("§7Supported by: §bThienVnPro");
});


function entityHitEvent({ damagingEntity: attacker, hitEntity, hitBlock }) {
  const equippable = attacker?.getComponent("minecraft:equippable");
  const weapon = equippable?.getEquipment(mc.EquipmentSlot.Mainhand);

  if (weapon?.typeId === "iaf:godtrident") {
    let target = hitEntity || hitBlock;
    if (hitEntity) {
      if (Math.random() < 0.4) {
        attacker.runCommand("damage @e[r=10,type=!player] 10");
        attacker.runCommand("effect @e[r=10,type=!player] slowness 3 1 true");


        target.runCommand("effect @e[r=10,type=!player] slowness 3 1 true");
        
        target.runCommand("damages @s 10 true");
        attacker.dimension.spawnParticle("iaf:nuocban", target.location, new mc.MolangVariableMap());
        attacker.dimension.spawnParticle("tranhuylong:songxungkich", target.location, new mc.MolangVariableMap());
      }

      if (Math.random() < 0.05) {
        attacker.runCommand("summon lightning_bolt");
        attacker.runCommand("execute at @e[r=17,c=8,type=!player,type=!item,type=!xp_orb] run summon lightning_bolt");
      }

      if (attacker.hasTag("giutset")) {
        attacker.runCommand("execute at @e[r=13,c=6,type=!player,type=!item,type=!xp_orb] as @e[r=13,c=6,type=!player,type=!item,type=!xp_orb] run function giutset");
      }
    }
  }

  if (weapon?.typeId === "iaf:hippocampus_slapper") {
    let target = hitEntity || hitBlock;
    if (hitEntity) {
      target.runCommand("effect @s nausea 5 2");
      target.runCommand("effect @s slowness 5 2");
    }
  }
}


mc.world.afterEvents.entityHitEntity.subscribe(entityHitEvent);
mc.world.afterEvents.entityHitBlock.subscribe(entityHitEvent);

