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
+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