Loot tables

This commit is contained in:
2026-10-08 00:54:45 -03:00
parent 757ee74bc7
commit ec2b53c02a
45 changed files with 1733 additions and 150 deletions
+66 -24
View File
@@ -25,6 +25,7 @@ the day before grows a day, then all farmland dries out. The crop shows the stag
"render_layer": "cutout",
"collision": false,
"mining": { "toughness": 0 },
"loot_table": "farmersdelight:blocks/cabbages",
"item": false,
"place_on": ["bworld:hoed_dirt"],
"states": [{ "name": "age", "bits": 3, "default": 0 }],
@@ -32,12 +33,7 @@ the day before grows a day, then all farmland dries out. The crop shows the stag
"bworld:crop": {
"days": 12,
"stages": 8,
"fertilizer": "bworld:bone_meal",
"drops": {
"young": "farmersdelight:cabbage_seeds",
"mature": "farmersdelight:cabbage",
"bonus": "farmersdelight:cabbage_seeds"
}
"fertilizer": "bworld:bone_meal"
}
}
}
@@ -59,18 +55,56 @@ the day before grows a day, then all farmland dries out. The crop shows the stag
`place_on` keeps it on farmland, and it pops off when the farmland is dug up.
## Drops
What a crop drops is its block's [loot table](../resources/loot_tables.md), with a `block_state` condition on `age` for
grown or not. `loot_tables/blocks/cabbages.json` gives a seed while it grows, and once it's grown at age 7 the cabbage
and 1 to 4 seeds, like Minecraft's wheat:
```json
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "farmersdelight:cabbage",
"conditions": [{ "condition": "block_state", "states": { "age": 7 } }]
},
{
"item": "farmersdelight:cabbage_seeds",
"conditions": [
{ "condition": "inverted", "term": { "condition": "block_state", "states": { "age": 7 } } }
]
}
]
},
{
"conditions": [{ "condition": "block_state", "states": { "age": 7 } }],
"entries": [
{
"item": "farmersdelight:cabbage_seeds",
"functions": [{ "function": "set_count", "count": { "n": 3, "p": 0.5714 }, "add": true }]
}
]
}
]
}
}
```
## Parameters
| Param | Meaning |
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `days` | Watered mornings from planting to fully grown. |
| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. |
| `drops` | What it drops when broken: `young` before it's grown, `mature` once it is, and `bonus`, 1 to 4 of an item when grown (Minecraft's 1 plus 3 tries at 57%, like wheat's seeds). `young` and `mature` can be one id or a list. |
| `fertilizer` | An item that grows it a day right away when used on it, like bone meal. |
| `water` | `"farmland"` (the default) needs watered farmland under it. `"always"` grows every morning. |
| `regrow_days`, `harvest` | Right clicking it once grown picks it, dropping `harvest.item` (`count` is `[min, max]`), and it grows back after `regrow_days`. |
| `upper` | `{ block, stages, supporting_age }`: its last stages grow a second block on top, see below. |
| `leaves` | What the spot becomes when it's broken, like the water rice was planted in. |
| Param | Meaning |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `days` | Watered mornings from planting to fully grown. |
| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. |
| `fertilizer` | An item that grows it a day right away when used on it, like bone meal. |
| `water` | `"farmland"` (the default) needs watered farmland under it. `"always"` grows every morning. |
| `regrow_days`, `harvest` | Right clicking it once grown picks it, dropping what the [loot table](../resources/loot_tables.md) `harvest` gives, and it grows back after `regrow_days`. |
| `upper` | `{ block, stages, supporting_age }`: its last stages grow a second block on top, see below. |
| `leaves` | What the spot becomes when it's broken, like the water rice was planted in. |
## Harvesting By Hand
@@ -81,10 +115,17 @@ Tomatoes are picked instead of broken:
"days": 11,
"stages": 8,
"regrow_days": 4,
"harvest": { "item": "farmersdelight:tomato", "count": [1, 2] },
"drops": {
"young": "farmersdelight:tomato_seeds",
"mature": ["farmersdelight:tomato", "farmersdelight:tomato_seeds"]
"harvest": "farmersdelight:harvest/tomatoes"
}
```
with `loot_tables/harvest/tomatoes.json` giving 1 or 2 tomatoes:
```json
{
"format_version": 1,
"loot_table": {
"pools": [{ "entries": [{ "item": "farmersdelight:tomato", "count": { "min": 1, "max": 2 } }] }]
}
}
```
@@ -103,17 +144,18 @@ block goes through its own `stages`:
"stages": 4,
"water": "always",
"upper": { "block": "farmersdelight:rice_panicles", "stages": 4, "supporting_age": 4 },
"leaves": "bworld:water",
"drops": { "young": "farmersdelight:rice", "mature": "farmersdelight:rice" }
"leaves": "bworld:water"
}
}
```
The top block uses `bworld:crop_top` with its own `stages` and `drops`. Breaking either half harvests the whole plant:
The top block uses `bworld:crop_top` with its own `stages`, and its own loot table. Breaking either half harvests the
whole plant:
```json
"loot_table": "farmersdelight:blocks/rice_panicles",
"components": {
"bworld:crop_top": { "stages": 4, "drops": { "mature": "farmersdelight:rice_panicle" } }
"bworld:crop_top": { "stages": 4 }
}
```
+4 -3
View File
@@ -60,7 +60,8 @@ there will be holes in the world behind them.
| `mining.tool` | none | The [tool type](../items/tools.md) that breaks it faster, like `pickaxe`, `axe` or `shovel`. |
| `mining.requires_tool` | `false` | Only drops when broken with `mining.tool`. |
| `mining.tier` | `0` | With `requires_tool`, the lowest tool tier it drops for. Iron ore is `1`, so a wood pickaxe gets nothing. |
| `drops` | nothing | The item id it drops when broken. |
| `drops` | nothing | The item id it drops when broken, one of it. |
| `loot_table` | none | A [loot table](../resources/loot_tables.md) it drops from when broken instead, with its states and the tool. Not with `drops`. |
| `item` | `true` | Also register an item that places it, with the same id. To give that item more, see [Block Items](../items/index.md#block-items). |
| `interactive` | `false` | Right clicking it does something, like opening a screen, so clients don't guess that it places a block. |
| `replaceable` | `false` | Placing a block into it replaces it, like water. Liquids default to `true`. |
@@ -79,8 +80,8 @@ It can't hold functions.
Breaking takes `mining.toughness` seconds by hand. A tool whose type matches `mining.tool` breaks it `tool.speed` times
faster. The client times breaking itself, and the server checks the result.
When it breaks, the block's components' `on_break` runs and it drops `drops`, unless it has `requires_tool` and the
player's tool is the wrong type or a tier below `mining.tier`.
When it breaks, the block's components' `on_break` runs and it drops `drops` or what its `loot_table` gives, unless it
has `requires_tool` and the player's tool is the wrong type or a tier below `mining.tier`.
## Placement
+1
View File
@@ -34,6 +34,7 @@ and players. Mods define new kinds in their `entities/` folder, and draw them wi
| `attack_damage` | `2` | What a monster's hits do, in half hearts. |
| `physical_shielding` | `0` | Protection from physical damage. Not used yet. |
| `arcane_shielding` | `0` | Protection from arcane damage. Not used yet. |
| `loot_table` | none | A [loot table](../resources/loot_tables.md) it drops from when it dies. |
`/summon <entity>` spawns one where you stand.
+2 -1
View File
@@ -19,6 +19,7 @@ mods/
effects/*.json # status effects, see entities/effects.md
recipes/*.json
tags/<registry>/<namespace>/*.json # tags, see resources/tags.md
loot_tables/**/*.json # loot tables, see resources/loot_tables.md
worldgen/ores.json
worldgen/spawns.json # monsters that spawn on their own, see entities/index.md
textures/*.png
@@ -114,7 +115,7 @@ deno task build # packs every mod in mods/ into server_mo
```
copper_tools.bmod
manifest.json # with scripts and credits pointing into the zip
data.json # every block, model, item, entity, recipe and ore
data.json # every block, model, item, entity, recipe, ore, tag and loot table
scripts/server.js # each script bundled into one module
scripts/client.js
scripts/worldgen.js
+184
View File
@@ -0,0 +1,184 @@
# 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");
```