AI goals
Give mobs goals and targets, move them to a place, switch their AI off and handle mobs that turn into other mobs.
Add a built-in goal
Get a Mob from an entity with entity.as_mob(), then call mob.add_ai_goal(priority, goal). A lower priority number is more important. The goals are values of BuiltinAiGoal:
Goal | What the mob does |
|---|---|
| Swims. |
| Walks around. |
| Attacks its target up close. |
| Looks at players within the range. |
| Looks around. |
| Runs away from danger. |
mob.clear_ai_goals() removes every goal of the mob, its own vanilla goals too. Add only the goals your feature needs, and do not remove normal mob behaviour just to save work.
A vindicator you name "Johnny" with set_custom_name behaves as Johnny: it attacks every creature near it.
Give a mob a target
mob.set_target(Some(entity)) sets the target and mob.get_target() reads it.
EntityTargetEvent fires when a mob takes, changes or loses its target, an enderman too. Cancel it and nothing changes: the mob keeps its target. An enderman also keeps its screaming and speed. A target your handler names instead is taken when the mob may attack it.
Move a mob
navigate_to_pos(pos, speed)andnavigate_to_entity(target, speed)walk the mob there.is_navigating(),has_reached_destination(),set_navigation_speed(speed)andstop_navigation()follow and control the walk.can_reach(pos, max_distance)asks whether there is a way.look_at(pos)andlook_at_entity(target)turn its head.
set_pathfinding_malus(node_type, malus) changes the cost of a kind of block for this mob: a negative value excludes it, zero adds no cost and a positive value makes the mob avoid it. The setting is kept in memory, so set it again when the mob is loaded from disk.
Switch a mob's AI off
mob.set_aware(false): the mob does nothing on its own (no goals, targeting, pathfinding or looking), but it still falls and is pushed. This is saved with the mob.mob.set_ai_disabled(true): switches the AI off. Useset_awarewhen the mob should still fall and be pushed.
Mobs that turn into other mobs
A mob that turns into another one (a pig struck by lightning, a drowning zombie, a sheared mooshroom, a growing tadpole) keeps its rider, its seat in a boat or on a mount, and its lead. These conversions are events you can handle:
- The creature spawn event fires for the new mob. Its spawn reason text is
lightning,drowned,sheared,metamorphosis,cured,piglin_zombified,slime_split,infectionorfrozen. - Cancel
EntityTransformEventand the old mob stays as it was. - Cancel the creature spawn event of a conversion and neither mob stays: the old one is removed too, as on Paper.
Special cases:
- Lightning. Cancel
PigZapEventto keep a pig. For a villager, cancel the extension event"entity-zap"(events::ENTITY_ZAP;event.zap(),event.zap_replacement()) orEntityTransformEvent(reasonlightning); the bolt then hits the villager. - Slimes and magma cubes.
SlimeSplitEventfires first (change the number of children or cancel), then one transform event with the reasonsplit, then a creature spawn event for each child. - Villagers killed by zombies. The conversion reason is
infection; a cancelled transform leaves the villager dead. - Skeletons in powder snow. They become strays with the reason
frozen; cancelling keeps the skeleton.
Keep it cheap
Avoid a world-wide search per mob, unlimited path requests and one task per entity. Stop your work for a mob when it is removed or your script unloads.