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

let peakMode = false;

system.runInterval(() => {
  const overworld = world.getDimension("overworld");
  const time = world.getTimeOfDay() % 24000;

  if (time < 13000 || time > 23000) return;

  for (const player of overworld.getPlayers()) {
    let rainChance = 0.003;

    if (!peakMode && Math.random() < 0.0005) {
      peakMode = true;
    }

    if (peakMode) {
      rainChance = 0.03;
      if (Math.random() < 0.01) peakMode = false;
    }

    if (Math.random() < rainChance) {
      const cluster = Math.random() < 0.3;
      const count = cluster ? 2 + Math.floor(Math.random() * 2) : 1;

      for (let i = 0; i < count; i++) {
        const offsetX = (Math.random() - 0.5) * 100;
        const offsetZ = (Math.random() - 0.5) * 100;
        const y = player.location.y + 55 + Math.floor(Math.random() * 15);

        overworld.spawnParticle("pa:estrellas_fugaces", {
          x: player.location.x + offsetX,
          y: y,
          z: player.location.z + offsetZ
        });
      }
    }
  }
}, 5);