Skip to content

Mob groups

Mob groups gather nearby mobs of one entity type into herds that Stope keeps current as mobs move, load, and die, each with a center, an optional leader, and a count of recent losses. Use them when mobs should act together, for example through reaction bindings, and MobControl alone when each mob acts on its own.

Type Summary Status Since Side Thread
GroupDefinition Describes one kind of mob group: its entity types, radius, size, leader, filter, and bindings. Experimental 1.2.0 both any
LeaderChoice How a mob group picks its leader. Experimental 1.2.0 both any
MobGroup Read-only view of one live mob group. Experimental 1.2.0 server server
MobGroupChangedCallback Fired when a mob joins or leaves a mob group. Experimental 1.2.0 server server
MobGroups Entry point for mob groups: define kinds of group, and Stope keeps live groups current. Experimental 1.2.0 server server

In your mod’s common initializer, with cow in the last lines standing for any cow on the server:

Gather cows into herds that follow the oldest cow
// Defined again at every server start, so each world gets the same groups.
ServerStartedCallback.EVENT.register(server -> MobGroups.define(id("herds"), List.of(
GroupDefinition.builder(id("cows"))
.type(EntityType.COW)
.leader(LeaderChoice.OLDEST)
.build())));
// Later, on the server thread, a cow's herd and its leader:
MobGroup herd = MobGroups.groupOf(cow);
Mob leader = herd == null ? null : herd.leader();

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

Define a mod’s kinds of group from a DataReloaders handler, so they follow its data files. With LOGGER, the mod’s SLF4J logger:

Define herds of cows with the bindings in the mod's data files
DataReloaders.registerFiles(id("herds"), "herd_bindings", "mymod/herd_bindings", files -> {
GroupDefinition.Builder cows = GroupDefinition.builder(id("cows"))
.type(EntityType.COW).tag(id("cattle"))
.radius(16).maxSize(12).leader(LeaderChoice.OLDEST).refreshInterval(20)
.filter(mob -> !mob.isLeashed());
for (DataFile file : files) {
try (Reader reader = file.openReader()) {
JsonObject binding = JsonParser.parseReader(reader).getAsJsonObject();
cows.binding(ReactionBinding.parse(binding, "mymod"));
} catch (IOException | RuntimeException e) {
// A broken file loses only its own binding; the herd keeps every other one.
LOGGER.warn("Skipped herd binding {}: {}", file.location(), e.getMessage());
}
}
MobGroups.define(id("herds"), List.of(cows.build()));
});
  • Every mob of an entity type a definition covers, grouped or not, checks its membership once every GroupDefinition#refreshInterval ticks, offset by its entity ID, so the members of a group check on different ticks.
  • A mob leaves its group when the group’s center is farther than GroupDefinition#radius.
  • A mob leaves its group when it fails the definition’s GroupDefinition#filter.
  • A mob joins the largest nearby group of its definition and entity type that has room and is at least as large as its own group, so neighboring groups drain into one. With no such group, it starts a group of its own.
  • The check of the group’s first member (the one that joined earliest) also recomputes the group’s center (the mean of member positions) and its leader. Standing behaviors and the threat_seen trigger are checked on this schedule (the group schedule).
  • One member far from the others pulls the mean toward itself.
  • When a group splits, either side may keep the old MobGroup object.
  • After MobGroups#define dissolves an owner’s groups, every member regroups at its own next check, so the new groups form within one refresh interval.