Minecraft Scripting API

Please note: Some examples on this page may be outdated or may not work as expected.

Block Interactions & Events

Interact with blocks, detect changes, and create custom block behaviors.

Understanding Block Events

Blocks are the foundation of Minecraft. Block events let you detect when they change and react. You can prevent certain blocks from being mined, auto-replace blocks, create chain reactions, or build structures automatically.

Key concept: Block events tell you what changed, and you can modify properties or prevent the action.

Block Breaking & Placement

When a player breaks or places a block, these events fire. You can cancel the action, detect what they broke, or create custom behaviors:

import { world } from "@minecraft/server";

// When a block is broken
world.afterEvents.blockBreak.subscribe((event) => {
  const block = event.brokenBlockPermutation;
  const player = event.player;
  
  // Prevent breaking specific blocks
  if (block.type.id === "minecraft:obsidian") {
    player.sendMessage("§cYou cannot break obsidian!");
    return;
  }
  
  console.log(`${player.nameTag} broke ${block.type.id}`);
});

// When a block is placed
world.afterEvents.blockPlace.subscribe((event) => {
  const block = event.block;
  const player = event.player;
  
  player.sendMessage(`§aPlaced ${block.typeId}`);
  
  // Create structures on placement
  if (block.typeId === "minecraft:emerald_block") {
    createPillar(block.location);
  }
});

function createPillar(location) {
  const dim = world.getDimension("overworld");
  for (let i = 1; i <= 5; i++) {
    const blockAbove = dim.getBlock({
      x: location.x,
      y: location.y + i,
      z: location.z
    });
    blockAbove.setType("minecraft:gold_block");
  }
}

Block Explosion & Redstone

world.afterEvents.explosion.subscribe((event) => {
  const explosionCenter = event.source;
  const affectedBlocks = event.getImpactedBlocks();
  
  console.log(`Explosion at ${explosionCenter}`);
  console.log(`${affectedBlocks.length} blocks affected`);
});

// Detect redstone changes
world.afterEvents.blockExplode.subscribe((event) => {
  const block = event.block;
  
  if (block.typeId === "minecraft:redstone_block") {
    console.log("Redstone activated!");
  }
});

Block Properties

// Read block properties
const block = world.getDimension("overworld").getBlock({ x: 0, y: 64, z: 0 });

// Get all properties
const permutation = block.permutation;
const properties = permutation.getAllProperties();

for (const prop of properties) {
  console.log(`${prop.name}: ${prop.value}`);
}

// Common properties
const isOpen = block.permutation.getProperty("open"); // For doors
const age = block.permutation.getProperty("age"); // For crops
const powered = block.permutation.getProperty("powered"); // For redstone

// Set properties
const newPermutation = block.permutation
  .withProperty("open", true)
  .withProperty("age", 7);

block.setPermutation(newPermutation);

Block Ticking

// Detect blocks that need updates
const tickingAreas = [];

function createTickingBlock(location) {
  const dim = world.getDimension("overworld");
  
  // Continuously update block behavior
  const interval = setInterval(() => {
    const block = dim.getBlock(location);
    
    // Custom logic every tick
    if (block.typeId === "minecraft:furnace") {
      console.log("Furnace is active");
    }
  }, 50); // 50ms interval
  
  tickingAreas.push(interval);
}

Sculpting & Custom Shapes

function createSphere(center, radius, blockType) {
  const dim = world.getDimension("overworld");
  
  for (let x = center.x - radius; x <= center.x + radius; x++) {
    for (let y = center.y - radius; y <= center.y + radius; y++) {
      for (let z = center.z - radius; z <= center.z + radius; z++) {
        const distance = Math.sqrt(
          Math.pow(x - center.x, 2) +
          Math.pow(y - center.y, 2) +
          Math.pow(z - center.z, 2)
        );
        
        if (distance <= radius) {
          const block = dim.getBlock({ x, y, z });
          block.setType(blockType);
        }
      }
    }
  }
}

// Usage
createSphere({ x: 0, y: 64, z: 0 }, 10, "minecraft:diamond_block");

Crop Detection

world.afterEvents.blockBreak.subscribe((event) => {
  const block = event.brokenBlockPermutation;
  
  // Detect crop harvesting
  if (block.type.id.includes("wheat")) {
    const age = block.getProperty("age");
    
    if (age === 7) {
      console.log("Fully grown wheat harvested");
    } else {
      console.log("Premature wheat broken");
    }
  }
});
Navigation