Skip to main content

Overview

The Entity API provides comprehensive control over all entities in the game, including mobs, projectiles, items, and custom entities.

Entity Class

The base class for all entities in PocketMine-MP. Namespace: pocketmine\entity\Entity

Core Methods

getId

public function getId() : int
Returns the entity’s unique runtime ID.
return
int
The entity runtime ID
// Example: Store entity ID for later reference
$entityId = $entity->getId();
// Later: Find entity by ID
$foundEntity = $world->getEntity($entityId);

getLocation

public function getLocation() : Location
Returns the entity’s current location.
return
Location
Location including position, yaw, and pitch
// Example: Get entity position
$location = $entity->getLocation();
$player->sendMessage("Entity at: {$location->x}, {$location->y}, {$location->z}");

getPosition

public function getPosition() : Position
Returns the entity’s position.
return
Position
Position including world reference

getWorld

public function getWorld() : World
Returns the world the entity is in.
return
World
The entity’s current world

Movement & Teleportation

teleport

public function teleport(Vector3|Position|Location $pos, ?float $yaw = null, ?float $pitch = null) : bool
Teleports the entity to a new location.
pos
Vector3|Position|Location
required
Destination position
yaw
float|null
New yaw rotation
pitch
float|null
New pitch rotation
return
bool
True if teleportation was successful
// Example: Teleport entity
$entity->teleport(new Vector3(100, 64, 100));

// Teleport with rotation
$entity->teleport($targetPos, 90.0, 0.0);

// Teleport to another world
$otherWorld = $server->getWorldManager()->getWorldByName("world_nether");
if($otherWorld !== null){
    $entity->teleport(new Position(0, 64, 0, $otherWorld));
}

getMotion

public function getMotion() : Vector3
Returns the entity’s current velocity.
return
Vector3
Velocity vector

setMotion

public function setMotion(Vector3 $motion) : bool
Sets the entity’s velocity.
motion
Vector3
required
New velocity vector
// Example: Launch entity upward
$entity->setMotion(new Vector3(0, 2, 0));

// Example: Push entity horizontally
$entity->setMotion(new Vector3(1, 0, 1));

addMotion

public function addMotion(float $x, float $y, float $z) : void
Adds to the entity’s current velocity.
x
float
required
X velocity to add
y
float
required
Y velocity to add
z
float
required
Z velocity to add
// Example: Apply knockback
$entity->addMotion(0, 0.5, 0);

Health & Damage

getHealth

public function getHealth() : float
Returns the entity’s current health.
return
float
Current health points

setHealth

public function setHealth(float $amount) : void
Sets the entity’s health.
amount
float
required
Health to set
// Example: Heal entity to full health
$entity->setHealth($entity->getMaxHealth());

// Example: Set specific health
$entity->setHealth(10.0);

getMaxHealth

public function getMaxHealth() : int
Returns the entity’s maximum health.
return
int
Maximum health points

setMaxHealth

public function setMaxHealth(int $amount) : void
Sets the entity’s maximum health.
amount
int
required
Maximum health to set

isAlive

public function isAlive() : bool
Returns whether the entity is alive.
return
bool
True if health > 0

attack

public function attack(EntityDamageEvent $source) : void
Applies damage to the entity.
source
EntityDamageEvent
required
Damage event
use pocketmine\event\entity\EntityDamageEvent;

// Example: Deal custom damage
$damage = new EntityDamageEvent($entity, EntityDamageEvent::CAUSE_CUSTOM, 5.0);
$entity->attack($damage);

heal

public function heal(EntityRegainHealthEvent $source) : void
Heals the entity.
source
EntityRegainHealthEvent
required
Heal event

kill

public function kill() : void
Kills the entity instantly.
// Example: Kill entity
if($entity->isAlive()){
    $entity->kill();
}

getLastDamageCause

public function getLastDamageCause() : ?EntityDamageEvent
Returns the last damage cause.
return
EntityDamageEvent|null
Last damage event, or null

Name Tags

getNameTag

public function getNameTag() : string
Returns the entity’s name tag.
return
string
The name tag text

setNameTag

public function setNameTag(string $name) : void
Sets the entity’s name tag.
name
string
required
Name tag text to set
// Example: Set custom name tag
$entity->setNameTag("§6Boss Monster");
$entity->setNameTagVisible(true);
$entity->setNameTagAlwaysVisible(true);

