ReadonlydimensionReadonlyisThis 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);
});
}
});
ReadonlyisThis property can't be read in early-execution mode.
ReadonlyisReadonlyisThis property can't be read in early-execution mode.
ReadonlylocalizationThis property can't be read in early-execution mode.
ReadonlylocationThis property can't be read in early-execution mode.
ReadonlypermutationThis property can't be read in early-execution mode.
ReadonlytypeThis property can't be read in early-execution mode.
ReadonlytypeThis property can't be read in early-execution mode.
ReadonlyxReadonlyyReadonlyzOptionalsteps: numberDefaults to: 1
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Optionalsteps: numberDefaults to: 1
This function can't be called in read-only mode.
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);
});
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Optionalsteps: numberDefaults to: 1
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
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})`
);
});
});
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Optionalamount: numberDefaults to: 1
OptionalwithData: booleanThis function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
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;
}
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Optionalstates: Record<string, string | number | boolean>This function can't be called in read-only mode.
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);
}
Optionalsteps: numberDefaults to: 1
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
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);
});
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Optionalsteps: numberDefaults to: 1
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Optionalsteps: numberDefaults to: 1
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
Example: createWall.ts