Skip to content

Mob control

MobControl gives one mob orders to run, keep away, walk to a point, hold still, leap, attack, or calm down, the same way whether its AI runs on goals or a brain. MobOwnership tells whether a player keeps a mob, so a mod can leave pets and mounts alone.

Type Summary Status Since Side Thread
MobControl Orders that move, alarm, hold, target, and calm a mob, whether its AI runs on goals or a brain. Experimental 1.2.0 server server
MobOwnership Whether a mob is kept by a player. Experimental 2.0.0 both server
TargetVeto A rule that forbids MobControl#attack from making a mob attack a target, for example no player in Peaceful. Experimental 1.2.0 server server

In your mod’s common initializer:

Keep a hurt animal away from the player who hurt it, unless a player keeps it
MobHurtCallback.EVENT.register((mob, source, amount, attacker) -> {
if (!(mob instanceof Animal) || !(attacker instanceof Player)) return;
if (MobOwnership.isKeptByPlayer(mob)) return;
// 8 blocks away at a brisk walk, for 200 ticks (10 seconds).
MobControl.avoid(mob, attacker, 8, 1.2, 200);
});

Experimental · Since 1.2.0 · Side: server · Thread: server

MobControl gives one mob an order from any server code, such as an event listener or a reaction. With mob a Mob the mod herds home and pen the BlockPos of its pen, run on the mod’s own schedule:

Walk a mob back to its pen, and stop asking once the pen is out of reach
if (!MobControl.hasMoveOrder(mob) && !MobControl.moveStoppedShort(mob, 200)) {
MobControl.moveTo(mob, pen.getX() + 0.5, pen.getY(), pen.getZ() + 0.5, 1.0, 200);
}

Stope drives each kind of AI through the parts vanilla already has:

  • A goal mob gets two Stope goals the first time it receives an order: a movement goal at priority 0 with the MOVE and LOOK flags, and a target goal at priority 0 with the TARGET flag.
  • A goal mob’s own melee and bow attacks pause during a movement order because Stope’s movement goal holds the flags they need.
  • A brain mob (any mob whose brain registers WALK_TARGET) is driven through memories at the end of its tick: WALK_TARGET for movement, IS_PANICKING while alarmed, AVOID_TARGET while avoiding, LOOK_TARGET while holding, and ATTACK_TARGET, ANGRY_AT, and RAM_TARGET for attacks. Stope sets each memory only when that brain registers it.
  • A movement order (alarm, avoid, move) stops a horse, donkey, mule, llama, or camel from eating (AbstractHorse#isEating), checked on every tick the order lasts: an eating horse cannot move, and starts eating at random.
  • A brain mob loses any walk target its idle behaviors set while a hold lasts.
  • A brain mob that has not reached a MobControl#moveTo point gets its walk target back at most every 10 ticks.
  • A goal mob on the ground with no path at all tries again after 10 ticks.
  • A goat with an attack order aims each ram at the target’s position as that ram starts.
  • A goat’s ram that ends without hitting (the target moved, or vanilla aborted the ram) is asked for again 10 ticks later.
  • An attack order on a new target, or one given after the last order ended, clears the ram cooldown.
  • An attack order on the same target while its order lasts only extends that order.
  • An attack order that runs out leaves what it set in place for vanilla AI: a goal mob’s target, a neutral mob’s anger, and a brain mob’s ATTACK_TARGET and ANGRY_AT. MobControl#calm clears them.
  • An attack order that ends early (its target dies, leaves the level, becomes vetoed, or is replaced by another target) takes back the memories Stope set.