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

const breath = {   
  "faf:ice_dragon": "faf:ice_dragon_adult_icebreath",
  "faf:ice_dragon1": "faf:ice_dragon_adult_icebreath",
  "faf:ice_dragon2": "faf:ice_dragon_adult_icebreath",
  "faf:ice_dragon3": "faf:ice_dragon_adult_icebreath"

};  

world.afterEvents.itemUse.subscribe(e => {
  const p = e.source;
  if (e.itemStack.typeId !== "faf:dragon_wand") return;

  const r = p.getComponent("minecraft:riding")?.entityRidingOn;
  if (!r) return;

  const t = breath[r.typeId]; // Sửa chỗ này
  if (!t) return;

  const d = p.getViewDirection();
  const distance = 7.1;
  const spawnPos = {
    x: p.location.x + d.x * distance,
    y: p.location.y + d.y * distance,
    z: p.location.z + d.z * distance
  };

  for (let i = 0; i < 2; i++) {
    const angleOffset = (i - 0.3) * 0.0;
    const dx = d.x * Math.cos(angleOffset) - d.z * Math.sin(angleOffset);
    const dz = d.x * Math.sin(angleOffset) + d.z * Math.cos(angleOffset);

    const b = p.dimension.spawnEntity(t, spawnPos);
    if (!b) continue;

    system.runTimeout(() => {
      b.applyImpulse({ x: dx * 7.0, y: d.y * 7.0, z: dz * 7.0 });
    }, 0.1);
  }

  p.playSound("mythology.ice.breath", { location: p.location, volume: 0.8 });
  r.playAnimation("animation.Dracan.firing");
});