Skip to content

Herd animals that flee from players

This guide gathers cows into herds that follow their oldest cow, and makes the adults of a herd run from a player who hurts one of them, then walk back to their leader.

Bindings are JSON files in the mod’s own data, so data packs can replace them. The reaction binding format lists every key used here. The first binding is the flight: when a player hurts a cow, the adults within 12 blocks run for 80 ticks (4 seconds), then follow the leader back. Save it as data/mymod/herd_bindings/flee_players.json:

Make the adults flee a player who hurts one, then regroup
{
"on": "member_hurt",
"do": "flee",
"who": "adults",
"radius": 12,
"when": { "source_type": "minecraft:player" },
"cooldown_ticks": 40,
"duration_ticks": 80,
"then": { "do": "follow_leader" }
}

The second has no on, so it is a standing behavior: between flights, stragglers walk back to the leader. Save it as data/mymod/herd_bindings/stay_together.json:

Walk stragglers back to the leader between flights
{
"do": "follow_leader",
"distance": 8
}

In your mod’s common initializer, register the reload handler from the MobGroups example. It defines herds of cows that follow the oldest cow, with the bindings parsed from every file in herd_bindings, and logs a file it cannot parse. Which files the handler receives, in what order, and when it runs again are on the DataReloaders section. How members join, leave, and pick a leader is on the MobGroups section.

The definition also covers every entity type in the mod’s cattle tag, so a data pack can add other animals without a code change; which of them share a herd is in GroupDefinition’s Javadoc. Save the tag as data/mymod/tags/entity_types/cattle.json:

Add mooshrooms to the animals that form herds
{
"values": ["minecraft:mooshroom"]
}

In a survival world, spawn eight cows close together and wait a second for them to form a herd. Hit one: the adults near it scatter away from you, and after about 4 seconds they walk back to the oldest cow. Hit another within 2 seconds and the herd ignores it, because of cooldown_ticks.