Reactions (Java)
A reaction binding ties a trigger, such as a group member being hurt, to a reaction its group runs, such as fleeing. These types parse bindings, run them for mob groups, and let a mod add its own reactions and triggers. The JSON keys, triggers, and Stope’s own reactions are on the reaction binding format page.
At a glance
Section titled “At a glance”| Type | Summary | Status | Since | Side | Thread |
|---|---|---|---|---|---|
EntityMatcher |
Matches entities by type ID or entity type tag, as written in bindings. | Experimental | 1.2.0 | both | any |
Reaction |
What a group’s members do when a binding’s trigger fires, or a standing behavior when the binding has no trigger. | Experimental | 1.2.0 | server | server |
ReactionBinding |
One parsed binding of a trigger to a reaction, or a standing behavior when it has no trigger. | Experimental | 1.2.0 | both | any |
ReactionContext |
What a running reaction sees: its group, the firer, the source, the chosen members, and time. | Experimental | 1.2.0 | server | server |
ReactionParams |
Typed, validated access to a reaction’s or trigger’s parameters from a binding. | Experimental | 1.2.0 | both | any |
Reactions |
Registry of reaction types by namespaced ID. | Experimental | 1.2.0 | both | setup |
ReactionStartedCallback |
Fired each time the reaction engine starts a reaction for a group. | Experimental | 1.2.0 | server | server |
Triggers |
Trigger IDs: Stope’s built-in triggers, and registration and firing for triggers other mods add. | Experimental | 1.2.0 | both | setup |
Quick start
Section titled “Quick start”In your mod’s common code; the binding is parsed at server start, once every mod has registered its reactions and triggers:
static final JsonObject FLEE = JsonParser.parseString(""" {"on": "member_hurt", "do": "flee", "who": "all"} """).getAsJsonObject();
// In your mod's common initializer:ServerStartedCallback.EVENT.register(server -> MobGroups.define(id("herds"), List.of( GroupDefinition.builder(id("cows")) .type(EntityType.COW) .binding(ReactionBinding.parse(FLEE, "mymod")) .build())));ReactionBinding
Section titled “ReactionBinding”EntityMatcher: Experimental · Since 1.2.0 · Side: both · Thread: any
ReactionBinding: Experimental · Since 1.2.0 · Side: both · Thread: any
ReactionBinding holds one parsed binding for a group definition. Stope reads no binding
files itself: a mod loads its own JSON, for example from a DataReloaders handler, passes each
binding object to ReactionBinding#parse, and attaches the bindings to a GroupDefinition.
With binding a parsed binding, disabled the Set<ResourceLocation> of reactions the
player switched off in the mod’s settings, and cows a GroupDefinition.Builder:
ReactionBinding kept = binding.without(disabled::contains);// Null when every step was switched off: the definition then gets no binding at all.if (kept != null) cows.binding(kept);Ordering and timing
Section titled “Ordering and timing”- A
thenstep starts when the step before it ends, with the same members, firer, and source. - Which group a trigger fires for, which events fire no trigger, and when standing behaviors run are on the reaction binding format page.
Reactions
Section titled “Reactions”Reaction: Experimental · Since 1.2.0 · Side: server · Thread: server
ReactionParams: Experimental · Since 1.2.0 · Side: both · Thread: any
Reactions: Experimental · Since 1.2.0 · Side: both · Thread: setup
Reactions registers a mod’s own reaction types beside Stope’s primitives. A reaction’s
parameters are keys of the binding object itself, beside on and do. List every key the
factory reads in Reactions#register, and read each with a range, so a bad value fails the
binding when it is parsed:
Reactions.register(id("gather"), Set.of("duration_ticks"), params -> { int duration = params.getInt("duration_ticks", 60, 1, 600); return ctx -> { MobGroup g = ctx.group(); for (Mob m : ctx.members()) MobControl.moveTo(m, g.centerX(), g.centerY(), g.centerZ(), 1.2, duration); return c -> c.age() < duration; // the Run: false ends it and starts `then` };});A reaction whose Reaction#start returns null has finished, and its then step starts in the
same tick. When a run ends on a later tick, its then step starts in that tick too and is
first ticked on the next server tick.
See also
Section titled “See also”- Reaction binding format:
every key,
whovalue, trigger, and primitive reaction. - Mob groups: the group schedule, and attaching bindings to a definition.
- Mob control: the orders a reaction gives.
- Herd animals that flee from players: a guide that builds a fleeing herd step by step.