151 lines
7.7 KiB
Markdown
151 lines
7.7 KiB
Markdown
# Items
|
|
|
|
Items are what players carry: in their inventory, in chests, and dropped on the ground. Every block registers an item
|
|
that places it, unless it sets `"item": false`. Everything else is defined in the mod's `items/` folder.
|
|
|
|
## Items and Item Stacks
|
|
|
|
Like blocks, each kind of item is defined once. What players hold are **item stacks**: an item id, a count, and optional
|
|
`data`.
|
|
|
|
```ts
|
|
interface ItemStack {
|
|
readonly id: string;
|
|
count: number;
|
|
data?: unknown; // any JSON value, set by server scripts
|
|
}
|
|
```
|
|
|
|
`data` is how one stack differs from another of the same item, like the water left in a watering can or the damage on a
|
|
tool. It's saved, and synced to clients as part of the container the stack is in, so client code can read it.
|
|
|
|
Stacks handed to server scripts are live: setting `count` or `data` on one changes the real stack, and a stack counted
|
|
down to 0 is gone.
|
|
|
|
## A Basic Item
|
|
|
|
`items/copper_ingot.json`:
|
|
|
|
```json
|
|
{
|
|
"format_version": 1,
|
|
"item": {
|
|
"id": "copper_tools:copper_ingot",
|
|
"texture": "copper_tools:copper_ingot",
|
|
"lore": "Smelted from copper ore."
|
|
}
|
|
}
|
|
```
|
|
|
|
## Properties
|
|
|
|
| Field | Default | Meaning |
|
|
| --------------- | ------------------- | ----------------------------------------------------------------------------- |
|
|
| `id` | required | The item's id. |
|
|
| `texture` | required | The texture it's drawn with. |
|
|
| `name` | from the id | What players see. `copper_tools:copper_ingot` is "Copper Ingot" when not set. |
|
|
| `lore` | none | Tooltip text under the name. `\n` starts a new line. |
|
|
| `max_stack` | `64`, `1` for tools | How many fit in one slot. |
|
|
| `places` | none | A block it places when used on a block, like seeds. |
|
|
| `tool` | none | Makes it a [tool](tools.md). |
|
|
| `food` | none | Makes it [edible](food.md). |
|
|
| `data_textures` | none | Other textures, picked by the stack's `data`, see below. |
|
|
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
|
|
| `components` | none | [Item components](#item-components) and their parameters. |
|
|
|
|
### Textures From Data
|
|
|
|
`data_textures` draws a stack with another texture when a key in its `data` is set, like the fishing rod while its line
|
|
is out:
|
|
|
|
```json
|
|
"data_textures": { "cast": "bworld:fishing_rod_cast" }
|
|
```
|
|
|
|
A stack whose `data.cast` is truthy uses `bworld:fishing_rod_cast`. When several keys are, the first listed wins.
|
|
|
|
### Worn Models
|
|
|
|
`worn` says how an item looks on a player who wears it, like Bedrock's attachables. A server script says what a player
|
|
wears with [`set_worn`](../entities/players.md#worn-items), and every client draws it on them. There's no third person
|
|
view, so players see what others wear, and what's on their own right arm in first person. Artifacts' power glove and
|
|
feral claws (`mods/artifacts`) and the base game's giant chestplate are examples.
|
|
|
|
```json
|
|
"worn": {
|
|
"model": "artifacts:power_glove_wide",
|
|
"texture": "artifacts:power_glove_wide",
|
|
"slim_model": "artifacts:power_glove_slim",
|
|
"slim_texture": "artifacts:power_glove_slim"
|
|
}
|
|
```
|
|
|
|
| Field | Meaning |
|
|
| ---------------------------- | ----------------------------------------------------------------------------------------------------- |
|
|
| `model` | An [entity model](../entities/index.md#entity-models) from a `models/*.geo.json`, made in Blockbench. |
|
|
| `texture` | The texture it's drawn with, laid out for the model's `texture_width` and `texture_height`. |
|
|
| `slim_model`, `slim_texture` | Optional, for players with slim (3 pixel wide) arms. The others when not set. |
|
|
|
|
A bone named like a part of the player, `head`, `body`, `right_arm`, `left_arm`, `right_leg` or `left_leg`, moves with
|
|
that part, so a model made over Blockbench's player template lines up. check-mods warns about a model with none of them,
|
|
since nothing of it would move. Bones under one of them go with it, and other bones stay with the body.
|
|
|
|
Things to know when making one:
|
|
|
|
- **Armor** can use a Minecraft armor texture (64x32, like `textures/entity/equipment/humanoid/` in a mod's assets) as
|
|
it is: a box over each part, inflated by 1, with the UVs of Minecraft's armor model. The giant chestplate
|
|
(`mods/bworld/models/giant_chestplate.geo.json`) is `body` at `[16, 16]` and both arms at `[40, 16]`.
|
|
- **Blockbench's `mirror` isn't supported**, so a box's texture is never flipped. Minecraft mirrors the left arm's and
|
|
leg's armor, which here show the right one's texture as it is.
|
|
- **Porting a Java mod's model**, like Artifacts' `ArmsMeshDefinitions`: Java models measure y down from the top of the
|
|
head, bedrock up from the feet. Keep x and z, and make each y `24 - y`, with a box's origin at its bottom.
|
|
|
|
## Item Components
|
|
|
|
Like [block components](../blocks/components.md), item components give items behavior. The server script registers them:
|
|
|
|
```ts
|
|
ctx.components.register_item<{ max_water: number }>("bworld:watering_can", {
|
|
on_create(item, params) {
|
|
item.data = { water: params.max_water };
|
|
},
|
|
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
|
|
},
|
|
});
|
|
```
|
|
|
|
and items list them with their parameters:
|
|
|
|
```json
|
|
"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. |
|
|
|
|
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.
|
|
|
|
`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).
|
|
|
|
## Server Script Helpers
|
|
|
|
```ts
|
|
ctx.items.exists("bworld:carrot"); // true
|
|
ctx.items.max_stack("bworld:wood_pickaxe"); // 1
|
|
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
|
|
|
|
player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet
|
|
```
|