Player api

This commit is contained in:
2026-10-13 22:59:48 -03:00
parent b34e97e487
commit a10b15a3fd
22 changed files with 581 additions and 89 deletions
+10 -7
View File
@@ -115,11 +115,12 @@ a slab's sides and plants aren't.
### Placing
`state_for_placement` picks the states a block is placed with, like Minecraft's `getStateForPlacement`. `placement` is
`{ x, y, z, face, hit, facing }`: where it's going, the face of the block that was clicked, where on that block the
click was (from 0 to 1 on each axis), and the way the player looks along the ground. It returns states by name, and the
rest keep their defaults, except that a 2 bit `facing` already faces the player. It runs before the server checks that
no one is in the way, so that's checked against the shape the block will really have. A trapdoor clicked onto the upper
half of a block's side hangs in the top half:
`{ x, y, z, face, hit, facing, sneaking }`: where it's going, the face of the block that was clicked, where on that
block the click was (from 0 to 1 on each axis), the way the player looks along the ground, and whether they're
[sneaking](../entities/players.md#sneaking), for blocks that sneaking places the other way round. It returns states by
name, and the rest keep their defaults, except that a 2 bit `facing` already faces the player. It runs before the server
checks that no one is in the way, so that's checked against the shape the block will really have. A trapdoor clicked
onto the upper half of a block's side hangs in the top half:
```ts
state_for_placement(placement): Record<string, number> {
@@ -142,8 +143,10 @@ the block had no components.
A rule's `on_interact` also runs on the clicking player's client, so they see what it does right away instead of waiting
for the server, like a trapdoor opening. It's for clicks that only change states with `ctx.world.set_state`, like
Minecraft's `useWithoutItem`: anything that needs the server, like opening a screen, giving items or tile data, goes in
the handler. `interaction` is `{ face, hit, facing }`: the face that was clicked, where on it (from 0 to 1 on each
axis), and the way the player looks along the ground. The trapdoor's is in `mods/bworld/scripts/shared.ts`:
the handler. `interaction` is `{ face, hit, facing, sneaking }`: the face that was clicked, where on it (from 0 to 1 on
each axis), the way the player looks along the ground, and whether they're sneaking. A player sneaking with an item in
hand doesn't interact at all: no `on_interact` runs, and the item is used or placed against the block instead. The
trapdoor's is in `mods/bworld/scripts/shared.ts`:
```ts
on_interact(block) {
+4 -3
View File
@@ -61,9 +61,10 @@ showed. These events fire for **every** player's actions, since they all go thro
### Order
Handlers run in the order they subscribed, and mods' `setup` functions run in [load order](registries.md#loading). For
`block_interact`, the before event runs first, then the block's own components' `on_interact`, rules before handlers. If
no component handled the click and the held item has no `on_use`, the held block is placed, which fires `block_place`.
Cancelling it undoes what the clicking player's client guessed, like a trapdoor it showed opening.
`block_interact`, the before event runs first, then the block's own components' `on_interact`, rules before handlers,
unless the player is [sneaking](../entities/players.md#sneaking) with an item in hand. If no component handled the click
and the held item has no `on_use`, the held block is placed, which fires `block_place`. Cancelling it undoes what the
clicking player's client guessed, like a trapdoor it showed opening.
## Errors
+50 -5
View File
@@ -52,6 +52,11 @@ interface Player {
remove_effect(id: string): boolean;
is_swimming(): boolean; // see Swimming
is_sneaking(): boolean; // see Sneaking
start_using(ticks: number): void; // see Using Items Over Time
stop_using(): void;
is_using(): boolean;
}
```
@@ -64,7 +69,7 @@ what a player is looking at, like Minecraft's `getPlayerPOVHitResult`:
```ts
const eye = player.eye_position;
const look = player.look_direction;
const reach = 5;
const reach = player.attribute("bworld:block_interaction_range");
const to = { x: eye.x + look.x * reach, y: eye.y + look.y * reach, z: eye.z + look.z * reach };
const hit = ctx.world.raycast(eye, to, { liquids: "source_only" });
```
@@ -96,10 +101,12 @@ even when their game isn't running, like in a background tab.
A player's stats are attributes, like Minecraft's: a base value that modifiers change without touching it.
| Attribute | Base | Meaning |
| ---------------------- | ---- | -------------------------------------------------------------------------- |
| `bworld:attack_damage` | 1 | How much their hits do. |
| `bworld:attack_speed` | 4 | Full strength attacks a second. Hitting sooner than that does less damage. |
| Attribute | Base | Meaning |
| --------------------------------- | ---- | -------------------------------------------------------------------------- |
| `bworld:attack_damage` | 1 | How much their hits do. |
| `bworld:attack_speed` | 4 | Full strength attacks a second. Hitting sooner than that does less damage. |
| `bworld:block_interaction_range` | 6 | How far from their eyes they break and use blocks, in blocks. |
| `bworld:entity_interaction_range` | 3 | How far from their eyes they hit entities, in blocks. |
```ts
player.add_modifier("bworld:attack_damage", { id: "my_mod:rage", amount: 2, operation: "add_value" });
@@ -116,6 +123,26 @@ Like Minecraft, an attack right after the last one, or right after changing the
damage, rising to full after `20 / attack_speed` ticks. A bar under the crosshair shows it filling up, and the server
sends players their `attack_speed` whenever it changes so the bar matches.
The two ranges are how far a player reaches, like NeoForge's `BLOCK_INTERACTION_RANGE` and `ENTITY_INTERACTION_RANGE`.
Their client aims that far, and the server allows a little more, for where they moved since they aimed. A tool that
reaches further gives a modifier while it's held, like Create's extendo grip:
```ts
ctx.events.after.tick.subscribe(() => {
for (const player of ctx.players.all()) {
if (player.held_item?.id === "copper_tools:grabber") {
player.add_modifier("bworld:block_interaction_range", {
id: "copper_tools:grabber",
amount: 3,
operation: "add_value",
});
} else {
player.remove_modifier("bworld:block_interaction_range", "copper_tools:grabber");
}
}
});
```
## Status Effects
Players can have any mod's [status effects](effects.md), like the base game's `bworld:speed`, `bworld:regeneration` or
@@ -155,6 +182,24 @@ client scripts, where `ctx.player.is_swimming()` is about the player on this cli
if (player.is_swimming()) player.add_effect("bworld:speed", 20);
```
## Sneaking
`player.is_sneaking()` says whether a player holds the sneak key, like NeoForge's `isShiftKeyDown()`. It's the key, not
the pose: a player flying down holds it too. Their client tells the server as soon as it changes. In client scripts,
`ctx.player.is_sneaking()` is about the player on this client.
Sneaking changes what right clicking a block does, like Minecraft: with an item in hand, the block isn't used at all,
and the item is used or placed against it instead. That's how a block is placed against a chest without opening it. With
an empty hand, the block is used as usual, and its handlers can read `player.is_sneaking()`. Blocks' placement and
interaction rules get `sneaking` too, see [Placing](../blocks/components.md#placing).
## Using Items Over Time
Some items are used for as long as the use button is held, like drawing a bow or eating in Minecraft. An item's `on_use`
starts it with `player.start_using(ticks)`, and the item's components hear how it goes, see
[Using Over Time](../items/index.md#using-over-time). `stop_using()` ends it without either ending, and `is_using()`
says whether they're using one. While they are, they walk at a fifth of their speed and can't sprint.
## Death
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
+45 -9
View File
@@ -136,8 +136,8 @@ ctx.components.register_item<{ max_water: number }>("bworld:watering_can", {
get_lore(item, params) {
return `Water: ${(item.data as { water: number }).water}/${params.max_water}`;
},
on_use(item, params, player) {
// right clicked while holding it
on_use(item, params, player, target) {
// right clicked while holding it, at target's block or at nothing
},
});
```
@@ -148,11 +148,14 @@ and items list them with their parameters:
"components": { "bworld:watering_can": { "max_water": 32 } }
```
| Handler | Called when |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
| `on_create(item, params)` | A stack of it is made: crafted, given, dropped by a block, and so on. |
| `get_lore(item, params)` | The server sends the stack to a player. Returns extra tooltip text for that stack, shown under the item's own `lore`. It isn't saved. |
| `on_use(item, params, player)` | A player right clicks while holding it, at nothing or at a block. |
| Handler | Called when |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `on_create(item, params)` | A stack of it is made: crafted, given, dropped by a block, and so on. |
| `get_lore(item, params)` | The server sends the stack to a player. Returns extra tooltip text for that stack, shown under the item's own `lore`. It isn't saved. |
| `on_use(item, params, player, target)` | A player right clicks while holding it, at nothing or at a block. |
| `on_use_tick(item, params, player, ticks)` | Every tick while it's [used over time](#using-over-time), `ticks` counting from 1. |
| `on_release_using(item, params, player, ticks)` | They let go of use before it finished, after using it for `ticks`. |
| `on_finish_using(item, params, player)` | They used it for as long as `start_using` said. |
At a block, the block's own [interaction](../blocks/components.md#interacting) comes first. An item with `on_use` is
used instead of being placed.
@@ -160,8 +163,41 @@ used instead of being placed.
`ctx.items.components(id)` gives the components an item's JSON lists and their parameters, so a mod can read its
components' parameters for an item without a stack, like the base game's curios slots do for `bworld:curio_modifiers`.
`on_use` doesn't say which block was clicked. For that, pair it with a block component's `on_interact`, the way the
watering can uses `bworld:water_source`, or subscribe to the `block_interact` [event](../concepts/events.md).
At a block, `target` is `{ block, face, hit }`: the block, with its tile data like a block component's, the face that
was clicked, and where on the block (0 to 1 on each axis), like Minecraft's `useOn`. At nothing it's `undefined`. A
player [sneaking](../entities/players.md#sneaking) with the item skips the block's own interaction, so a wrench can pick
up a block instead of using it:
```ts
on_use(item, params, player, target) {
if (target && player.is_sneaking()) ctx.world.break_block(target.block.x, target.block.y, target.block.z);
},
```
### Using Over Time
An item can be used for as long as the use button is held, like a bow or Minecraft's food. Its `on_use` calls
`player.start_using(ticks)`, then:
- `on_use_tick` runs every tick while they hold use, with how many ticks they've used it.
- `on_finish_using` runs once they've held it for `ticks`, and it stops.
- `on_release_using` runs instead if they let go first, with how long they held it, like a bow shooting.
`-1` keeps it going until they let go. Switching to another slot, or the item being used up or moved, stops it without
either ending. While using an item, a player walks at a fifth of their speed and can't sprint, and clicking again does
nothing.
```ts
ctx.components.register_item("copper_tools:sling", {
on_use(item, params, player) {
player.start_using(-1);
},
on_release_using(item, params, player, ticks) {
const power = Math.min(ticks / 20, 1); // full power after a second
// throw something with it
},
});
```
## Server Script Helpers