Script API - v1.26.10.23
    Preparing search index...
    Index

    Constructors

    Properties

    afterEvents: WorldAfterEvents

    This property can be read in early-execution mode.

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

    const callback = world.afterEvents.itemUse.subscribe((event) => {
    if (event.itemStack.typeId == "minecraft:water_bucket") {
    event.source.sendMessage("You used water bucket once.");

    // Unsubscribe callback after first use
    world.afterEvents.itemUse.unsubscribe(callback);
    }
    });
    beforeEvents: WorldBeforeEvents

    This property can be read in early-execution mode.

    // Check out how BeforeEvents privilege system work:
    // https://wiki.bedrock.dev/scripting/script-server.html#beforeevents-privilege-system
    import { world, system, TimeOfDay } from "@minecraft/server";

    // Use system.run()
    world.beforeEvents.chatSend.subscribe((event) => {
    event.cancel = true;
    // setTime changes world state, must be run after its execution by a tick
    system.run(() => {
    world.setTimeOfDay(TimeOfDay.Night);
    });
    });

    /**
    @param {number} ticks
    /
    function sleep(ticks) {
    return new Promise((resolve) => {
    system.runTimeout(() => resolve(), ticks);
    });
    }

    // Or execute function at a later tick using async functions
    world.beforeEvents.chatSend.subscribe(async (event) => {
    // synchronous code
    event.cancel = true;

    // asynchronous code
    await sleep(10); // Pretend you have a sleep function that returns a promise that resolves in 10 ticks
    world.setTimeOfDay(TimeOfDay.Night);
    });
    import { system, world } from "@minecraft/server";

    const callback = world.beforeEvents.itemUse.subscribe((event) => {
    if (event.itemStack.typeId == "minecraft:water_bucket") {
    event.source.sendMessage("You cannot use water bucket at this time, please try again.");
    event.cancel = true;

    // Unsubscribe callback after first use
    system.run(() => world.beforeEvents.itemUse.unsubscribe(callback));
    }
    });
    gameRules: GameRules
    import { world } from "@minecraft/server";

    world.gameRules.doDayLightCycle = false;
    world.gameRules.doEntityDrops = false;
    world.gameRules.doFireTick = false;
    world.gameRules.doWeatherCycle = false;
    world.gameRules.doMobSpawning = false;
    isHardcore: boolean
    import { world } from "@minecraft/server";

    // Subscribe to the `playerSpawn` event, which is triggered when the world is initialized
    world.afterEvents.playerSpawn.subscribe(() => {
    // Check if the world is in hardcore mode
    // Notify players about the current mode
    if (world.isHardcore) {
    world.sendMessage("Welcome to Hardcore mode! Be careful, as death is permanent.");
    } else {
    world.sendMessage("This world is not in Hardcore mode. Play safely!");
    }
    });
    scoreboard: Scoreboard
    import { world } from "@minecraft/server";
    const money = world.scoreboard.getObjective("money");
    seed: string
    structureManager: StructureManager
    import { world } from "@minecraft/server";

    world.structureManager.getWorldStructureIds().forEach((id) => {
    const structure = world.structureManager.get(id);
    structure.isValid;
    });

    Methods

    • Returns void

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      world.clearDynamicProperties();
    • Returns number

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      const gametime = world.getAbsoluteTime();
      world.sendMessage("Current gametime: " + gametime);
    • Returns Player[]

      This function can't be called in read-only mode.

      This function can throw errors.

      CommandError

      minecraftcommon.InvalidArgumentError

      This function can't be called in early-execution mode.

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

      system.runInterval(() => {
      for (const player of world.getAllPlayers()) {
      const health = player.getComponent(EntityHealthComponent.componentId);
      player.onScreenDisplay.setActionBar(
      `Name: ${player.name} | Health: ${health.currentValue.toFixed()} / ${health.effectiveMax}`
      );
      }
      }, 5000);
    • Returns number

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      world.sendMessage("Day " + world.getDay());
    • Returns Vector3

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      const spawnLocation = world.getDefaultSpawnLocation();
      world.sendMessage(`Spawn location: ${spawnLocation.x}, ${spawnLocation.y}, ${spawnLocation.z}`);
    • Parameters

      • dimensionId: string

      Returns Dimension

      This function can't be called in read-only mode.

      This function can throw errors.

      This function can't be called in early-execution mode.

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

      world.getDimension("nether");
      import { world } from "@minecraft/server";

      world.getDimension("overworld");
      import { world } from "@minecraft/server";

      world.getDimension("the_end");
    • Parameters

      • identifier: string

      Returns string | number | boolean | Vector3

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      // Subscribe to the `playerSpawn` event, which is triggered when a player joins the world
      world.afterEvents.playerSpawn.subscribe((eventData) => {
      // Extract the player entity from the event data
      const player = eventData.player;

      // Attempt to retrieve a custom dynamic property
      const playersCount = world.getDynamicProperty("playersCount");

      if (typeof playersCount !== "number") {
      // If the property doesn't exist, set a default value
      player.setDynamicProperty("playersCount", 1);
      } else {
      // If the property exists, increment its value
      player.setDynamicProperty("playersCount", playersCount + 1);
      }
      });
    • Returns string[]

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      world.getDynamicPropertyIds().forEach((id) => {
      const value = world.getDynamicProperty(id)!;
      world.sendMessage(`Dynamic property ${id} has value ${value}`);
      });
      world.sendMessage("There are " + world.getDynamicPropertyIds().length + " dynamic properties");
    • Returns number

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      world.getDynamicPropertyTotalByteCount();
    • Parameters

      • id: string

      Returns Entity

      This function can't be called in read-only mode.

      This function can throw errors.

      This function can't be called in early-execution mode.

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

      // Entity::id - format: '-451021564564561'
      const entityId = world
      .getDimension("overworld")
      .spawnEntity("minecraft:npc", { x: 0, y: 70, z: 0 }, { initialPersistence: true }).id;
      world.getEntity(entityId).typeId; // minecraft:npc
    • Returns MoonPhase

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      // Get the current moon phase
      const moonPhase: MoonPhase = world.getMoonPhase();

      // Display a message based on the current moon phase
      switch (moonPhase) {
      case MoonPhase.FullMoon:
      world.sendMessage("It's full moon!");
      break;
      case MoonPhase.WaningGibbous:
      world.sendMessage("It's waning gibbous moon!");
      break;
      // Add cases for other moon phases as needed
      default:
      world.sendMessage("It's another phase of the moon.");
      }
    • Parameters

      Returns Player[]

      This function can't be called in read-only mode.

      This function can throw errors.

      CommandError

      minecraftcommon.InvalidArgumentError

      This function can't be called in early-execution mode.

      import { world } from "@minecraft/server";
      world.getPlayers({ families: ["player"] });
      import { EntityQueryOptions, world } from "@minecraft/server";

      const entityQueryOptions: EntityQueryOptions = {
      minLevel: 10,
      maxLevel: 30,
      tags: ["team_red"],
      excludeNames: ["Admin"],
      };

      const filteredPlayers = world.getPlayers(entityQueryOptions);
      console.log(
      "Filtered Players:",
      filteredPlayers.map((player) => player.name)
      );
    • Returns number

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      function GetWorldTime() {
      const daytime = world.getTimeOfDay() + 6000;
      const datetime = new Date(daytime * 3.6 * 1000);
      const hours = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
      const minutes = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();

      return { hours, minutes };
      }

      system.runInterval(() => {
      const { hours, minutes } = GetWorldTime();
      for (const player of world.getAllPlayers()) {
      player.onScreenDisplay.setActionBar(`Time - ${hours}:${minutes}`);
      }
      });
    • Parameters

      Returns void

      This function can't be called in read-only mode.

      This function can throw errors.

      minecraftcommon.PropertyOutOfBoundsError

      This function can't be called in early-execution mode.

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

      // Subscribe to the `playerSpawn` event, which is triggered when a player joins the world
      world.afterEvents.playerSpawn.subscribe((eventData) => {
      const player = eventData.player;

      // Play a music track for the player when they join the world
      player.playMusic("minecraft:music.game", {
      volume: 1.0, // Volume level of the music (1.0 is normal)
      fade: 0.5, // Fade in/out time in seconds
      });

      player.sendMessage("Welcome! Enjoy the music while you play.");
      });
    • Parameters

      Returns void

      This function can't be called in read-only mode.

      This function can throw errors.

      This function can't be called in early-execution mode.

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

      // Subscribe to the `playerSpawn` event, which is triggered when a player joins the world
      world.afterEvents.playerSpawn.subscribe((eventData) => {
      // ignore if player respawns
      if (!eventData.initialSpawn) return;

      const player = eventData.player;

      // Construct a welcome message for the player who just joined
      const welcomeMessage = `Welcome ${player.name}! Have fun in the world of Minecraft!`;

      // Use world.sendMessage to broadcast the message to all players in the world
      world.sendMessage(welcomeMessage);

      // Notify the player specifically with a different message
      player.sendMessage("Feel free to explore and build your adventure!");
      });
    • Parameters

      • absoluteTime: number

      Returns void

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

      import { TimeOfDay, world } from "@minecraft/server";
      world.setAbsoluteTime(TimeOfDay.Noon);
    • Parameters

      Returns void

      This function can't be called in read-only mode.

      This function can throw errors.

      Error

      LocationOutOfWorldBoundariesError

      This function can't be called in early-execution mode.

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

      world.setDefaultSpawnLocation({
      x: 0,
      y: -64,
      z: 0,
      });
    • Parameters

      • identifier: string
      • Optionalvalue: string | number | boolean | Vector3

      Returns void

      This function can't be called in read-only mode.

      This function can throw errors.

      minecraftcommon.ArgumentOutOfBoundsError

      This function can't be called in early-execution mode.

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

      // Subscribe to the `playerSpawn` event, which is triggered when a player joins the world
      world.afterEvents.playerSpawn.subscribe((eventData) => {
      // Extract the player entity from the event data
      const player = eventData.player;

      // Attempt to retrieve a custom dynamic property
      let playersCount = world.getDynamicProperty("playersCount");

      // If the property doesn't exist
      playersCount ??= 0;

      // set a default value
      // @ts-ignore assuming playersCount is a number
      player.setDynamicProperty("playersCount", playersCount + 1);
      });
    • Parameters

      • timeOfDay: number

      Returns void

      This function can't be called in read-only mode.

      This function can throw errors.

      This function can't be called in early-execution mode.

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

      world.setTimeOfDay(TimeOfDay.Day);
      import { TimeOfDay, world } from "@minecraft/server";

      world.setTimeOfDay(TimeOfDay.Midnight);
    • Returns void

      This function can't be called in read-only mode.

      This function can't be called in early-execution mode.

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

      world.stopMusic();