BlockState
Implements
Index
Constructors
constructor
Constructs new BlockState object from given ID and data.
Parameters
id: number
data: number
Returns BlockState
Properties
readonlydata
Data of the block.
readonlyid
Numeric ID of the block.
Methods
addState
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
Parameters
object: any
Returns boolean
Whether the following object is equal to given, according to different parameters.
getData
Returns number
Data of the block.
getId
Returns number
ID of the block.
getNamedStates
Returns Map<string, number>
All named states from following object in java.util.Map instance.
getNamedStatesScriptable
Returns KeyStateScriptable
All named states from following object in JS object instance.
getRuntimeId
Returns number
ID of the blockstate in runtime.
getState
Parameters
state: number
Returns number
State of the given number if it's present in the following object.
getStates
Returns Map<number, number>
All states from following object in java.util.Map instance.
getStatesScriptable
Returns KeyStateScriptable
All states from following object in JS object instance.
hasState
Parameters
state: number
Returns boolean
Whether the state by given number is present in the following object.
isValidState
Returns boolean
Whether the state is valid.
staticgetAllStates
- since: 2.4.0b122-4 arm64
Returns string[]
List of all state keys, including vanilla ones from EBlockStates. Order is randomized.
staticgetBlockStateCapacity
- since: 2.4.0b122-4 arm64
Parameters
state: number
Returns number
Maximum capacity of state, state takes values from 0 to capacity (exclusive).
staticgetIdByName
- 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
- 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
- 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
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.
Do not use numeric identifiers to save inside containers, convert them to named identifiers before, numeric ones may change with each world entrance.