Files
bworld/docs/modding/resources/loot_tables.md
T
2026-10-08 00:54:45 -03:00

185 lines
12 KiB
Markdown

# Loot Tables
A loot table says what something gives, as data: a block when it's broken, a mob when it dies, a fishing rod when it
reels something in, a crop when it's picked, and a container a script fills, like a chest in a ruin. They work like
Minecraft's, and mods can add their own conditions and functions from a server script.
## Files and Ids
Every `.json` in a mod's `loot_tables/` folder is a table, and its id is its path, like Minecraft's:
| File | Loot table id |
| ----------------------------------- | ------------------------------ |
| `loot_tables/blocks/wheat.json` | `bworld:blocks/wheat` |
| `loot_tables/gameplay/fishing.json` | `bworld:gameplay/fishing` |
| `loot_tables/entities/rotling.json` | `undergarden:entities/rotling` |
The folders are only for keeping things tidy. By habit, `blocks/` is for blocks, `entities/` for mobs, `gameplay/` for
things like fishing and `chests/` for containers.
## A Table
`loot_tables/blocks/wheat.json`, the base game's wheat: its seeds when it's young, and once it's grown its wheat and 1
to 4 seeds:
```json
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{ "item": "bworld:wheat", "conditions": [{ "condition": "block_state", "states": { "age": 7 } }] },
{
"item": "bworld:wheat_seeds",
"conditions": [
{ "condition": "inverted", "term": { "condition": "block_state", "states": { "age": 7 } } }
]
}
]
},
{
"conditions": [{ "condition": "block_state", "states": { "age": 7 } }],
"entries": [
{
"item": "bworld:wheat_seeds",
"functions": [{ "function": "set_count", "count": { "n": 3, "p": 0.5714 }, "add": true }]
}
]
}
]
}
}
```
A table has **pools**. Each pool is rolled `rolls` times, and each roll picks one of its **entries** whose conditions
hold, by `weight`. What every pool gives together is what the table gives.
| Where | Field | Default | Meaning |
| ----- | ------------ | ------- | --------------------------------------------------------------------------- |
| table | `pools` | | The pools. |
| table | `conditions` | none | Unless they all hold, the table gives nothing. |
| table | `functions` | none | Change every item the table gives. |
| pool | `rolls` | `1` | How many times it picks an entry, a [number](#numbers). |
| pool | `entries` | | What it picks from. |
| pool | `conditions` | none | Unless they all hold, the pool isn't rolled. |
| pool | `functions` | none | Change every item the pool gives. |
| entry | `type` | `item` | See [entries](#entries). |
| entry | `weight` | `1` | How likely it's picked against the other entries that can be. `0` is never. |
| entry | `conditions` | none | Unless they all hold, it can't be picked. |
| entry | `functions` | none | Change the items it gives. |
## Entries
| `type` | Field | Gives |
| ------------ | ------- | --------------------------------------------------------------------------------------- |
| `item` | `item` | That item. `count`, a [number](#numbers), is how many, `1` when not set. |
| `tag` | `tag` | One item of an item [tag](tags.md) at random, like `"#bworld:logs"`. Takes `count` too. |
| `loot_table` | `table` | What rolling another table gives, like a shared list of treasure. |
| `empty` | | Nothing. With a `weight`, it's the chance a roll gives nothing. |
Fishing's table picks cod 60 times out of 85 and salmon 25:
```json
"pools": [{ "entries": [{ "item": "bworld:cod", "weight": 60 }, { "item": "bworld:salmon", "weight": 25 }] }]
```
## Numbers
Counts and rolls are whole numbers, picked one of three ways:
| Written | Means |
| ------------------------- | ----------------------------------------------------------------------------- |
| `3` | Always 3. |
| `{ "min": 1, "max": 3 }` | 1, 2 or 3, each as likely. |
| `{ "n": 3, "p": 0.5714 }` | How many of 3 tries at a 57.14% chance work, 0 to 3. Minecraft's bonus seeds. |
## Conditions
| `condition` | Fields | Holds when |
| ------------------ | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `random_chance` | `chance` | A random number is below `chance`, from 0 to 1. |
| `match_tool` | `items`, `tool`, `min_tier` | It's done with an item in `items` (ids and #tags), of [tool](../items/tools.md) type `tool`, of at least `min_tier`. Each is optional. |
| `block_state` | `states`, `block` | The block's states match, each a number or `{ "min", "max" }`, like `{ "age": { "min": 3 } }`. `block` checks its id too. |
| `killed_by_player` | | A player killed the mob. |
| `inverted` | `term` | `term` doesn't hold. |
| `any_of` | `terms` | One of `terms` holds. |
| `all_of` | `terms` | They all hold. |
A condition with an id, like `my_mod:full_moon`, is a mod's own, see [below](#from-a-script).
## Functions
Functions run in order, first an entry's, then its pool's, then the table's. Any function can have `conditions`, and
does nothing unless they hold.
| `function` | Fields | Does |
| ------------- | -------------- | ---------------------------------------------------------------------- |
| `set_count` | `count`, `add` | Sets the count to a [number](#numbers), or adds it with `"add": true`. |
| `limit_count` | `min`, `max` | Keeps the count between them. |
| `set_data` | `data` | Sets the item's data, any JSON, like what a map leads to. |
## Where Tables Are Used
| What | How |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| [Blocks](../blocks/index.md) | `"loot_table"` in the block's json, rolled when it breaks, with its states and the tool. `"drops"` is the shorthand for one of an item. |
| [Mobs](../entities/index.md) | `"loot_table"` in the entity's json, rolled when it dies. `killed_by_player` holds when a player killed it. |
| [Crops](../blocks/crops.md) | Their block's `loot_table`, with `block_state` on `age` for grown or not. `harvest` is a table for picking. |
| Fishing | The fishing rod's `loot_table`, `bworld:gameplay/fishing`. |
| Scripts | `ctx.loot.roll` and `ctx.loot.fill`, for anything else, like chests in structures. |
`check-mods` checks that every item, tag and table a table names exists, and that the tables blocks and mobs name do.
## From a Script
Server scripts roll tables, fill containers, and add conditions, functions and changes of their own:
```ts
import type { ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
// "condition": "my_mod:night" in any table
ctx.loot.register_condition("my_mod:night", (_params, context) => ctx.world.time % 24000 > 13000);
// "function": "my_mod:named", "name": "..." gives the item a name in its data
ctx.loot.register_function("my_mod:named", (item, params) => ({ ...item, data: { name: params.name } }));
// every fish a player catches comes with a coin, like neoforge's global loot modifiers
ctx.loot.modify("bworld:gameplay/fishing", (items) => {
items.push({ id: "bworld:copper_coin", count: 1 });
});
}
```
| `ctx.loot` | Does |
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `exists(id)` | Whether the table exists. |
| `roll(id, context?)` | What it gives, in stacks that fit a slot. Throws for a table that doesn't exist. |
| `fill(container, id, context?)` | Rolls it into the container's empty slots at random, like Minecraft's chests. Returns what didn't fit. |
| `register_condition(id, fn)` | A condition: `fn(params, context)` says whether it holds. Setup only. |
| `register_function(id, fn)` | A function: `fn(item, params, context)` gives the changed item, or `undefined` to take it away. Setup only. |
| `modify(table, fn)` | Changes what a table gives each time, or every table's with `"*"`. `fn(items, context, table)` changes the list or returns a new one. Setup only. |
`params` is the condition's or function's json. Ids are in the mod's namespace, and a table that uses one nobody
registered stops the server from starting, like a missing [component](../blocks/components.md). What a mod's code throws
is logged under the mod, and the roll carries on as if that condition didn't hold or that function did nothing.
The **context** says what's happening. Every part is optional:
| Field | What |
| ------------------ | ----------------------------------------------------------------------------------------------------- |
| `position` | Where it happens. |
| `block` | `{ id, x, y, z, states }`, the block being broken. Its states are read from the world when not given. |
| `tool` | What it's done with, like the pickaxe or the fishing rod. |
| `player` | Who did it. |
| `entity` | The mob that died. |
| `killed_by_player` | A player killed it. |
The engine gives blocks and mobs all they have. A script rolling a table gives what its conditions need:
```ts
const loot = ctx.loot.roll("my_mod:chests/ruin", { position: { x, y, z } });
const left = ctx.loot.fill(chest, "my_mod:chests/ruin");
```