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.
Write the bindings
Section titled “Write the bindings”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:
{ "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:
{ "do": "follow_leader", "distance": 8}Read the bindings and define the herd
Section titled “Read the bindings and define the herd”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.
Let data packs add animals
Section titled “Let data packs add animals”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:
{ "values": ["minecraft:mooshroom"]}Try it in game
Section titled “Try it in game”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.
See also
Section titled “See also”- Mob groups: what makes a group, its leader, and its schedule.
- Reaction binding format:
every key,
whovalue, trigger, and primitive reaction. - Reactions (Java): leaving out reactions a player switched off, and adding reactions of your own.
- Saved data and reloaders: the reload handler, and the order in which it lists data-pack and config files.