Skip to main content

Overview

The Block API provides comprehensive control over block types, states, properties, and interactions in PocketMine-MP.

Block Class

The base class for all block types. Namespace: pocketmine\block\Block

Core Methods

getTypeId

public function getTypeId() : int
Returns the type ID that identifies this type of block.
return
int
The block type ID
// Example: Check block type
if($block->getTypeId() === BlockTypeIds::STONE){
    $player->sendMessage("This is stone!");
}

getStateId

public function getStateId() : int
Returns the full blockstate ID including all properties (facing, powered, etc.).
return
int
The complete block state ID

getName

public function getName() : string
Returns the printable English name of the block.
return
string
The block’s name
// Example: Display block name
$player->sendMessage("You're looking at: " . $block->getName());

getPosition

public function getPosition() : Position
Returns the position of this block in the world.
return
Position
The block’s position including world reference
// Example: Get block position
$pos = $block->getPosition();
$player->sendMessage("Block at: {$pos->x}, {$pos->y}, {$pos->z}");

State Comparison

hasSameTypeId

public function hasSameTypeId(Block $other) : bool
Returns whether the given block has the same type ID as this one.
other
Block
required
Block to compare with
return
bool
True if type IDs match

isSameState

public function isSameState(Block $other) : bool
Returns whether the given block has the same type and properties.
other
Block
required
Block to compare with
return
bool
True if blocks have identical states

Item Conversion

asItem

public function asItem() : Item
Returns the block as an item. Block-only state is discarded.
return
Item
Item representation of the block
// Example: Give player block as item
$item = $block->asItem();
$player->getInventory()->addItem($item);

Placement & Breaking

canBePlaced

public function canBePlaced() : bool
Returns whether this block can be placed when obtained as an item.
return
bool
True if the block can be placed

canBeReplaced

public function canBeReplaced() : bool
Returns whether this block can be replaced by another block.
return
bool
True if the block can be replaced

canBePlacedAt

public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool
Returns whether this block can replace the given block.
blockReplace
Block
required
Block to be replaced
clickVector
Vector3
required
Exact click position relative to block
face
int
required
Face of the block that was clicked
isClickedBlock
bool
required
Whether this is the directly clicked block

place

public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool
Generates a block transaction for placing this block.
tx
BlockTransaction
required
Transaction to add blocks to
item
Item
required
Item used to place the block
blockReplace
Block
required
Block being replaced
blockClicked
Block
required
Block that was clicked
face
int
required
Face of clicked block
clickVector
Vector3
required
Exact click position
player
Player|null
Player placing the block
return
bool
Whether the placement should proceed

onPostPlace

public function onPostPlace() : void
Called immediately after the block has been placed in the world.

onBreak

public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool
Called when the block is broken.
item
Item
required
Item used to break the block
player
Player|null
Player breaking the block
returnedItems
Item[]
Items to be returned to player
return
bool
Whether the block was successfully broken

Drops & Break Info

getBreakInfo

public function getBreakInfo() : BlockBreakInfo
Returns information about the destruction requirements of this block.
return
BlockBreakInfo
Object containing hardness, tool requirements, etc.
// Example: Check break info
$breakInfo = $block->getBreakInfo();
$hardness = $breakInfo->getHardness();
$toolType = $breakInfo->getToolType();

getDrops

public function getDrops(Item $item) : array
Returns items dropped when the block is broken.
item
Item
required
Item used to break the block
return
Item[]
Array of items to drop
// Example: Get block drops
$drops = $block->getDrops($item);
foreach($drops as $drop){
    $player->sendMessage("Drops: " . $drop->getName());
}

getDropsForCompatibleTool

public function getDropsForCompatibleTool(Item $item) : array
Returns drops when broken with a compatible tool.

getDropsForIncompatibleTool

public function getDropsForIncompatibleTool(Item $item) : array
Returns drops when broken with an incompatible tool.

getSilkTouchDrops

public function getSilkTouchDrops(Item $item) : array
Returns drops when broken with Silk Touch.

getXpDropForTool

public function getXpDropForTool(Item $item) : int
Returns XP dropped when broken with the given tool.
return
int
Amount of XP to drop

isAffectedBySilkTouch

public function isAffectedBySilkTouch() : bool
Returns whether Silk Touch affects this block’s drops.

Interaction

onInteract

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool
Called when a player interacts with this block.
item
Item
required
Item used to interact
face
int
required
Face of the block clicked
clickVector
Vector3
required
Exact click position
player
Player|null
Player interacting
returnedItems
Item[]
Items to return to player
return
bool
Whether an action took place

onAttack

public function onAttack(Item $item, int $face, ?Player $player = null) : bool
Called when a player attacks (left-clicks) this block.
return
bool
Whether an action took place (prevents breaking if true)

Updates

onNearbyBlockChange

public function onNearbyBlockChange() : void
Called when this block or an adjacent block changes state.

ticksRandomly

public function ticksRandomly() : bool
Returns whether random block updates will be done on this block.

onRandomTick

public function onRandomTick() : void
Called when this block receives a random tick.

onScheduledUpdate

public function onScheduledUpdate() : void
Called when this block is updated by the delayed update scheduler.

