Files
2026-11-22 13:21:27 -03:00

264 lines
16 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 phases
}
```
`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.
Two stacks of one item only merge into one when their `data` is the same, whatever order its keys are in, in an
inventory or on the ground. No data is the same as `{}`.
Stacks handed to server phases 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. Leave it out to [fill in a block's item](#block-items). |
| `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. |
| `base_sell_value` | none | What one is worth when sold, a whole number of 0 or more. |
| `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. |
| `model` | none | A [model](../resources/models.md#item-models) it's drawn with instead of its texture, or an [entity model](../resources/models.md#entity-models-as-items). |
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
| `entity` | none | An [object](../entities/index.md#behaviors) it lies in the world as instead of a dropped item, holding the stack. |
| `targets_fluids` | `false` | Players holding it aim at fluids, like buckets and fishing rods. Others aim through. |
| `fluid_container` | none | Makes it hold a fluid, like a bucket, see [Fluids](../resources/fluids.md#containers). |
| `layers` | none | Textures drawn over its own, each in a color, see [below](#layers). |
| `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.
### Layers
`layers` are textures drawn over the item's own, in order, like the fluid in a bucket or the trim on armor. Each can be
drawn in a color, with `tint`:
```json
"layers": [
{ "texture": "copper_tools:jar_fluid", "tint": "fluid" },
{ "texture": "copper_tools:jar_label", "tint": "#c42d39" }
]
```
| `tint` | The layer's drawn in |
| --------------------------------- | ---------------------------------------------------------------------------------------------------- |
| none | Its own colors. |
| a color, like `"#c42d39"` | That color. |
| `"grass"`, `"foliage"`, `"water"` | That color outside any biome, like a block's [tint](../blocks/index.md#tinting) in its item. |
| `"fluid"` | The color of the fluid a [fluid container](../resources/fluids.md#containers) holds. Not when empty. |
A tint multiplies the texture's colors, so a tinted layer is painted in light grays. A layer whose tint has no color for
a stack, like `"fluid"` on an empty container, isn't drawn. Layers are drawn on the front and back of a flat item, so
they should stay inside the item's own shape, and an item drawn with a `model` can't have them.
### Worn Models
`worn` says how an item looks on a player who wears it, like Bedrock's attachables. The server phase 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.
### Block Items
A block's item only has the block's texture and name. To give it anything else, like `lore` or `base_sell_value`, add an
item file with the block's id and no `texture`. What it sets is added to the block's item, and the rest stays as the
block made it.
`items/copper_block.json`:
```json
{
"format_version": 1,
"item": {
"id": "copper_tools:copper_block",
"base_sell_value": 9
}
}
```
The block has to be in the same mod and can't set `"item": false`, or `check-mods` reports an error and the game won't
load the mod.
## Item Components
Like [block components](../blocks/components.md), item components give items behavior. The server phase 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, target) {
// right clicked while holding it, at target's block or at nothing
},
});
```
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, 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.
`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`.
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
},
});
```
## Machines Using Items
`ctx.world.use_item_on(item, x, y, z, face)` uses an item on the face of a block the way a player holding it would, like
Create's deployer: the block's `on_interact`, or the item's `on_use` with the block as its target, or placing it. A
player that isn't anyone, named `[machine]`, does it, standing in the block in front of that face and looking at it,
like NeoForge's `FakePlayer`. It's never online, so nothing sent to it goes anywhere.
```ts
const { item, given } = ctx.world.use_item_on({ id: "bworld:cobblestone", count: 5 }, x, y, z, "top");
// item is what it holds after, { id: "bworld:cobblestone", count: 4 } once one is placed, undefined when used up.
// given is anything else it was given, like a stick from a wand's on_use
```
## Server Script Helpers
```ts
ctx.items.exists("bworld:carrot"); // true
ctx.items.ids(); // every item, in load order
ctx.items.max_stack("bworld:wood_pickaxe"); // 1
ctx.items.max_stack(id, data); // the same for a stack with this data, fewer for a full fluid container
ctx.items.fluid_capacity("bworld:bucket"); // how much a fluid container holds full, or undefined
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
ctx.items.damage(item, 1); // takes durability off a tool stack, true when that used it up
ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value
ctx.items.sell_value(id, data); // the same for a stack with this data, with a full fluid container's fluid on top
player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet
player.exchange_held({ id: "bworld:bucket", count: 1, data }); // one of the held item becomes this
```
Client and shared phases have `ctx.items.sell_value(id, data)` too, worked out the same way with that side's
[fluid rules](../resources/fluids.md#rules), so a client can show what a full bottle of juice is worth, like the base
game's [tooltip line](../gui/index.md#item-tooltips) does.