Player api
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user