Properties

getLightLevel

public function getLightLevel() : int
Returns the amount of light emitted by this block (0-15).
return
int
Light level (0-15)

getLightFilter

public function getLightFilter() : int
Returns light filtered when passing through this block (0-15).
return
int
Light filter value (0-15)

blocksDirectSkyLight

public function blocksDirectSkyLight() : bool
Returns whether this block blocks direct sky light.

isTransparent

public function isTransparent() : bool
Returns whether this block allows light to pass through.

isSolid

public function isSolid() : bool
Returns whether this block is considered “solid”.

getFrictionFactor

public function getFrictionFactor() : float
Returns a multiplier for entity velocity when moving on this block.
return
float
Friction factor (0.0-1.0, higher = more slippery)

canBeFlowedInto

public function canBeFlowedInto() : bool
Returns whether liquid can flow into this block’s cell.

canClimb

public function canClimb() : bool
Returns whether entities can climb this block.

Type Tags

hasTypeTag

public function hasTypeTag(string $tag) : bool
Returns whether this block has the given type tag.
tag
string
required
Tag to check for
return
bool
True if the block has this tag
use pocketmine\block\BlockTypeTags;

// Example: Check if block is dirt-like
if($block->hasTypeTag(BlockTypeTags::DIRT)){
    $player->sendMessage("This is dirt-like!");
}

getTypeTags

public function getTypeTags() : array
Returns all type tags for this block.
return
string[]
Array of type tag strings

VanillaBlocks Helper

Provides easy access to vanilla block types. Namespace: pocketmine\block\VanillaBlocks

Usage

use pocketmine\block\VanillaBlocks;

// Get block instances
$stone = VanillaBlocks::STONE();
$grass = VanillaBlocks::GRASS();
$oakLog = VanillaBlocks::OAK_LOG();
$diamond = VanillaBlocks::DIAMOND_BLOCK();
$air = VanillaBlocks::AIR();

// Place blocks
$world->setBlock($pos, VanillaBlocks::STONE());
$world->setBlock($pos->add(1, 0, 0), VanillaBlocks::GRASS());

Common Blocks


BlockTypeIds Constants

Numeric constants for block type IDs. Namespace: pocketmine\block\BlockTypeIds
use pocketmine\block\BlockTypeIds;

// Compare block types
if($block->getTypeId() === BlockTypeIds::STONE){
    // This is stone
}

if($block->getTypeId() === BlockTypeIds::GRASS){
    // This is grass
}

Common Usage Patterns

Checking Block Type

use pocketmine\block\BlockTypeIds;
use pocketmine\block\VanillaBlocks;

// Method 1: Using type ID
if($block->getTypeId() === BlockTypeIds::STONE){
    $player->sendMessage("This is stone!");
}

// Method 2: Using hasSameTypeId
if($block->hasSameTypeId(VanillaBlocks::STONE())){
    $player->sendMessage("This is stone!");
}

// Method 3: Using instanceof (for specific block classes)
use pocketmine\block\Chest;

if($block instanceof Chest){
    $player->sendMessage("This is a chest!");
}

Custom Block Placement

use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\block\VanillaBlocks;

public function onBlockPlace(BlockPlaceEvent $event) : void{
    $player = $event->getPlayer();
    $block = $event->getBlock();
    
    // Prevent placing certain blocks
    if($block->hasSameTypeId(VanillaBlocks::TNT())){
        $event->cancel();
        $player->sendMessage("You cannot place TNT here!");
    }
}

Working with Block States

use pocketmine\block\Door;
use pocketmine\math\Facing;

// Example: Working with doors
$door = VanillaBlocks::OAK_DOOR();
if($door instanceof Door){
    $door->setFacing(Facing::NORTH);
    $door->setOpen(true);
    $world->setBlock($pos, $door);
}

Reading Block NBT Data

use pocketmine\block\tile\Chest;

// Example: Access chest inventory
$tile = $world->getTile($pos);
if($tile instanceof Chest){
    $inventory = $tile->getInventory();
    foreach($inventory->getContents() as $item){
        $player->sendMessage("Item: " . $item->getName());
    }
}

Filling a Region

use pocketmine\math\Vector3;
use pocketmine\block\VanillaBlocks;

public function fill(World $world, Vector3 $pos1, Vector3 $pos2, Block $block) : void{
    $minX = (int) min($pos1->x, $pos2->x);
    $maxX = (int) max($pos1->x, $pos2->x);
    $minY = (int) min($pos1->y, $pos2->y);
    $maxY = (int) max($pos1->y, $pos2->y);
    $minZ = (int) min($pos1->z, $pos2->z);
    $maxZ = (int) max($pos1->z, $pos2->z);
    
    for($x = $minX; $x <= $maxX; $x++){
        for($y = $minY; $y <= $maxY; $y++){
            for($z = $minZ; $z <= $maxZ; $z++){
                $world->setBlock(new Vector3($x, $y, $z), $block);
            }
        }
    }
}

// Usage
$this->fill(
    $world,
    new Vector3(0, 64, 0),
    new Vector3(10, 74, 10),
    VanillaBlocks::GLASS()
);

Build docs developers (and LLMs) love