Script API - v1.20.60
    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.

    Index

    Properties

    dimension: Dimension

    Returns the dimension that the block is within.

    isAir: boolean

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

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    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

    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.

    permutation: BlockPermutation

    Additional block configuration data that describes the block.

    This property can throw when used.

    LocationInUnloadedChunkError

    LocationOutOfWorldBoundariesError

    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 { ItemStack } from "@minecraft/server";

      const getEntityInventoryComponent = block.getComponent("inventory");
      const inventoryContainer = getEntityInventoryComponent.container;

      // Custom function to add an item to the inventory
      function addItemToInventory(itemStack) {
      return inventoryContainer.addItem(itemStack);
      }

      // Custom function to move an item within the inventory
      function moveItemWithinInventory(fromSlot, toSlot) {
      inventoryContainer.moveItem(fromSlot, toSlot, inventoryContainer);
      }

      // Custom function to transfer an item from inventory to another container
      function transferItemToContainer(fromSlot, targetContainer) {
      return inventoryContainer.transferItem(fromSlot, targetContainer);
      }

      // Example usage of the custom functions
      const newItemStack = new ItemStack("apple", 10); // Assuming "apple" is a valid item
      const addedItem = addItemToInventory(newItemStack);
      if (addedItem) {
      console.log("Item added to inventory:", addedItem);
      }

      const sourceSlot = 2;
      const destinationSlot = 5;
      moveItemWithinInventory(sourceSlot, destinationSlot);

      const targetContainer = someOtherContainer; // Assuming 'someOtherContainer' is an instance of another container
      const transferredItem = transferItemToContainer(0, targetContainer);
      if (transferredItem) {
      console.log("Item transferred to another container:", transferredItem);
      }
    • Beta

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

    • Beta

      Parameters

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

      Returns boolean

      This function can throw errors.

      Error

      LocationInUnloadedChunkError

      LocationOutOfWorldBoundariesError

    • 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.

    • 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.