Files
bworld/docs/modding/entities/effects.md
T
2026-11-05 22:37:41 -03:00

142 lines
6.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Status Effects
Status effects change a player or a mob for a while, like speed or poison. They work like NeoForge's `MobEffect`s: 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 script, say what it does over time. An effect can also
change things where they happen, through an [event](../concepts/events.md). Even the base game's effects are defined
this way, in `mods/bworld/effects`.
## Defining an Effect
`effects/swiftness.json`:
```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](../concepts/registries.md#identifiers). |
| `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](players.md#attributes) 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:
```json
"components": {
"copper_tools:rusting": { "every": 40 }
}
```
The server script registers them during `setup`, in the mod's own namespace:
```ts
import type { ServerContext } from "cubi/server";
export function setup(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:
```ts
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;
}
```
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:
```ts
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 script:
```ts
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](players.md#status-effects):
```ts
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](#effects-through-events).