isNameTagVisible

public function isNameTagVisible() : bool
Returns whether the name tag is visible.

setNameTagVisible

public function setNameTagVisible(bool $value = true) : void
Sets name tag visibility.
value
bool
default:"true"
Whether the name tag should be visible

isNameTagAlwaysVisible

public function isNameTagAlwaysVisible() : bool
Returns whether the name tag is always visible.

setNameTagAlwaysVisible

public function setNameTagAlwaysVisible(bool $value = true) : void
Sets whether the name tag is always visible.

Scale & Size

getScale

public function getScale() : float
Returns the entity’s scale multiplier.
return
float
Scale multiplier (1.0 = normal size)

setScale

public function setScale(float $value) : void
Sets the entity’s scale.
value
float
required
Scale multiplier (must be > 0)
// Example: Make entity twice as large
$entity->setScale(2.0);

// Example: Make entity half size
$entity->setScale(0.5);

getSize

public function getSize() : EntitySizeInfo
Returns the entity’s size information.
return
EntitySizeInfo
Width and height information

getBoundingBox

public function getBoundingBox() : AxisAlignedBB
Returns the entity’s collision bounding box.
return
AxisAlignedBB
The entity’s bounding box

Fire & Burning

isOnFire

public function isOnFire() : bool
Returns whether the entity is on fire.

setOnFire

public function setOnFire(int $seconds) : void
Sets the entity on fire.
seconds
int
required
Duration in seconds
// Example: Set entity on fire for 5 seconds
$entity->setOnFire(5);

getFireTicks

public function getFireTicks() : int
Returns remaining fire ticks.

setFireTicks

public function setFireTicks(int $ticks) : void
Sets fire duration in ticks.

extinguish

public function extinguish() : void
Extinguishes the entity.
// Example: Put out fire
if($entity->isOnFire()){
    $entity->extinguish();
}

Visibility & Properties

isInvisible

public function isInvisible() : bool
Returns whether the entity is invisible.

setInvisible

public function setInvisible(bool $value = true) : void
Sets entity invisibility.
value
bool
default:"true"
Whether the entity should be invisible
// Example: Make entity invisible
$entity->setInvisible(true);

isSilent

public function isSilent() : bool
Returns whether the entity makes sounds.

setSilent

public function setSilent(bool $value = true) : void
Sets whether the entity is silent.

canClimb

public function canClimb() : bool
Returns whether the entity can climb ladders.

setCanClimb

public function setCanClimb(bool $value = true) : void
Sets whether the entity can climb.

Ownership & Targeting

getOwningEntity

public function getOwningEntity() : ?Entity
Returns the entity’s owner.
return
Entity|null
The owner entity, or null

setOwningEntity

public function setOwningEntity(?Entity $owner) : void
Sets the entity’s owner.
owner
Entity|null
required
New owner, or null to remove
// Example: Set projectile owner
use pocketmine\entity\projectile\Arrow;

if($arrow instanceof Arrow){
    $arrow->setOwningEntity($player);
}

getTargetEntity

public function getTargetEntity() : ?Entity
Returns the entity’s target.

setTargetEntity

public function setTargetEntity(?Entity $target) : void
Sets the entity’s target.

Lifecycle

close

public function close() : void
Removes the entity from the world.
// Example: Remove entity
if($entity->isAlive()){
    $entity->close();
}

isClosed

public function isClosed() : bool
Returns whether the entity has been closed.

isFlaggedForDespawn

public function isFlaggedForDespawn() : bool
Returns whether the entity is flagged for removal.

flagForDespawn

public function flagForDespawn() : void
Flags the entity for removal on next tick.

canSaveWithChunk

public function canSaveWithChunk() : bool
Returns whether the entity saves with chunks.

setCanSaveWithChunk

public function setCanSaveWithChunk(bool $value) : void
Sets whether the entity saves with chunks.

NBT Data

saveNBT

public function saveNBT() : CompoundTag
Saves the entity to NBT.
return
CompoundTag
NBT data for the entity

Living Class

Extends Entity with living entity features. Namespace: pocketmine\entity\Living

Additional Methods

getEffects

