Skip to content

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.

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

In your mod’s common code; the binding is parsed at server start, once every mod has registered its reactions and triggers:

Make the whole herd flee when one of its cows is hurt
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())));

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:

Leave out reactions a player switched off
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);
  • A then step 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.

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:

Register a reaction that gathers a group at its center
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.