Readonly
dimensionReadonly
isimport { 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
isReadonly
Beta
isReturns 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).
import { world } from "@minecraft/server";
// Subscribe to the event that triggers when a player places a block
world.afterEvents.playerPlaceBlock.subscribe((eventData) => {
// Destructure the block object from the event data
const { block } = eventData;
// Check if the placed block is solid
if (block.isSolid) {
// Get the coordinates of the block's location
let { x, y, z } = block.location;
// Create a string of the coordinates in the format "x, y, z"
let coordinates = [x, y, z].join(", ");
// Send a message to the world indicating the location of the placed solid block
world.sendMessage("You placed a solid block at location: " + coordinates);
}
});
Readonly
isReadonly
isReadonly
localizationReadonly
locationReadonly
permutationReadonly
typeReadonly
typeReadonly
xReadonly
yReadonly
zOptional
steps: numberNumber of steps below to step before returning. Defaults to: 1
Returns the Block below this block (negative in the Y direction).
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);
});
The type of liquid this function should be called for.
Whether this block is removed when touched by liquid.
The type of liquid this function should be called for.
Whether this block can have a liquid placed over it.
Beta
Block type or block permutation to check placement for.
Optional
faceToPlaceOn: DirectionOptional specific face of this block to check placement against.
Returns true
if the block type or permutation can be
placed on this block, else false
.
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.
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.
Returns undefined if redstone power is not applicable to this block.
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;
}
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")}`);
}
}
The type of liquid this function should be called for.
Whether this block stops liquid from flowing.
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
The type of liquid this function should be called for.
Whether this block is removed and spawns its item when touched by liquid.
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.
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);
}
Permutation that contains a set of property states for the Block.
Identifier of the type of block to apply - for example, minecraft:powered_repeater.
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);
});
Beta
Permutation that contains a set of property states for the Block.
Returns true
if the block permutation data was
successfully set, else false
.
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