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.
// 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.).
The complete block state ID
getName
public function getName () : string
Returns the printable English name of the block.
// 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.
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.
isSameState
public function isSameState ( Block $other ) : bool
Returns whether the given block has the same type and properties.
True if blocks have identical states
Item Conversion
asItem
public function asItem () : Item
Returns the block as an item. Block-only state is discarded.
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.
True if the block can be placed
canBeReplaced
public function canBeReplaced () : bool
Returns whether this block can be replaced by another block.
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.
Exact click position relative to block
Face of the block that was clicked
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.
Transaction to add blocks to
Item used to place the block
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 used to break the block
Player breaking the block
Items to be returned to player
Whether the block was successfully broken
Drops & Break Info
getBreakInfo
public function getBreakInfo () : BlockBreakInfo
Returns information about the destruction requirements of this block.
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 used to break the block
// Example: Get block drops
$drops = $block -> getDrops ( $item );
foreach ( $drops as $drop ){
$player -> sendMessage ( "Drops: " . $drop -> getName ());
}
public function getDropsForCompatibleTool ( Item $item ) : array
Returns drops when broken with a compatible tool.
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.
public function getXpDropForTool ( Item $item ) : int
Returns XP dropped when broken with the given tool.
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.
Face of the block clicked
Items to return to player
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.
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).
getLightFilter
public function getLightFilter () : int
Returns light filtered when passing through this block (0-15).
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.
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.
hasTypeTag
public function hasTypeTag ( string $tag ) : bool
Returns whether this block has the given type tag.
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!" );
}
public function getTypeTags () : array
Returns all type tags for this block.
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
VanillaBlocks :: STONE ()
VanillaBlocks :: GRANITE ()
VanillaBlocks :: DIORITE ()
VanillaBlocks :: ANDESITE ()
VanillaBlocks :: COBBLESTONE ()
VanillaBlocks :: BEDROCK ()
VanillaBlocks :: COAL_ORE ()
VanillaBlocks :: IRON_ORE ()
VanillaBlocks :: GOLD_ORE ()
VanillaBlocks :: DIAMOND_ORE ()
VanillaBlocks :: EMERALD_ORE ()
VanillaBlocks :: GRASS ()
VanillaBlocks :: DIRT ()
VanillaBlocks :: SAND ()
VanillaBlocks :: GRAVEL ()
VanillaBlocks :: CLAY ()
VanillaBlocks :: SNOW ()
VanillaBlocks :: ICE ()
VanillaBlocks :: WATER ()
VanillaBlocks :: LAVA ()
VanillaBlocks :: OAK_LOG ()
VanillaBlocks :: SPRUCE_LOG ()
VanillaBlocks :: BIRCH_LOG ()
VanillaBlocks :: JUNGLE_LOG ()
VanillaBlocks :: ACACIA_LOG ()
VanillaBlocks :: DARK_OAK_LOG ()
VanillaBlocks :: OAK_PLANKS ()
VanillaBlocks :: OAK_LEAVES ()
VanillaBlocks :: GLASS ()
VanillaBlocks :: GLOWSTONE ()
VanillaBlocks :: SEA_LANTERN ()
VanillaBlocks :: REDSTONE_LAMP ()
VanillaBlocks :: BOOKSHELF ()
VanillaBlocks :: CRAFTING_TABLE ()
VanillaBlocks :: FURNACE ()
VanillaBlocks :: CHEST ()
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 ()
);