Mob spawning & AI 5.46.0
VanillaMine spawns mobs like Bedrock. Plugins can see why a mob exists, stop spawns before they happen, and decide which targets mobs attack. The API follows Paper's naming so it is familiar.
API: SpawnReason, Living::getSpawnReason(), MobPreSpawnEvent, EntityNaturalSpawnEvent, EntityTargetEvent.
Why a mob exists
use pocketmine\entity\SpawnReason;
$reason = $entity->getSpawnReason(); // e.g. SpawnReason::NATURAL, SPAWN_EGG, SPAWNER, BREEDING, RAID…Values: default, natural, structure, wandering_trader, village_cat, patrol, raid, spawn_egg, spawner, breeding, custom. SpawnReason::NATURAL_REASONS lists the natural ones. Mobs of plugin classes are custom; plugins may set any string with setSpawnReason(). The reason is saved with the mob.
Stopping spawns
MobPreSpawnEvent fires before the natural spawner creates a mob or a herd. Nothing exists yet, so cancelling is free: use it for spawn control (regions, worlds, per-type limits).
use pocketmine\event\entity\MobPreSpawnEvent;
public function onPreSpawn(MobPreSpawnEvent $event) : void{
if($event->getEntityIdentifier() === "minecraft:phantom"){
$event->cancel();
}
// $event->getPosition(), $event->getReason()
}EntityNaturalSpawnEvent fires after the mob was created and before players see it (natural spawning, structures, wandering traders, village cats). Cancelling removes the mob. Use it when you need the entity itself.
Choosing targets
EntityTargetEvent fires when a mob picks a new attack target. Cancel it to keep the mob without a target, or change the target.
use pocketmine\event\entity\EntityTargetEvent;
public function onTarget(EntityTargetEvent $event) : void{
if($event->getTarget() instanceof MyNpc){
$event->cancel();
}
}Plugins that spawn their own mobs
| Setting | Default | Effect |
|---|---|---|
mobs.spawning.count-all-mobs-for-spawning | true | Every non-persistent mob counts towards the population caps, including spawn eggs, spawners, breeding and plugin mobs. false = only natural ones (Paper's behaviour with false). |
mobs.spawning.spawn-limits.<type> | -1 | Caps per 9×9 chunks for animal, monster, ambient, water_animal, pillager. -1 = vanilla (4, 8 surface / 16 underground, 2 underground, 36, 8). A value replaces both surface and underground caps. |
mobs.ignore-plugin-npcs | true | Mobs never target plugin NPCs: living entities of plugin classes that have a name, stand still for clients or are not saved with the world. |
mobs.spawning.enabled | true | false turns natural spawning off and leaves spawning to your plugin. |
Persistent mobs and plugin NPCs never count towards the caps.
