Перейти к основному содержанию

BlockState

since: 2.2.1b89

A block state is a set of parameters applicable to any blocks in world, created to store data permanently. Each state can be requested by getting a block using BlockSource.getBlock and then calling BlockState.hasState/BlockState.getState, states can be used in ICRender.BlockState conditions, by game itself and manually by developer.

More about capabilities

Do not use numeric identifiers to save inside containers, convert them to named identifiers before, numeric ones may change with each world entrance.

Implements

Index

Constructors

constructor

  • Constructs new BlockState object from given ID and data.


    Parameters

    • id: number
    • data: number

    Returns BlockState

Properties

readonlydata

data: number

Data of the block.

readonlyid

id: number

Numeric ID of the block.

Methods

addState

  • addState(state: number, value: number): BlockState
  • Adds state to the following object.


    Parameters

    • state: number
    • value: number

    Returns BlockState

    BlockState object itself.

addStates

  • since: 2.2.1b102

    Adds states to the following object from given JS object instance.


    Parameters

    • states: object

    Returns BlockState

    BlockState object itself.

addStatesMap

  • since: 2.2.1b102

    Adds states to the following object from given map instance.


    Parameters

    • states: Map<unknown, number>

    Returns BlockState

    BlockState object itself.

equals

  • equals(object: any): boolean
  • Parameters

    • object: any

    Returns boolean

    Whether the following object is equal to given, according to different parameters.

getData

  • getData(): number
  • Returns number

    Data of the block.

getId

  • getId(): number
  • Returns number

    ID of the block.

getNamedStates

  • getNamedStates(): Map<string, number>
  • Returns Map<string, number>

    All named states from following object in java.util.Map instance.

getNamedStatesScriptable

getRuntimeId

  • getRuntimeId(): number
  • Returns number

    ID of the blockstate in runtime.

getState

  • getState(state: number): number
  • Parameters

    • state: number

    Returns number

    State of the given number if it's present in the following object.

getStates

  • getStates(): Map<number, number>
  • Returns Map<number, number>

    All states from following object in java.util.Map instance.

getStatesScriptable

hasState

  • hasState(state: number): boolean
  • Parameters

    • state: number

    Returns boolean

    Whether the state by given number is present in the following object.

isValidState

  • isValidState(): boolean
  • Returns boolean

    Whether the state is valid.

staticgetAllStates

  • getAllStates(): string[]
  • since: 2.4.0b122-4 arm64

    Returns string[]

    List of all state keys, including vanilla ones from EBlockStates. Order is randomized.

staticgetBlockStateCapacity

  • getBlockStateCapacity(state: number): number
  • since: 2.4.0b122-4 arm64

    Parameters

    • state: number

    Returns number

    Maximum capacity of state, state takes values from 0 to capacity (exclusive).

staticgetIdByName

  • getIdByName(key: string): number
  • since: 2.4.0b122-4 arm64

    Parameters

    • key: string

    Returns number

    Numeric state identifier that can be used for most block operations. Works for both new and vanilla states.

staticgetNameById

  • getNameById(state: number): string
  • since: 2.4.0b122-4 arm64

    Parameters

    • state: number

    Returns string

    Named state identifier, stable for saving in tiles and other objects in mods. Works for both new and vanilla states.

staticregisterBlockState

  • registerBlockState(key: string, capacity: number): number
  • since: 2.4.0b122-4 arm64 (until 2.4.0b123-2 arm64 identifiers could be mismatched)

    Creates a state that can be applied to any block via Block.addBlockState or SpecialType.states. Accepts any integer numeric value from 0 to capacity (exclusive). When called on existing state if new capacity is larger, it will be incremented for existing state.

    Learn how to use
    // store numeric identifier in variable, as alternative BlockState.getIdByName comes handy
    const HANDLE_TYPE_STATE = BlockState.registerBlockState("tcon_handle_type", 8);

    IDRegistry.genBlockID("tcon_workbench");
    Block.createBlock("tcon_workbench", [{ ... }], {
    // state key can be passed as alternative, vanilla EBlockStates too
    states: [HANDLE_TYPE_STATE]
    });

    Block.registerClickFunction("tcon_workbench", (coords, item, tile, playerUid) => {
    if (item.id !== VanillaItemID.stick) {
    return;
    }
    const region = BlockSource.getDefaultForActor(playerUid);
    const block = region.getBlock(coords.x, coords.y, coords.z);
    // increment to existing state, 0-5 values are applicable
    if (block.hasState(HANDLE_TYPE_STATE)) {
    const handleType = block.getState(HANDLE_TYPE_STATE) + 1;
    block.addState(HANDLE_TYPE_STATE, handleType < 6 ? handleType : 0);
    } else {
    // if state does not exist, assume that it has a default value of 0 and increment it
    block.addState(HANDLE_TYPE_STATE, 1);
    }
    region.setBlock(coords.x, coords.y, coords.z, block);
    });

    Parameters

    • key: string

      a unique name by which state can be retrieved from other mods, must not overlap with vanilla EBlockStates; if identifier is intended for your mod only, add a prefix (e.g., for "handle_type", "tcon_handle_type")

    • capacity: number

      number of states that may be applicable to block, it is recommended to use powers of two (2 for boolean values, 8 for 5-8 states inclusive, and so on)

    Returns number