Move effects out of cubi
This commit is contained in:
@@ -48,7 +48,7 @@ showed. These events fire for **every** player's actions, since they all go thro
|
||||
| `block_place` | ✓ | ✓ | `player`, `block` (`{ id, x, y, z }`), `face` clicked, `hit` where on it (0-1 each axis), `item` held |
|
||||
| `block_interact` | ✓ | ✓ | `player`, `block`, `item` held. Before fires for every right click on a block, after only when a component's `on_interact` handled it |
|
||||
| `chat_send` | ✓ | ✓ | `player`, `message`. Before handlers can change `message` |
|
||||
| `player_damage` | ✓ | ✓ | `player`, `amount`, `cause`. Before handlers can change `amount`, after fires only when they were hurt, with what they took |
|
||||
| `entity_damage` | ✓ | ✓ | `entity`, `player`, `amount`, `cause`, `bypasses_invulnerability`. See below |
|
||||
| `player_death` | | ✓ | `player`, `cause`. They've dropped what they carried and respawn right after |
|
||||
| `player_join` | | ✓ | `player` |
|
||||
| `player_leave` | | ✓ | `player` |
|
||||
@@ -56,7 +56,13 @@ showed. These events fire for **every** player's actions, since they all go thro
|
||||
| `tick` | | ✓ | `dt`, in seconds. 20 times a second |
|
||||
| `new_day` | | ✓ | `day`. At 06:00, after every block's `on_new_day` |
|
||||
|
||||
`cause` is `"fall"`, `"void"`, `"starve"`, or whatever a mod passed to `player.damage(amount, cause)`.
|
||||
`cause` is `"fall"`, `"void"`, `"starve"`, `"mob_attack"`, or whatever a mod passed to `damage(amount, cause)`.
|
||||
|
||||
`entity_damage` fires whenever a player or a mob is hurt, like NeoForge's `LivingIncomingDamageEvent`. `entity` is a
|
||||
[`LivingRef`](../entities/effects.md#effect-components), and `player` is set when it's a player. Before handlers can
|
||||
change `amount` or cancel it. They run before the hurt cooldown, so what they leave is what's compared with the last
|
||||
hit. `bypasses_invulnerability` is set for damage that nothing should stop, like falling out of the world. After fires
|
||||
only when the entity was hurt, with the amount the before handlers left.
|
||||
|
||||
### Order
|
||||
|
||||
|
||||
@@ -4,8 +4,9 @@ Status effects change a player or a mob for a while, like speed or poison. They
|
||||
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`.
|
||||
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
|
||||
|
||||
@@ -73,13 +74,46 @@ 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.
|
||||
@@ -102,5 +136,6 @@ player.add_effect("copper_tools:swiftness", 200, { amplifier: 1 });
|
||||
| `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.
|
||||
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).
|
||||
|
||||
@@ -99,7 +99,7 @@ half a second, unless the new hit is harder, and then only by the difference.
|
||||
A player's client moves them, except while they're knocked back: then the server moves them until they land, so it works
|
||||
even when their game isn't running, like in a background tab.
|
||||
|
||||
`damage(amount, cause)` hurts them like anything else would. `cause` shows in the `player_damage` and `player_death`
|
||||
`damage(amount, cause)` hurts them like anything else would. `cause` shows in the `entity_damage` and `player_death`
|
||||
[events](../concepts/events.md) and in the death message. Before events can change the amount or cancel it.
|
||||
|
||||
## Attributes
|
||||
|
||||
Reference in New Issue
Block a user