Make the fishing loot table be generated from the fish items

This commit is contained in:
2026-10-11 11:40:44 -03:00
parent 5a59a63862
commit ab2b8e6545
9 changed files with 86 additions and 47 deletions
+6
View File
@@ -1,6 +1,7 @@
// what server scripts get, see docs/modding/
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type { ArgumentType, CommandNode } from "../commands.ts";
import type { LootTableJson } from "../loot.ts";
export type { ArgumentType, CommandContext, CommandNode, Suggestion } from "../commands.ts";
@@ -375,6 +376,8 @@ export interface ContainerApi {
export interface ItemApi {
exists(id: Id): boolean;
// every item, in load order
ids(): Id[];
// how many fit in one slot
max_stack(id: Id): number;
// the tool type it is, like "pickaxe" or "shovel", from the item's "tool"
@@ -443,6 +446,9 @@ export interface LootApi {
// changes what a table gives every time it's rolled, or every table's with "*", like neoforge's global loot
// modifiers. return the new items, or change the list. only during setup
modify(table: Id | "*", fn: (items: ItemStack[], context: LootContext, table: Id) => ItemStack[] | void): void;
// a table made by the script instead of a file, like the base game's fishing table built from its fish. the json
// is what a file's "loot_table" is, the id is in the mod's namespace. only during setup
register_table(id: Id, table: Omit<LootTableJson, "id">): void;
}
// forms, not built yet: menus whose screens the engine provides, see docs/modding_plan.md
+1
View File
@@ -166,6 +166,7 @@ watering can uses `bworld:water_source`, or subscribe to the `block_interact` [e
```ts
ctx.items.exists("bworld:carrot"); // true
ctx.items.ids(); // every item, in load order
ctx.items.max_stack("bworld:wood_pickaxe"); // 1
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value
+24 -16
View File
@@ -11,7 +11,7 @@ Every `.json` in a mod's `loot_tables/` folder is a table, and its id is its pat
| File | Loot table id |
| ----------------------------------- | ------------------------------ |
| `loot_tables/blocks/wheat.json` | `bworld:blocks/wheat` |
| `loot_tables/gameplay/fishing.json` | `bworld:gameplay/fishing` |
| `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
@@ -133,26 +133,33 @@ does nothing unless they hold.
## Fishing
The base game adds two conditions for picking fish by where and when they're reeled in:
| `condition` | Fields | Holds when |
| --------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `bworld:season` | `seasons` | The [season](../worldgen/biomes.md#seasons) where it happens is one of `seasons`, like `["summer", "autumn"]`. |
| `bworld:time` | `from`, `to` | The hour on a 24 hour clock is from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. |
A fish without a `bworld:season` is caught in every season, and one without `bworld:time` all day:
The fishing table, `bworld:gameplay/fishing`, isn't a file: the base game builds it from every item with the
`bworld:fish` component, so a mod adds a fish by giving its item the component:
```json
{
"item": "my_mod:catfish",
"conditions": [
{ "condition": "bworld:season", "seasons": ["summer", "autumn"] },
{ "condition": "bworld:time", "from": 20, "to": 4 }
]
"components": {
"bworld:fish": {
"weight": 20,
"seasons": ["summer", "autumn"],
"time": { "from": 20, "to": 4 }
}
}
```
When every fish can be ruled out, nothing is caught, so keep one without conditions.
| 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. |
| `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. |
When every fish can be ruled out, nothing is caught, so keep one that's always caught.
Those become the `bworld:season` 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:time` | `from`, `to` | The hour is from `from` up to `to`, the same as the fish's `time`. |
## From a Script
@@ -183,6 +190,7 @@ export function setup(ctx: ServerContext) {
| `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
+6
View File
@@ -7,6 +7,12 @@
"nutrition": 2,
"saturation_modifier": 0.1
},
"components": {
"bworld:fish": {
"weight": 60,
"seasons": ["spring", "summer", "winter"]
}
},
"base_sell_value": 30
}
}
+6
View File
@@ -7,6 +7,12 @@
"nutrition": 2,
"saturation_modifier": 0.1
},
"components": {
"bworld:fish": {
"weight": 25,
"seasons": ["summer", "autumn", "winter"]
}
},
"base_sell_value": 32
}
}
@@ -1,31 +0,0 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "bworld:cod",
"weight": 60,
"conditions": [
{
"condition": "bworld:season",
"seasons": ["spring", "summer", "winter"]
}
]
},
{
"item": "bworld:salmon",
"weight": 25,
"conditions": [
{
"condition": "bworld:season",
"seasons": ["summer", "autumn", "winter"]
}
]
}
]
}
]
}
}
+25
View File
@@ -619,6 +619,9 @@ function register_buckets(ctx: ServerContext) {
const FISHING_ROD = "bworld:fishing_rod";
const FISHING_HOOK = "bworld:fishing_hook";
const FISHABLE = "bworld:fishable";
const FISH = "bworld:fish";
// built from every item with bworld:fish, see register_fishing
const FISHING_TABLE = "bworld:gameplay/fishing";
// further than this from the hook, the line snaps
const LINE_LENGTH = 16;
// blocks per tick it's thrown at, and how it falls and slows down in the air each tick, like minecraft's hook
@@ -645,6 +648,16 @@ interface FishingRodParams {
loot_table: string;
}
// an item that's caught by fishing
interface FishParams {
// how often it's caught against the other fish, 1 when not given
weight?: number;
// the seasons it's caught in, every one when not given
seasons?: Season[];
// the hours it's caught at, like bworld:time's, all day when not given
time?: { from: number; to: number };
}
interface Cast {
// the hotbar slot the rod is in, switching away reels it in
slot: number;
@@ -731,6 +744,18 @@ function register_fishing(ctx: ServerContext) {
return from <= to ? hour >= from && hour < to : hour >= from || hour < to;
});
// every item with bworld:fish is in the fishing table, caught in its seasons and at its hours
ctx.components.register_item<FishParams>(FISH, {});
const entries = ctx.items.ids().flatMap((id) => {
const fish = ctx.items.components(id)[FISH] as FishParams | undefined;
if (!fish) return [];
const conditions = [];
if (fish.seasons) conditions.push({ condition: "bworld:season", seasons: fish.seasons });
if (fish.time) conditions.push({ condition: "bworld:time", ...fish.time });
return [{ item: id, weight: fish.weight ?? 1, conditions }];
});
ctx.loot.register_table(FISHING_TABLE, { pools: entries.length > 0 ? [{ entries }] : [] });
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
on_use(rod, params, player) {
const cast = casts.get(player.id);
+13
View File
@@ -10,6 +10,7 @@ import {
type LootItem,
type LootTableJson,
roll_loot,
validate_loot_table,
} from "$/common/loot.ts";
import type { ItemStack, LootContext, LootParams } from "$/common/mod_api/server.ts";
import { ModLoadError } from "$/common/mod_loader.ts";
@@ -53,6 +54,18 @@ export class LootTables {
this.#modifiers.push({ mod, table, fn });
}
// a table a script made, checked like a file's
register_table(mod: string, id: string, table: Omit<LootTableJson, "id">) {
if (!id.startsWith(`${mod}:`)) {
throw new ModLoadError(mod, `loot table ${id} must be in the namespace "${mod}"`);
}
if (this.exists(id)) throw new ModLoadError(mod, `loot table ${id} already exists`);
const json = { ...structuredClone(table), id };
const problems = validate_loot_table(json);
if (problems.length > 0) throw new ModLoadError(mod, `loot table ${id}: ${problems.join(", ")}`);
EverythingRegistry.register<LootTableJson>("loot_tables", id, json);
}
#own(mod: string, id: string, what: string, map: Map<string, unknown>) {
if (!id.startsWith(`${mod}:`)) throw new ModLoadError(mod, `${what} ${id} must be in the namespace "${mod}"`);
if (map.has(id)) throw new ModLoadError(mod, `${what} ${id} is registered twice`);
+5
View File
@@ -779,6 +779,7 @@ export class ModRuntime {
},
items: {
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
max_stack: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
@@ -856,6 +857,10 @@ export class ModRuntime {
setup_only("loot modifiers");
this.loot.modify(mod, table, fn);
},
register_table: (id, table) => {
setup_only("loot tables");
this.loot.register_table(mod, id, table);
},
},
containers: {
create: (size) => {