Readonly
dimensionThis property can't be read in early-execution mode.
Readonly
isThis 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);
});
}
});
Readonly
isReturns 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't be read in early-execution mode.
Readonly
isReturns 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.)
This property can't be read in early-execution mode.
Readonly
isThis property can't be read in early-execution mode.
Readonly
localizationThis property can't be read in early-execution mode.
Readonly
locationThis property can't be read in early-execution mode.
Readonly
permutationThis property can't be read in early-execution mode.
Readonly
typeThis property can't be read in early-execution mode.
Readonly
typeIdentifier of the type of block for this block. Warning: Vanilla block names can be changed in future releases, try using 'Block.matches' instead for block comparison.
This property can't be read in early-execution mode.
Readonly
xThis property can't be read in early-execution mode.
Readonly
yThis property can't be read in early-execution mode.
Readonly
zThis property can't be read in early-execution mode.
Optional
steps: numberNumber of steps above to step before returning. Defaults to: 1
Returns the Block above this block (positive in the Y direction).
This function can't be called in early-execution mode.
Optional
steps: numberNumber of steps below to step before returning. Defaults to: 1
Returns the Block below this block (negative in the Y direction).
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);
});
Returns the Vector3 of the center of this block on the X and Z axis.
This function can't be called in early-execution mode.
The type of liquid this function should be called for.
Whether this block is removed when touched by liquid.
This function can't be called in early-execution mode.
The type of liquid this function should be called for.
Whether this block can have a liquid placed over it.
This function can't be called in early-execution mode.
Returns the Vector3 of the center of this block on the X, Y, and Z axis.
This function can't be called in early-execution mode.
Optional
steps: numberNumber of steps to the east to step before returning. Defaults to: 1
Returns the Block to the east of this block (positive in the X direction).
This function can't be called in early-execution mode.
The identifier of the component (e.g., 'minecraft:inventory'). If no namespace prefix is specified, 'minecraft:' is assumed. Available component IDs are those in the BlockComponentTypes enum and custom component IDs registered with the BlockComponentRegistry.
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.
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})`
);
});
});
Optional
amount: numberNumber of instances of this block to place in the item stack. Defaults to: 1
Optional
withData: booleanWhether additional data facets of the item stack are included. Defaults to: false
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.
This function can't be called in early-execution mode.
Rc
The brightness level on the block.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Returns undefined if redstone power is not applicable to this block.
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;
}
Rc
The brightness level on the block.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
The list of tags that the block has.
This function can't be called in early-execution mode.
Tag to check for.
Returns true
if the permutation of this block has the tag,
else false
.
import { DimensionLocation } from "@minecraft/server";
function checkBlockTags(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
// Fetch the block
const block = targetLocation.dimension.getBlock(targetLocation);
// check that the block is loaded
if (block) {
log(`Block is dirt: ${block.hasTag("dirt")}`);
log(`Block is wood: ${block.hasTag("wood")}`);
log(`Block is stone: ${block.hasTag("stone")}`);
}
}
This function can't be called in early-execution mode.
The type of liquid this function should be called for.
Whether this block stops liquid from flowing.
This function can't be called in early-execution mode.
Whether liquid can flow into the block from the provided direction, or flow out from the provided direction when liquid is placed into it with a bucket
Returns whether liquid can flow into the block from the provided direction, or flow out from the provided direction when liquid is placed into it with a bucket.
This function can't be called in early-execution mode.
The type of liquid this function should be called for.
Whether this block is removed and spawns its item when touched by liquid.
This function can't be called in early-execution mode.
Block type identifier to match this API against.
Optional
states: Record<string, string | number | boolean>Optional set of block states to test this block against.
Returns true if the block matches the specified criteria.
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);
}
Optional
steps: numberNumber of steps to the north to step before returning. Defaults to: 1
Returns the Block to the north of this block (negative in the Z direction).
This function can't be called in early-execution mode.
The offset vector. For example, an offset of 0, 1, 0 will return the block above the current 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.)
This function can't be called in early-execution mode.
Permutation that contains a set of property states for the Block.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Identifier of the type of block to apply - for example, minecraft:powered_repeater.
This function can't be called in read-only mode.
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);
});
true if the block should have water within it.
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't be called in early-execution mode.
Optional
steps: numberNumber of steps to the south to step before returning. Defaults to: 1
Returns the Block to the south of this block (positive in the Z direction).
This function can't be called in early-execution mode.
Optional
steps: numberNumber of steps to the west to step before returning. Defaults to: 1
Returns the Block to the west of this block (negative in the X direction).
This function can't be called in early-execution mode.
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.
Example: createWall.ts