Files
2026-11-14 12:18:41 -03:00

227 lines
21 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 the server phase.
## 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/chests/ruin.json` | `my_mod:chests/ruin` |
| `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 `loot_table` of the `bworld:fishable` block the hook is in, `bworld:gameplay/fishing` for water, rolled when it's reeled in. See [Fishing](#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.
## Fishing
The fishing tables aren't files: the base game builds them from every item with the `bworld:fish` component, so a mod
adds a fish by giving its item the component. A fish lives in one fluid, water unless its `fluid` says otherwise, and is
in that fluid's table: the `loot_table` of its `bworld:fishable` component, `bworld:gameplay/fishing` when it doesn't
name one. Water's is `bworld:gameplay/fishing` and lava's is `bworld:gameplay/lava_fishing`:
```json
"components": {
"bworld:fish": {
"weight": 20,
"seasons": ["summer", "autumn"],
"time": { "from": 20, "to": 4 },
"biomes": ["bworld:swamp", "#bworld:is_river"]
}
}
```
| Field | What |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `weight` | How often it's caught against the other fish, 1 when not given. |
| `seasons` | The [seasons](../worldgen/biomes.md#seasons) it's caught in where the hook is, every one when not given. |
| `fluid` | The `bworld:fishable` block it's caught in, `bworld:water` when not given, like `"bworld:lava"`. |
| `biomes` | The [biomes](../worldgen/biomes.md#biome-ids) and [biome tags](../worldgen/biomes.md#biome-tags) it's caught in where the hook is, every one when not given. |
| `time` | The hours it's caught at on a 24 hour clock, from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. All day when not given. |
| `minigame` | What the player does to land it once it's reeled in, in a screen that holds the fish until they win. Closing it lets the fish get away. Caught right away when not given. The base game's are `"test"`, a Catch button, `"hold"`, a bar that fills while the mouse is held and empties while it isn't: full lands the fish, empty loses it, `"valley"`, Stardew Valley's, where holding the mouse lifts a bar to keep the fish in it, `"ring"`, where a green ring grows out toward gray ones and clicking while it's inside each lands the fish, `"maze"`, where the mouse goes from the start to the end of a maze without touching its walls, in 8 seconds, and `"chess"`, a mate in one from Lichess's puzzles to find in 8 seconds. |
| `difficulty` | How hard its minigame is, 10 when not given. In `"hold"`, the bar empties `1 + difficulty / 100` times as fast as it fills: 1.1 at 10, 2 at 100. In `"ring"`, the green ring grows `1 + difficulty / 100` times as fast, and there's a gray ring to click for every 30 difficulty: 1 up to 30, 2 up to 60, 3 up to 90, 4 up to 120. In `"maze"`, the maze is 4 cells a side and 1 more for every 25 difficulty. In `"valley"`, it's Stardew Valley's fish difficulty: the higher, the more the fish darts around. |
When every fish can be ruled out, nothing is caught, so keep one that's always caught.
`/fish` reels in what the table gives where you stand, without casting, and `/fish <fish>` reels in that fish, minigame
and all, for trying them out.
Those become the `bworld:season`, `bworld:biome` and `bworld:time` conditions, which any table can use too:
| `condition` | Fields | Holds when |
| --------------- | ------------ | ----------------------------------------------------------------------------- |
| `bworld:season` | `seasons` | The season where it happens is one of `seasons`, like `["summer", "autumn"]`. |
| `bworld:biome` | `biomes` | The biome where it happens is one of `biomes`, or in one of its `#tags`. |
| `bworld:time` | `from`, `to` | The hour is from `from` up to `to`, the same as the fish's `time`. |
## From a Script
The server phase rolls tables, fills containers, and adds conditions, functions and changes of its own:
```ts
import type { ServerContext } from "cubi/server";
export function server(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. |
| `register_table(id, table)` | A table made by the script instead of a file, `table` being what a file's `"loot_table"` is, like the fishing table. 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");
```