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

const FOG_ID = "mid:fog_the_otherside";

// límites del área (XZ)
const MIN = -30000;
const MAX = -20000;

function isInsideZone(x, z) {
  return (
    x >= MIN && x <= MAX &&
    z >= MIN && z <= MAX
  );
}

system.runInterval(() => {
  for (const player of world.getPlayers()) {

    // Solo The End
    if (player.dimension.id !== "minecraft:the_end") {
      player.runCommandAsync("fog @s remove mid_otherside");
      continue;
    }

    const { x, z } = player.location;

    if (isInsideZone(x, z)) {
      // Aplica la niebla si está dentro
      player.runCommandAsync(
        `fog @s push ${FOG_ID} mid_otherside`
      );
    } else {
      // Quita la niebla si salió del área
      player.runCommandAsync(
        "fog @s remove mid_otherside"
      );
    }
  }
}, 20); // se ejecuta cada segundo