Implement tags !

This commit is contained in:
2026-09-30 13:41:29 -03:00
parent dc70ada7c4
commit 342b45d593
51 changed files with 978 additions and 219 deletions
+12 -4
View File
@@ -68,15 +68,23 @@ recipes with the same tool, material and addition are an error.
## Choices
Anywhere a recipe takes an item (`key` values, `input`, `item`), a list of ids means any of them fits:
Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and
so does a list of ids and tags:
```json
"key": { "P": ["bworld:planks", "bworld:birch_planks", "bworld:spruce_planks"] }
"key": { "P": "#bworld:planks" }
```
```json
"item": ["bworld:coal", "#bworld:logs"]
```
In the crafting grid, each slot can hold a different one of them, so a stick can be made from oak and birch planks
together. The recipe viewer takes turns showing them.
Use a tag rather than listing ids when other mods might have more of the same thing: a mod that adds planks to
`#bworld:planks` makes them work in every recipe that takes it.
## Custom Recipe Types
Any `type` with a namespace is a mod's own, like `farmersdelight:cooking` for the cooking pot. The engine only stores
@@ -101,8 +109,8 @@ const recipes = ctx.recipes.list("farmersdelight:cooking").map((json, i) => {
if (!Array.isArray(json.ingredients) || json.ingredients.length > 6) {
throw new Error(`cooking recipe ${i + 1}: ingredients must be 1 to 6 items`);
}
// ...check the rest, and turn it into whatever the machine needs
return json;
// ...check the rest, and turn it into whatever the machine needs. expand turns #tags into their items
return { ingredients: json.ingredients.map((choice) => ctx.tags.expand(choice)) };
});
```
+108
View File
@@ -0,0 +1,108 @@
# Tags
A tag is a named list of blocks, items or entities, like every kind of planks. Recipes and blocks can use a tag anywhere
they take an id, and any mod can add to any tag, so a mod's planks can make sticks just by adding them to
`#bworld:planks`. Tags work like [NeoForge's](https://docs.neoforged.net/docs/resources/server/tags).
A tag's id starts with `#` where it's used, so `#bworld:planks` is the tag and `bworld:planks` is the oak planks item.
## Tag Files
Tags are data in a mod's `tags/` folder, at `tags/<registry>/<namespace>/<path>.json`:
```
tags/
items/
bworld/planks.json # the item tag #bworld:planks
c/ingots/copper.json # #c:ingots/copper
blocks/
my_mod/soft.json # the block tag #my_mod:soft
```
`<registry>` is `blocks`, `items` or `entities`. A block tag and an item tag with the same id are two different tags, so
a block that should be in both, like planks, needs a file for each.
```json
{
"format_version": 1,
"tag": {
"values": [
"my_mod:maple_planks",
"#my_mod:painted_planks",
{ "id": "other_mod:birch_planks", "required": false }
]
}
}
```
| Field | Default | Meaning |
| --------- | ------- | -------------------------------------------------------------------------------------- |
| `values` | | What's in the tag: ids, and `#tags` whose contents are all in it too. |
| `replace` | `false` | Throws away what mods loaded before this one put in the tag. |
| `remove` | none | Ids and `#tags` to take out of the tag again, if they're in it. Written like `values`. |
An entry in `values` must exist, or the tag doesn't load and the server doesn't start. Write it as
`{ "id": ..., "required": false }` when it's fine for it to be missing, like another mod's item that may not be
installed. What `remove` lists never has to exist.
A tag can't contain itself, directly or through other tags.
## Adding to Other Mods' Tags
A mod's tag files can be in any namespace. Every mod's file for the same tag is merged, in
[load order](../concepts/registries.md#loading), so adding to someone else's tag only adds to it. This is
`mods/maple/tags/items/bworld/planks.json`:
```json
{ "format_version": 1, "tag": { "values": ["maple:maple_planks"] } }
```
`replace` and `remove` apply in load order too: a mod can take out what an earlier mod added, and a later mod can add it
back. Depend on the mod whose tag you change, so your file loads after its. check-mods warns when you don't.
## Choosing a Namespace
- A tag for your own mod's behavior, like the blocks your drill can't break, goes in your namespace.
- A tag other mods would want to share, like a new metal's ingots, goes in the `c` namespace, like NeoForge's and
Fabric's shared tags. No mod owns `c`, so using it needs no dependency.
- Tag names are plural (`planks`, `ingots`). Group them with folders, with a tag for the whole folder holding each tag
in it: `#c:ingots` holds `#c:ingots/iron`, `#c:ingots/copper` and so on.
## The Base Game's Tags
| Tag | Registry | What's in it |
| ---------------- | ------------- | ------------------------------------------------------------------------ |
| `#bworld:planks` | items, blocks | Every kind of planks. |
| `#bworld:logs` | items, blocks | Every kind of log. |
| `#bworld:leaves` | blocks | Every kind of leaves. |
| `#c:ingots` | items | `#c:ingots/copper`, `#c:ingots/tin`, `#c:ingots/iron`, `#c:ingots/gold`. |
## Using Tags in Data
- **Recipes**: anywhere a recipe takes an item (shaped `key` values, furnace `input`, fuel `item`) it can take an item
tag, on its own or in a list: `"key": { "P": "#bworld:planks" }`. See [Choices](recipes.md#choices).
- **Blocks**: `place_on` and `place_in` take block tags, like `"place_on": ["#bworld:logs"]` for a fungus that grows on
any log. See [Placement](../blocks/index.md#placement).
A tag nobody defines is a check-mods error there.
## Using Tags in Scripts
Server and client scripts both have `ctx.tags`. Tags can be written with or without their `#`:
```ts
ctx.tags.has("items", "bworld:planks", item.id); // whether it's in the tag
ctx.tags.get("blocks", "#bworld:logs"); // every id in it, including its tags' ids
ctx.tags.of("items", "bworld:iron_ingot"); // every tag it's in: ["c:ingots", "c:ingots/iron"]
ctx.tags.exists("items", "c:ingots/tin"); // whether any mod defines it
ctx.tags.expand(["#bworld:planks", "bworld:stick"]); // every item a recipe's choice stands for
```
`has` and `get` treat a tag nobody defines as empty.
Mods' own [recipe types](recipes.md#custom-recipe-types) should take tags like the base game's do. `expand` turns a
choice into the items it stands for, so the mod's server script can check and match them:
```ts
const ingredient = ctx.tags.expand(json.ingredient); // "#bworld:planks" -> ["bworld:planks", "bworld:spruce_planks", ...]
```