목차

Items

Items, like blocks, are owned by the server. You describe them in JSON (stack size, durability, tool, food) and give them behaviour through hooks. The mod has the same shape as a block mod: the content: field of veil_mod! (blocks.md).

The minimal item: a decl::Content table

Items and recipes go in the same table. This is what mc189 mod new my_item --template item generates: one charm, a shapeless recipe from paper and glowstone dust, and particles on right-click. assets/charm.png is a placeholder.

//! my item — item mod template. The same wasm runs on client and server.
#![no_std]
use mc189_mod_sdk::prelude::*;
use mc189_mod_sdk::srvitem::{ev, Hook};

const CONTENT: Content = Content::new()
    .items(&[Item::new("charm", r#"{"display":"Charm","stack":16,"hooks":["use"],"lore":["Sparkles when used"]}"#).texture("charm.png")])
    .recipes(&[r#"{"type":"shapeless","ingredients":["minecraft:paper","minecraft:glowstone_dust"],"result":{"item":"my_item:charm","count":2}}"#]);
const CHARM: u32 = CONTENT.item("charm");

/// Server hook: right-click in the air sprays particles (EnumParticleTypes 10 = magicCrit)
fn on_item(h: &Hook) -> i32 {
    if h.item == CHARM && h.ev == ev::USE {
        if let Some((_, x, y, z)) = srvblock::entity(h.eid) {
            srvblock::particle(10, x as f32, y as f32 + 1.0, z as f32, 0.4, 0.6, 0.4, 0.1, 16);
        }
    }
    0
}

veil_mod! {
    id: "my_item",
    name: "my item",
    version: "0.1.0",
    description: "my item",
    authors: ["me"],
    groups: ["base", "content", "server"],
    res: ["charm.png" => include_bytes!("../assets/charm.png")],
    content: CONTENT,
    item_hook: on_item,
}
  • .items(&[Item::new("name", json).texture("png")]) and .recipes(&[recipe_json, …]). The table entry's name is inserted into the JSON, so leave name out.
  • Recipe results are written as "modid:name" (the template fills in your mod id).
  • CONTENT.item("charm") is the hook number. Blocks, items and mobs share one numbering, so with blocks in the same table the item's number is block count + item index (blocks.md explains the trap).
  • Registration order is blocks, items, mobs, recipes, creative tabs. .tabs(&[…]) takes creative tab JSON.

Definition fields

reg::item_def(json), up to 16 KiB.

FieldDefaultMeaning
namerequired[a-z0-9_./]{1,64}; modid: is prefixed
displaythe nameShown name, 1 to 64 characters (tooltip, inventory, creative search). A resource-pack lang key item.<modid>:<name>.name wins over it — per-language names: see blocks.md
toolnone{kind, level, speed, damage}. kind is pickaxe, axe, shovel or sword; level 0 to 3 (wood, stone, iron, diamond); speed defaults to 2.0, damage to 0
foodnone{heal 0..20, saturation (default 0.6), always}. With always you can eat when full
stack1 for tools, otherwise 641 to 64
durability00 to 32767; 0 never wears out
fuel0Furnace burn time in ticks
hooks[]use, hit_entity, use_on_block, craft
lore[]Grey tooltip lines, 8 lines of up to 128 characters
subtypesfalseDamage value selects a variant (vanilla hasSubtypes): variants do not stack together and do not wear out

An item cannot be both a tool and a food. A bad definition makes the whole mod fail to load. Mining speed, attack damage, wear and the 32-tick eating time follow the vanilla formulas exactly.

reg::item("name") is a shortcut for a plain ingredient with no fields (up to 64, durability 0).

Recipes

reg::recipe(json), up to 1024 per mod.

typeFields
shapedpattern (1 to 3 rows of equal length), key (character → ingredient), result
shapelessingredients (1 to 9), result
smeltinginput, result, xp (furnace)

An ingredient is "name" or {"item": name, "meta": n}; a result is "name" or {"item", "count" 1..64, "meta"}. A name without : means minecraft:. Vanilla and modded items and blocks all work. A recipe that names something unknown is dropped on its own, with a log line.

Hooks

Only events listed in hooks reach item_hook. The argument is Hook { ev, item, eid, x, y, z, c, damage }.

evWhenArgumentsReturn value
USERight-click in the aireid, damage1 skips the vanilla use or eat
HIT_ENTITYHitting an entityeid, c = the victim's eidignored
USE_ON_BLOCKRight-click on a blockeid, x y z, c = face1 means handled
CRAFTA recipe producing this item matches (each time the result slot fills)eid, x = grid width, y = result count, damage = result damage. Ingredients via srvitem::craft_slot(i) and craft_nbt(i)srvitem::craft_out(Some(d)) sets the result damage, craft_out(None) cancels the result, 0 leaves it alone

Inside a hook, use srvitem (read the held item, wear it, shrink the stack, give items) and srvblock (read and write the world). From the items_basic example, a chisel that turns cobblestone into stone bricks and loses 1 durability:

        ev::USE_ON_BLOCK => {
            if srvblock::get(h.x, h.y, h.z).map(|s| s >> 4) != Some(COBBLESTONE) {
                return 0;
            }
            srvblock::set(h.x, h.y, h.z, STONEBRICK << 4);
            srvblock::sound(h.x as f32 + 0.5, h.y as f32 + 0.5, h.z as f32 + 0.5, "dig.stone", 1.0, 0.8);
            srvitem::damage_held(h.eid, 1);
            1
        }

A block state is id << 4 | meta.

Textures

A table entry's .texture("x.png") does this for you. By hand, call item::model("name", "texture.png") from client. It borrows a vanilla item transform for how the item sits in the hand: the wooden tool of the same kind for tools, the apple for everything else. Embed the texture with the res: field.

Your mod automatically gets its own creative tab. To split items over several tabs, call reg::creative_tab(json) (id, title, icon, items, hide) during registration.

Doing it by hand, without a table

#![no_std]
use mc189_mod_sdk::prelude::*;
use mc189_mod_sdk::srvitem::{ev, Hook};
use mc189_mod_sdk::item;

const CHARM: &str = r#"{"name":"moon_charm","display":"Moon charm","stack":16,"hooks":["use"],"lore":["Chimes when used"]}"#;
const RECIPE: &str = r#"{"type":"shaped","pattern":["GG","GS"],"key":{"G":"minecraft:glowstone_dust","S":"minecraft:stick"},"result":{"item":"moon_charm:moon_charm"}}"#;

fn common() {
    // same order on both sides, no conditions
    reg::item_def(CHARM);
    reg::recipe(RECIPE);
}

fn client() {
    item::model("moon_charm", "charm.png");
}

fn item_hook(h: &Hook) -> i32 {
    // h.item counts blocks, items and mobs together; this mod registers one item, so it is 0
    if h.item == 0 && h.ev == ev::USE {
        if let Some((_, x, y, z)) = srvblock::entity(h.eid) {
            srvblock::sound(x as f32, y as f32, z as f32, "note.pling", 0.6, 1.6);
        }
    }
    0
}

veil_mod! {
    id: "moon_charm",
    name: "Moon charm",
    version: "1.0.0",
    description: "An item that chimes when used",
    authors: ["me"],
    groups: ["base", "content", "server"],
    common: common,
    client: client,
    item_hook: item_hook,
    res: ["charm.png" => include_bytes!("charm.png")],
}

Common pitfalls

  • h.item does not match. It is not a numeric item id but the registration index, counting blocks, items and mobs together from 0. Use CONTENT.item("…"), or keep the value reg::item_def returned. Do not compare it with srvitem::num("name"): that is this world's item number (the one give and held use), a different thing.
  • The recipe does not work. Every character in pattern must appear in key, and every ingredient name must exist. The server log says which recipe it dropped.
  • item_hook never fires. Is the event in the JSON hooks list? Is item_hook: in veil_mod!?
  • The mod will not load. An item has both food and tool, or a field has a value it does not accept. One bad definition fails the whole mod. A misspelt field name does not fail: unknown fields are silently ignored, so check spelling against the table.

Examples: items_basic (tools, food, recipes, furnace), srv_showcase (a sword with a custom enchantment), creative_tab_demo.