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.
// 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.
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.
Position including world reference
getWorld
public function getWorld() : World
Returns the world the entity is in.
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
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.
setMotion
public function setMotion(Vector3 $motion) : bool
Sets the entity’s velocity.
// 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.
// Example: Apply knockback
$entity->addMotion(0, 0.5, 0);
Health & Damage
getHealth
public function getHealth() : float
Returns the entity’s current health.
setHealth
public function setHealth(float $amount) : void
Sets the entity’s health.
// 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.
setMaxHealth
public function setMaxHealth(int $amount) : void
Sets the entity’s maximum health.
isAlive
public function isAlive() : bool
Returns whether the entity is alive.
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.
Last damage event, or null
public function getNameTag() : string
Returns the entity’s name tag.
public function setNameTag(string $name) : void
Sets the entity’s name tag.
// Example: Set custom name tag
$entity->setNameTag("§6Boss Monster");
$entity->setNameTagVisible(true);
$entity->setNameTagAlwaysVisible(true);
public function isNameTagVisible() : bool
Returns whether the name tag is visible.
public function setNameTagVisible(bool $value = true) : void
Sets name tag visibility.
Whether the name tag should be visible
public function isNameTagAlwaysVisible() : bool
Returns whether the name tag is always visible.
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.
Scale multiplier (1.0 = normal size)
setScale
public function setScale(float $value) : void
Sets the entity’s scale.
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.
Width and height information
getBoundingBox
public function getBoundingBox() : AxisAlignedBB
Returns the entity’s collision bounding box.
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.
// 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.
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.
The owner entity, or null
setOwningEntity
public function setOwningEntity(?Entity $owner) : void
Sets the entity’s owner.
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.
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.
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.
Fully qualified class name
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;
}
}