Move effects into the modding api

This commit is contained in:
2026-10-02 11:53:44 -03:00
parent 4309c700d2
commit e5dcb97643
33 changed files with 458 additions and 109 deletions
+2
View File
@@ -51,6 +51,8 @@ export function setup(ctx: ServerContext) {
}
```
Items have components the same way, and so do [status effects](../entities/effects.md#effect-components).
Components can only be registered during `setup`, and only in the mod's own namespace. Once every mod's `setup` has run,
a block or item that lists a component nobody registered stops the server from starting, naming the block and the
component.
+5 -4
View File
@@ -1,7 +1,7 @@
# Registries
Every block, item, block model, entity, entity model and texture has an id, and is registered under it once, while mods
load. After that the game refers to things only by their id.
Every block, item, block model, entity, entity model, status effect and texture has an id, and is registered under it
once, while mods load. After that the game refers to things only by their id.
## Identifiers
@@ -32,8 +32,9 @@ The server and every client load mods the same way, in the same order:
1. **Order.** Dependencies come before the mods that need them, and otherwise mods load alphabetically. Mods that depend
on each other in a circle are an error. The server sends its order to clients, and they follow it.
2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models and entities. Then
every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which can use tags.
2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models, entities and effects.
Then every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which can use
tags.
3. **Scripts.** Server scripts' `setup` runs on the server, and client scripts' on each client. Worldgen scripts'
`setup` runs on the server before it makes chunks.
+102
View File
@@ -0,0 +1,102 @@
# 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. 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",
"modifiers": [{ "attribute": "bworld: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`. |
| `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 "bworld/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;
damage(amount: number, cause?: string): void; // cause is for events and the death message
heal(amount: number): void;
}
```
Like block components, an effect that lists a component nobody registered stops the server from starting.
## Using Effects
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. |
Resistance and water breathing have no components: like in Minecraft, the engine checks for them where damage and air
are worked out.
+2 -2
View File
@@ -103,8 +103,8 @@ sends players their `attack_speed` whenever it changes so the bar matches.
## Status Effects
Players have some of Minecraft's status effects: `bworld:speed`, `bworld:slowness`, `bworld:regeneration`,
`bworld:poison`, `bworld:instant_health`, `bworld:resistance` and `bworld:water_breathing`.
Players can have any mod's [status effects](effects.md), like the base game's `bworld:speed`, `bworld:regeneration` or
`bworld:water_breathing`.
```ts
player.add_effect("bworld:regeneration", 100, { amplifier: 1 }); // regeneration II for 5 seconds
+3 -2
View File
@@ -16,6 +16,7 @@ mods/
models/*.json # block models
models/*.geo.json # entity models
entities/*.json
effects/*.json # status effects, see entities/effects.md
recipes/*.json
tags/<registry>/<namespace>/*.json # tags, see resources/tags.md
worldgen/ores.json
@@ -36,8 +37,8 @@ Data folders can have subfolders. Every data file holds one definition, wrapped
}
```
The key is `block`, `item`, `model`, `entity`, `recipe` or `tag`, depending on the folder. File names don't matter, the
`id` inside does, except for [tags](../resources/tags.md), whose id is their path.
The key is `block`, `item`, `model`, `entity`, `effect`, `recipe` or `tag`, depending on the folder. File names don't
matter, the `id` inside does, except for [tags](../resources/tags.md), whose id is their path.
## `manifest.json`