Files
bworld/docs/modding/items/index.md
T

5.2 KiB

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.

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:

{
	"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.
food none Makes it edible.
data_textures none Other textures, picked by the stack's data, see below.
components none 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:

"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.

Item Components

Like block components, item components give items behavior. The server script registers them:

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:

"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 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.

Server Script Helpers

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