Script API - v1.26.10.22
    Preparing search index...
    import { Vector3Utils, VECTOR3_NORTH, VECTOR3_WEST, VECTOR3_EAST, VECTOR3_SOUTH } from "@minecraft/math";
    import { Entity } from "@minecraft/server";
    import { MinecraftBlockTypes } from "@minecraft/vanilla-data";

    const VECTOR3_NORTHWEST = Vector3Utils.add(VECTOR3_NORTH, VECTOR3_WEST);
    const VECTOR3_NORTHEAST = Vector3Utils.add(VECTOR3_NORTH, VECTOR3_EAST);
    const VECTOR3_SOUTHWEST = Vector3Utils.add(VECTOR3_SOUTH, VECTOR3_WEST);
    const VECTOR3_SOUTHEAST = Vector3Utils.add(VECTOR3_SOUTH, VECTOR3_EAST);

    /**
    Get the blocks from player surroundings and set to obsidian
    /
    export function createWallAroundEntity(entity: Entity) {
    const location = entity.location;
    const block = entity.dimension.getBlock(location);
    if (!block) return;
    const blockNorth = block.north();
    const blockSouth = block.south();
    const blockEast = block.east();
    const blockWest = block.west();
    const blockNorthWest = block.offset(VECTOR3_NORTHWEST);
    const blockNorthEast = block.offset(VECTOR3_NORTHEAST);
    const blockSouthWest = block.offset(VECTOR3_SOUTHWEST);
    const blockSouthEast = block.offset(VECTOR3_SOUTHEAST);
    const blocks = [
    blockNorth,
    blockSouth,
    blockEast,
    blockWest,
    blockNorthWest,
    blockNorthEast,
    blockSouthWest,
    blockSouthEast,
    ];
    // Check if the player have contact with cactus or sweet berry bush
    for (const block of blocks) {
    if (!block) continue;
    block.setType(MinecraftBlockTypes.Obsidian);
    }
    }
    Index

    Constructors

    Properties

    dimension: Dimension
    import { world } from "@minecraft/server";

    // Block explodes when player break block
    world.afterEvents.playerBreakBlock.subscribe((event) => {
    event.block.dimension.createExplosion(event.block.location, 1);
    });
    isAir: boolean

    This property can't be read in early-execution mode.

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

    // bridge egg
    world.afterEvents.entitySpawn.subscribe(({ entity }) => {
    if (entity.typeId === "minecraft:egg") {
    const id = system.runInterval(() => {
    const block = entity.dimension.getBlock(entity.location).below();
    if (block.isAir) block.setPermutation(BlockPermutation.resolve("minecraft:wool"));
    else system.clearRun(id);
    });
    }
    });
    isLiquid: boolean

    This property can't be read in early-execution mode.

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

    // Block at (0, 0, 0)
    const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
    if (block.isLiquid) {
    console.warn("block is liquid");
    }
    isValid: boolean
    isWaterlogged: boolean

    This property can't be read in early-execution mode.

    localizationKey: string

    This property can't be read in early-execution mode.

    location: Vector3

    This property can throw errors.

    This property can't be read in early-execution mode.

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

    // Block at (0, 0, 0)
    const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
    block.dimension.createExplosion(block.location, 10);
    permutation: BlockPermutation

    This property can't be read in early-execution mode.

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

    // Block at (0, 0, 0)
    const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });

    // List all block states
    const blockStates = block.permutation.getAllStates();
    console.log(JSON.stringify(blockStates, null, 4));
    type: BlockType

    This property can't be read in early-execution mode.

    typeId: string

    This property can't be read in early-execution mode.

    x: number
    y: number
    z: number

    Methods

    • Parameters

      • Optionalsteps: number

        Defaults to: 1

      Returns Block

      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 player place block event
      world.afterEvents.playerPlaceBlock.subscribe((eventData) => {
      const { player, block } = eventData;

      // Get the block below the placed block
      const blockBelow = block.below(1);

      // Copy the type of the block below to the placed block
      block.setType(blockBelow.type);
      });
    • Type Parameters

      • T extends string

      Parameters

      • componentId: T

      Returns BlockComponentReturnType<T>

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

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

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

      // Chest Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      block.setPermutation(BlockPermutation.resolve("minecraft:chest"));

      const inventory = block.getComponent("inventory").container;
      inventory.addItem(new ItemStack("minecraft:cobblestone", 64));
      import { BlockPermutation, ItemStack, SignSide, world } from "@minecraft/server";
      import { MinecraftBlockTypes } from "@minecraft/vanilla-data";

      // Chest Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      block.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.BirchHangingSign));

      const sign = block.getComponent("minecraft:sign");
      sign.setText("back", SignSide.Back);
      sign.setText("front", SignSide.Front);
      import { BlockPermutation, ItemStack, world } from "@minecraft/server";
      import { MinecraftBlockTypes } from "@minecraft/vanilla-data";

      // Chest Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      block.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.DarkoakStandingSign));

      const sign = block.getComponent("minecraft:sign");
      sign.setText("");
      import { BlockPermutation, world } from "@minecraft/server";
      import { MinecraftBlockTypes } from "@minecraft/vanilla-data";

      // This example demonstrates how to work with pistons and their attached blocks
      // We'll create a simple piston setup and check what blocks are attached to it

      const overworld = world.getDimension("overworld");

      // Step 1: Create a piston at position (0, 64, 0)
      const pistonLocation = { x: 0, y: 64, z: 0 };
      const pistonBlock = overworld.getBlock(pistonLocation);
      pistonBlock.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.Piston));

      // Step 2: Place some blocks in front of the piston that it can push
      // Let's place a few dirt blocks in front of the piston
      const blockPositions = [
      { x: 0, y: 64, z: 1 }, // Directly in front
      { x: 0, y: 64, z: 2 }, // One block further
      { x: 0, y: 64, z: 3 }, // Two blocks further
      ];

      blockPositions.forEach((pos) => {
      const block = overworld.getBlock(pos);
      block.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.Dirt));
      });

      // Step 3: Get the piston component and check its properties
      const pistonComponent = pistonBlock.getComponent("piston");

      if (pistonComponent) {
      console.log("Piston component found!");

      // Get the blocks that would be affected if this piston extends
      const attachedBlocks = pistonComponent.getAttachedBlocks();

      console.log("Number of attached blocks:", attachedBlocks.length);

      // Display information about each attached block
      attachedBlocks.forEach((block, index) => {
      console.log(`Attached block ${index + 1}:`);
      console.log(` - Location: (${block.location.x}, ${block.location.y}, ${block.location.z})`);
      console.log(` - Type: ${block.typeId}`);
      });

      // You can also check if the piston can be activated
      // Note: This would require redstone power in an actual game scenario
      console.log("Piston state information logged to console.");
      } else {
      console.log("Failed to get piston component - make sure the block is actually a piston!");
      }

      // Bonus: Listen for piston activation events in the world
      // This is more useful for real gameplay scenarios
      world.afterEvents.pistonActivate.subscribe((eventData) => {
      const { dimension, piston, isExpanding } = eventData;

      console.log(`Piston ${isExpanding ? "extending" : "retracting"} detected!`);

      // The 'piston' here is already the BlockPistonComponent
      // We can directly use its methods
      const attachedBlocks = piston.getAttachedBlocks();
      console.log(`This piston is moving ${attachedBlocks.length} blocks`);

      // Log details about each block being moved
      attachedBlocks.forEach((block, index) => {
      console.log(
      ` Block ${index + 1}: ${block.typeId} at (${block.location.x}, ${block.location.y}, ${block.location.z})`
      );
      });
      });
    • 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";

      /**
      Function to check if a given block is powered by redstone
      @param {import('@minecraft/server').Block} block
      @returns true if a given block is powered by redstone
      /
      function isBlockPowered(block) {
      // Get the redstone power level of the block
      // The 'getRedstonePower' method returns a number representing the power level
      // If the power level is greater than 0, it means the block is powered
      return block.getRedstonePower() > 0;
      }
    • Parameters

      • blockName: string
      • Optionalstates: Record<string, string | number | boolean>

      Returns boolean

      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";

      /**
      Function to check if a given block is red wool
      @param {import('@minecraft/server').Block} block
      @returns true if a given block is red wool
      /
      function isRedWool(block) {
      // Define the type id of block we are checking for
      let typeId = "wool";

      // Define the block state we are looking for
      // In this case, we want the color to be red
      let states = { color: "red" };

      // Check if the block matches the specified type and states
      // The 'matches' method returns true if the block is of the specified type and has the specified states
      return block.matches(typeId, states);
      }
    • Parameters

      • Optionalsteps: number

        Defaults to: 1

      Returns Block

      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";

      // Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      const blockAtNorth = block.north();
    • Parameters

      Returns void

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

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

      import { BlockPermutation, world } from "@minecraft/server";
      // Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      if (block.typeId === "minecraft:bedrock") {
      block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      }
    • Parameters

      Returns void

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

      This function can throw errors.

      Error

      LocationInUnloadedChunkError

      LocationOutOfWorldBoundariesError

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

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

      // Get all available block types
      const blockTypes = BlockTypes.getAll();

      // Function to get a random block type
      const getRandomBlockType = () => blockTypes[Math.floor(blockTypes.length * Math.random())];

      // Subscribe to player place block event
      world.afterEvents.playerPlaceBlock.subscribe((eventData) => {
      const { player, block } = eventData;
      const randomBlockType = getRandomBlockType();

      player.sendMessage(`You placed a ${randomBlockType.id} block!`);
      block.setType(randomBlockType);
      });
    • Parameters

      • Optionalsteps: number

        Defaults to: 1

      Returns Block

      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";

      // Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      const nearbyBlock = block.south();
    • Parameters

      • Optionalsteps: number

        Defaults to: 1

      Returns Block

      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";

      // Block at (0, 0, 0)
      const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
      const blockAtWest = block.west(10);