Script API - v1.21.0
    Preparing search index...

    Represents a block in a dimension. A block represents a unique X, Y, and Z within a dimension and get/sets the state of the block at that location. This type was significantly updated in version 1.17.10.21.

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

    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

    Properties

    dimension: Dimension

    Returns the dimension that the block is within.

    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

    Returns true if this block is an air block (i.e., empty space).

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    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

    Returns true if this block is a liquid block - (e.g., a water block and a lava block are liquid, while an air block and a stone block are not. Water logged blocks are not liquid blocks).

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    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");
    }
    isSolid: boolean

    Returns true if this block is solid and impassible - (e.g., a cobblestone block and a diamond block are solid, while a ladder block and a fence block are not).

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    isWaterlogged: boolean

    Returns or sets whether this block has a liquid on it.

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    location: Vector3

    Coordinates of the specified block.

    This property can throw when used.

    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

    Additional block configuration data that describes the block.

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

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

    // Block at (0, 0, 0)
    const block = world.getDimension("overworld").getBlock({ x: 0, y: 0, z: 0 });
    console.warn(block.typeId, JSON.stringify(block.permutation.getAllStates()));
    type: BlockType

    Gets the type of block.

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    typeId: string

    Identifier of the type of block for this block.

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    x: number

    X coordinate of the block.

    y: number

    Y coordinate of the block.

    z: number

    Z coordinate of the block.

    Methods

    • Returns Vector3

      Returns the @minecraft/server.Location of the center of this block on the X and Z axis.

    • Beta

      Parameters

      • blockToPlace: string | BlockPermutation | BlockType

        Block type or block permutation to check placement for.

      • OptionalfaceToPlaceOn: Direction

        Optional specific face of this block to check placement against.

      Returns boolean

      Returns true if the block type or permutation can be placed on this block, else false.

      Checks to see whether it is valid to place the specified block type or block permutation, on a specified face on this block

      This function can throw errors.

      Error

      LocationInUnloadedChunkError

      LocationOutOfWorldBoundariesError

    • Returns Vector3

      Returns the @minecraft/server.Location of the center of this block on the X, Y, and Z axis.

    • Type Parameters

      Parameters

      • componentId: T

        The identifier of the component (e.g., 'minecraft:inventory'). If no namespace prefix is specified, 'minecraft:' is assumed. Available component IDs can be found as part of the BlockComponentTypes enum.

      Returns BlockComponentTypeMap[T]

      Returns the component if it exists on the block, otherwise undefined.

      Gets a component (that represents additional capabilities) for a block - for example, an inventory component of a chest block.

      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("lol");
      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.Piston));

      const piston = block.getComponent("piston");
      console.warn(piston.state, JSON.stringify(piston.getAttachedBlocksLocations()));
    • Parameters

      • Optionalamount: number

        Number of instances of this block to place in the item stack.

      • OptionalwithData: boolean

        Whether additional data facets of the item stack are included.

      Returns ItemStack

      An itemStack with the specified amount of items and data. Returns undefined if block type is incompatible.

      Creates a prototype item stack based on this block that can be used with Container/ContainerSlot APIs.

    • Parameters

      • tag: string

        Tag to check for.

      Returns boolean

      Returns true if the permutation of this block has the tag, else false.

      Checks to see if the permutation of this block has a specific tag.

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

      // Fetch the block
      const block = world.getDimension("overworld").getBlock({ x: 1, y: 2, z: 3 });

      console.log(`Block is dirt: ${block.hasTag("dirt")}`);
      console.log(`Block is wood: ${block.hasTag("wood")}`);
      console.log(`Block is stone: ${block.hasTag("stone")}`);
    • Returns boolean

      True if this block object is still working and valid.

      Returns true if this reference to a block is still valid (for example, if the block is unloaded, references to that block will no longer be valid.)

    • Parameters

      • blockName: string

        Block type identifier to match this API against.

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

        Optional set of block states to test this block against.

      Returns boolean

      Returns true if the block matches the specified criteria.

      Tests whether this block matches a specific criteria.

    • Parameters

      • Optionalsteps: number

        Number of steps to the north to step before returning.

      Returns Block

      Returns the Block to the north of this block (negative in the Z direction).

      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

      • offset: Vector3

        The offset vector. For example, an offset of 0, 1, 0 will return the block above the current block.

      Returns Block

      Block at the specified offset, or undefined if that block could not be retrieved (for example, the block and its relative chunk is not loaded yet.)

      Returns a block at an offset relative vector to this block.

    • Parameters

      • permutation: BlockPermutation

        Permutation that contains a set of property states for the Block.

      Returns void

      Sets the block in the dimension to the state of the permutation.

      This function can't be called in read-only 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"));
      }
    • Beta

      Parameters

      • isWaterlogged: boolean

        true if the block should have water within it.

      Returns void

      Sets whether this block has a water logged state - for example, whether stairs are submerged within water.

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

      This function can throw errors.

      Error

      LocationInUnloadedChunkError

      LocationOutOfWorldBoundariesError

    • Parameters

      • Optionalsteps: number

        Number of steps to the south to step before returning.

      Returns Block

      Returns the Block to the south of this block (positive in the Z direction).

      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();
    • Beta

      Parameters

      • permutation: BlockPermutation

        Permutation that contains a set of property states for the Block.

      Returns boolean

      Returns true if the block permutation data was successfully set, else false.

      Tries to set the block in the dimension to the state of the permutation by first checking if the placement is valid.

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

    • Parameters

      • Optionalsteps: number

        Number of steps to the west to step before returning.

      Returns Block

      Returns the Block to the west of this block (negative in the X direction).

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