7.0 KiB
Status Effects
Status effects change a player or a mob for a while, like speed or poison. They work like NeoForge's MobEffects: an
effect is defined once, and adding it to someone gives them an instance of it with a duration and a level.
Like blocks and items, an effect is data with components. Its JSON in the mod's effects/ folder says what it is and
which attributes it changes. Components, registered by the server phase, say what it does over time. An effect can also
change things where they happen, through an event. Even the base game's effects are defined
this way, in mods/bworld/effects.
Defining an Effect
effects/swiftness.json:
{
"format_version": 1,
"effect": {
"id": "copper_tools:swiftness",
"category": "beneficial",
"icon": "copper_tools:swiftness",
"modifiers": [{ "attribute": "cubi:movement_speed", "amount": 0.1, "operation": "add_multiplied_total" }]
}
}
| Field | Required | What it does |
|---|---|---|
id |
yes | The effect's id, in the mod's namespace. |
category |
no | "beneficial", "harmful" or "neutral" (the default), like NeoForge's MobEffectCategory. |
instant |
no | true makes it happen once when added instead of lasting, like bworld:instant_health. |
icon |
no | A texture, shown at the top of the inventory screen while the player has it, with its name, level and time left on hover. 16×16 like an item's. |
modifiers |
no | Attribute modifiers while it's on. Each level adds amount again. |
components |
no | What it does over time, see below. |
The modifiers' operation is "add_value", "add_multiplied_base" or "add_multiplied_total". They come off when the
effect ends.
Effect Components
An effect that does something each tick lists components, with their parameters:
"components": {
"copper_tools:rusting": { "every": 40 }
}
The server phase registers them while it runs, in the mod's own namespace:
import type { ServerContext } from "cubi/server";
export function server(ctx: ServerContext) {
ctx.components.register_effect<{ every: number }>("copper_tools:rusting", {
should_tick: (ticks, amplifier, params) => ticks % params.every === 0,
on_tick(entity, amplifier) {
entity.damage(amplifier + 1, "rust");
},
});
}
| Handler | Called when | Returns |
|---|---|---|
should_tick(ticks, amplifier, params) |
Every tick the effect is on, to ask whether on_tick runs. Every tick without it. |
true to run on_tick |
on_tick(entity, amplifier, params) |
When should_tick says so. Instant effects run it once, when added. |
false takes the effect off |
ticks is how many ticks the effect has left, or how long it's been on for one that lasts forever. amplifier is the
level minus 1. entity is the player or mob that has it:
interface LivingRef {
readonly id: string;
readonly health: number;
readonly max_health: number;
// ticks of air left under water, max_air when full. it drowns 20 ticks after reaching 0. setting it over max_air
// sets max_air
air: number;
readonly max_air: number;
// the one it has of that type, like player.effect
effect(id: Id): { readonly amplifier: number; readonly duration: number } | undefined;
damage(amount: number, cause?: string): void; // cause is for events and the death message
heal(amount: number): void;
// its attributes and their modifiers, like a player's (see players.md). a mob's modifiers are saved with it
attribute(id: Id): number;
add_modifier(attribute: Id, modifier: AttributeModifier): void;
remove_modifier(attribute: Id, id: string): boolean;
}
Effects tick after the entity loses air under water, so setting air from on_tick keeps it from running out. That's
all bworld:water_breathing does:
ctx.components.register_effect("bworld:refill_air", {
on_tick(entity) {
entity.air = entity.max_air;
},
});
Like block components, an effect that lists a component nobody registered stops the server from starting.
Effects Through Events
Some effects change something as it happens, like damage coming in, rather than over time. Those need no component: a
handler checks whether the entity has the effect. bworld:resistance is one, subscribing to entity_damage in its
server phase:
ctx.events.before.entity_damage.subscribe((event) => {
const resistance = event.entity.effect("bworld:resistance");
if (!resistance || event.bypasses_invulnerability) return;
event.amount *= Math.max(0, 1 - 0.2 * (resistance.amplifier + 1));
});
Before handlers run in mod load order, so mods loaded later see the lowered amount.
Using Effects
/effect <effect> [seconds] gives you one while testing, for 30 seconds unless you say.
Any mod's effects are added to players by id, see Status Effects:
player.add_effect("copper_tools:swiftness", 200, { amplifier: 1 });
The Base Game's Effects
| Effect | What it does |
|---|---|
bworld:speed |
20% faster a level. |
bworld:slowness |
15% slower a level. |
bworld:regeneration |
Heals 1 every 50 ticks, twice as often each level. |
bworld:poison |
Hurts 1 every 25 ticks, twice as often each level. Never kills. |
bworld:instant_health |
Heals 4 at once, doubling each level. |
bworld:resistance |
Takes 20% less damage a level. |
bworld:water_breathing |
Doesn't run out of air under water. |
Regeneration, poison and instant health use components that tick. Water breathing's component fills the air back up each
tick, so it never runs out. Resistance has no component, it lowers damage in a before.entity_damage handler, see
Effects Through Events.