public function getEffects() : EffectManager
Returns the effect manager for this entity.
return
EffectManager
The effect manager
use pocketmine\entity\effect\VanillaEffects;
use pocketmine\entity\effect\EffectInstance;

// Example: Apply potion effect
$effect = new EffectInstance(VanillaEffects::SPEED(), 20 * 60, 1); // 1 minute, level 2
$entity->getEffects()->add($effect);

hasLineOfSight

public function hasLineOfSight(Entity $entity) : bool
Returns whether this entity can see another entity.

getArmorInventory

public function getArmorInventory() : ArmorInventory
Returns the armor inventory.

Human Class

Extends Living with player-like features. Namespace: pocketmine\entity\Human

Additional Methods

getInventory

public function getInventory() : PlayerInventory
Returns the player inventory.

getSkin

public function getSkin() : Skin
Returns the entity’s skin.

setSkin

public function setSkin(Skin $skin) : void
Sets the entity’s skin.

EntityFactory

Factory for creating and registering entities. Namespace: pocketmine\entity\EntityFactory

Methods

getInstance

public static function getInstance() : EntityFactory
Returns the singleton instance.

register

public function register(string $className, callable $creationFunc, array $saveNames) : void
Registers a custom entity type.
className
string
required
Fully qualified class name
creationFunc
callable
required
Entity creation callback
saveNames
string[]
required
NBT save identifiers
use pocketmine\entity\EntityFactory;
use pocketmine\entity\EntityDataHelper;
use pocketmine\entity\Location;
use pocketmine\nbt\tag\CompoundTag;

// Example: Register custom entity
EntityFactory::getInstance()->register(
    CustomEntity::class,
    function(Location $location, CompoundTag $nbt) : CustomEntity{
        return new CustomEntity($location, $nbt);
    },
    ['CustomEntity', 'my_plugin:custom_entity']
);

Common Usage Patterns

Spawning Entities

use pocketmine\entity\Location;
use pocketmine\entity\EntityFactory;
use pocketmine\nbt\tag\CompoundTag;

// Spawn a basic entity
$location = new Location(100, 64, 100, $world, 0, 0);
$nbt = CompoundTag::create();
$entity = EntityFactory::getInstance()->create(EntityTypeIds::ZOMBIE, $location, $nbt);
if($entity !== null){
    $entity->spawnToAll();
}

Finding Nearby Entities

use pocketmine\math\AxisAlignedBB;

// Find entities within radius
$radius = 10;
$bb = new AxisAlignedBB(
    $pos->x - $radius, $pos->y - $radius, $pos->z - $radius,
    $pos->x + $radius, $pos->y + $radius, $pos->z + $radius
);

$nearbyEntities = $world->getNearbyEntities($bb);
foreach($nearbyEntities as $entity){
    if($entity instanceof Living){
        // Do something with living entities
    }
}

Applying Effects

use pocketmine\entity\Living;
use pocketmine\entity\effect\VanillaEffects;
use pocketmine\entity\effect\EffectInstance;

if($entity instanceof Living){
    // Add speed effect
    $entity->getEffects()->add(
        new EffectInstance(VanillaEffects::SPEED(), 20 * 30, 1) // 30 seconds, level 2
    );
    
    // Add multiple effects
    $entity->getEffects()->add(
        new EffectInstance(VanillaEffects::REGENERATION(), 20 * 10, 0)
    );
    
    // Remove all effects
    $entity->getEffects()->clear();
}

Custom Entity Behavior

use pocketmine\entity\Living;
use pocketmine\entity\EntitySizeInfo;
use pocketmine\nbt\tag\CompoundTag;

class CustomMob extends Living{
    
    protected function getInitialSizeInfo() : EntitySizeInfo{
        return new EntitySizeInfo(1.8, 0.6); // height, width
    }
    
    protected function getInitialDragMultiplier() : float{
        return 0.02;
    }
    
    protected function getInitialGravity() : float{
        return 0.08;
    }
    
    public function getName() : string{
        return "Custom Mob";
    }
    
    protected function entityBaseTick(int $tickDiff = 1) : bool{
        $hasUpdate = parent::entityBaseTick($tickDiff);
        
        // Custom behavior every tick
        if($this->isAlive()){
            // AI logic here
        }
        
        return $hasUpdate;
    }
}

Build docs developers (and LLMs) love