Skip to content

Custom mobs and effects

Fidorial runs mob AI itself (goals, A* pathfinding on the ai-workers threads). A plugin can plug into it in three ways: declare a new mob, attach a behaviour to an existing one, and add a potion effect.

The client only knows vanilla entity types, so a new mob looks like a vanilla one (its networkType) while the server gives it its own stats and brain. Here, a bull that looks like a cow:

public static final Key BULL = Key.key("myplugin", "bull");
@Override
public void onLoad(final PluginContext ctx) {
ctx.server().registries().mobs().register(
MobDefinition.builder(BULL, Key.key("cow"))
.maxHealth(24f) // half-hearts
.movementSpeed(0.28) // blocks per tick
.size(0.95, 1.45) // hitbox, in blocks
.followRange(24.0) // how far it looks for targets, 0 = never
.attackDamage(5f)
.soundSource(Sound.Source.HOSTILE)
.behaviour(BullBehaviour::new) // one instance per bull
.persistent(true) // saved with the world
.build(),
this);
}

It can then be summoned with /summon myplugin:bull, or from code:

ctx.server().registries().mobs().spawn(BULL, player.location()); // Optional<Mob>

When your plugin stops, remove everything it declared with ctx.server().registries().mobs().unregisterAll(this).

A MobBehaviour is the plugin-side half of a mob: one instance per mob, free to keep that mob’s state. Every method is optional.

public final class BullBehaviour implements MobBehaviour {
private final Mob mob;
public BullBehaviour(final Mob mob) {
this.mob = mob;
}
@Override
public void onSpawn() {
mob.goals().add(new ChargeGoal(mob, 1));
mob.goals().add(new WanderGoal(mob, 5));
}
@Override
public void onHurt(final DamageSource source, final float amount) {
if (source.causingEntity() instanceof final Player attacker) {
mob.setTarget(attacker);
}
}
@Override
public boolean onInteract(final Player player, final EquipmentSlotGroup hand) {
mob.lookAt(player);
return false; // true to stop the vanilla handling
}
}
Method Called
onSpawn() Once, right after the mob is created, before its first tick. Add the goals here.
onTick(tick) Every tick, after the goals ran.
onHurt(source, amount) After the mob actually lost health.
onDeath() When it runs out of health, before the death animation.
onRemove() When it leaves the world (death or unload).
onInteract(player, hand) When a player right-clicks it.

Behaviours run on the mob’s region thread: you can change the mob and the blocks around it directly, but never block.

attach adds a behaviour to every mob of a type spawned from now on, vanilla ones included:

// Every new cow charges at players.
ctx.server().registries().mobs().attach(Key.key("cow"), BullBehaviour::new, this);

mob.behaviour(BullBehaviour.class) finds your behaviour back on a given mob.

A mob runs one goal at a time. Every tick, a goal with a better priority (a lower number) that canStart() takes over; the running goal stops as soon as shouldContinue() returns false.

public final class ChargeGoal implements Goal {
private final Mob mob;
private final int priority;
private int ticks;
public ChargeGoal(final Mob mob, final int priority) {
this.mob = mob;
this.priority = priority;
}
@Override
public int priority() {
return priority;
}
@Override
public boolean canStart() {
final Player nearest = mob.nearestPlayer(mob.followRange());
if (nearest == null || !mob.hasLineOfSightTo(nearest)) {
return false;
}
mob.setTarget(nearest);
return true;
}
@Override
public boolean shouldContinue() {
final Player target = mob.target();
return target != null && target.isAlive() && !mob.isDead()
&& mob.distanceSqTo(target) < mob.followRange() * mob.followRange();
}
@Override
public void tick() {
final Player target = mob.target();
mob.lookAt(target);
if (++ticks % 10 == 1) { // do not recompute the path every tick
mob.navigateTo(target.location(), mob.movementSpeed() * 1.5);
}
if (mob.distanceSqTo(target) < 2.0) {
target.damage(DamageSource.mobAttack(mob), mob.attackDamage());
}
}
@Override
public void stop() {
ticks = 0;
mob.setTarget(null);
mob.navigation().stop();
mob.setMoveSpeed(0.0);
}
}

mob.goals() also has remove, removeType(Class), clear() and active() (the running goal), handy to strip the AI of a vanilla mob you attached a behaviour to.

final MobEffectRegistry effects = ctx.server().registries().effects();
player.addEffect(effects.instance(MobEffectKeys.REGENERATION, 20 * 10, 0)); // 10 s, level I
player.addEffect(effects.builder(MobEffectKeys.NIGHT_VISION)
.duration(MobEffectInstance.INFINITE_DURATION)
.ambient(true)
.showIcon(false)
.build());

Every change goes through EntityEffectEvent (cancellable), with the action ADDED, CHANGED, REMOVED or EXPIRED. A listener can also replace the effect being applied, for instance to cap its level:

ctx.events().subscribe(EntityEffectEvent.class, event -> {
final MobEffectInstance effect = event.effect();
if (effect != null && effect.amplifier() > 4) {
event.setEffect(effect.toBuilder().amplifier(4).build());
}
});

A new effect needs a key, a behaviour, and a way to be shown to vanilla clients, which only know vanilla effects: a fallback whose icon they display instead.

public static final TypedKey<MobEffect> BLEEDING =
TypedKey.create(RegistryKey.MOB_EFFECT, Key.key("myplugin", "bleeding"));
@Override
public void onLoad(final PluginContext ctx) {
final MobEffectRegistry effects = ctx.server().registries().effects();
effects.register(effects.define(BLEEDING)
.category(MobEffectDefinition.Category.HARMFUL)
.fallback(MobEffectKeys.POISON) // vanilla clients see the Poison icon
.behaviour(new BleedingBehaviour())
.build(), this);
}
final class BleedingBehaviour implements MobEffectBehaviour {
@Override
public void onTick(final LivingEntity entity, final MobEffectInstance effect) {
if (effect.duration() % 40 == 0) { // every 2 seconds
entity.damage(DamageSource.generic(), 1f + effect.amplifier());
}
}
}

MobEffectBehaviour has onApplied, onTick and onRemoved. A single instance serves every entity carrying the effect: keep per-entity state in a map keyed by UUID, and clean it in onRemoved.

You can also attach a behaviour to a vanilla effect, to make it do something it does not do yet on Fidorial:

effects.attach(MobEffectKeys.REGENERATION, new MyRegeneration(), this);

Remove everything with effects.unregisterAll(this) in onDisable().