diff --git a/client/mods.ts b/client/mods.ts index c0a8d57..8bd47f2 100644 --- a/client/mods.ts +++ b/client/mods.ts @@ -117,6 +117,12 @@ export function predict_neighbors(x: number, y: number, z: number) { let recipe_book = new RecipeBook(); const mod_names = new Map(); +// the name of the mod whose namespace an id is in, or the namespace when no loaded mod has it +function mod_name(id: string) { + const namespace = id.split(":")[0]; + return mod_names.get(namespace) ?? namespace; +} + export function set_mods_client(game_client: Client) { client = game_client; } @@ -343,12 +349,17 @@ function client_context(listing: ModListing): ClientContext { ids: () => EverythingRegistry.entries("items").map(([id]) => id), mod(id) { known_item(id); - const namespace = id.split(":")[0]; - return mod_names.get(namespace) ?? namespace; + return mod_name(id); }, }, blocks: blocks_api(mod), - fluids: fluid_rules.api(), + fluids: { + ...fluid_rules.api(), + mod(id) { + if (!EverythingRegistry.get("fluids", id)) throw new Error(`[${mod}] unknown fluid ${id}`); + return mod_name(id); + }, + }, loot: { table: (id) => structuredClone(EverythingRegistry.get("loot_tables", id)), }, diff --git a/common/fluids.ts b/common/fluids.ts index 1a43ee5..6209d2e 100644 --- a/common/fluids.ts +++ b/common/fluids.ts @@ -47,6 +47,7 @@ export class FluidRules { // ctx.fluids for server and client scripts api(): FluidApi { return { + ids: () => EverythingRegistry.entries("fluids").map(([id]) => id), exists: (id) => EverythingRegistry.get("fluids", id) !== undefined, name: (fluid) => this.name(fluid), color: (fluid) => this.color(fluid), diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index 0dbd2f7..7eef23b 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -71,7 +71,10 @@ export interface ClientContext { solid(id: Id): boolean; }; // every fluid, named and colored by its rules, see docs/modding/resources/fluids.md - fluids: FluidApi; + fluids: FluidApi & { + // the mod that registered it, like ctx.items.mod + mod(id: Id): string; + }; // the mods' loot table files (see docs/modding/resources/loot_tables.md), to show what they give. tables server // scripts make with ctx.loot.register_table aren't here loot: { diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index f006a49..423a068 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -26,6 +26,8 @@ export interface FluidVariant { // ctx.fluids in server and client scripts: every fluid any mod defines, the same on the server and every client export interface FluidApi { + // every fluid's id, in load order, like ctx.items.ids. each without data, which a fluid like juice has many of + ids(): Id[]; exists(id: Id): boolean; // what players see, from the fluid's rules for its data, or else its json name(fluid: FluidVariant): string; diff --git a/docs/modding/gui/index.md b/docs/modding/gui/index.md index a5a6bdb..15eb202 100644 --- a/docs/modding/gui/index.md +++ b/docs/modding/gui/index.md @@ -168,6 +168,8 @@ ctx.items.mod(id); // the name of the mod that added it ctx.items.components(id); // its components and their params, like the server's, to show what they mean ctx.blocks.exists(id); ctx.blocks.components(id); // the same for a block, like what a crab pot catches with each bait +ctx.fluids.ids(); // every fluid, in load order +ctx.fluids.mod(id); // the name of the mod that added it ctx.fluids.name(fluid); // and color, exists and same, see ../resources/fluids.md#using-fluids ctx.loot.table(id); // a loot table file's JSON, or undefined. tables server scripts make aren't here ctx.recipes.list(type?); // every recipe as its JSON, including mods' own types diff --git a/docs/modding/resources/fluids.md b/docs/modding/resources/fluids.md index 9f6df2b..519cb73 100644 --- a/docs/modding/resources/fluids.md +++ b/docs/modding/resources/fluids.md @@ -98,12 +98,17 @@ Server and client scripts have `ctx.fluids`, which goes through the rules: ```ts const birch = { id: "copper_tools:syrup", data: { tree: "bworld:birch_log" } }; +ctx.fluids.ids(); // every fluid's id in load order, like ["bworld:juice", "copper_tools:syrup"] ctx.fluids.exists("copper_tools:syrup"); // true ctx.fluids.name(birch); // "Birch Log Syrup" ctx.fluids.color(birch); // "#d9a441" ctx.fluids.same(birch, { id: "copper_tools:syrup" }); // false, the data is different ``` +`ids` gives each fluid once, without data, so a fluid that's many things by its data, like syrup from each tree, is only +there as plain `copper_tools:syrup`. Client scripts also have `ctx.fluids.mod(id)`, the name of the mod that added it, +like `ctx.items.mod`. + ## Containers An item with `fluid_container` holds one fluid, full or empty, like a bucket or a bottle: diff --git a/tests/fluid_registry_test.ts b/tests/fluid_registry_test.ts index 8ff3eb4..39d131d 100644 --- a/tests/fluid_registry_test.ts +++ b/tests/fluid_registry_test.ts @@ -133,3 +133,11 @@ Deno.test("fluids are the same when their ids and data are, whatever order the d assertEquals(server, [true, true, false, false, false]); assertEquals(client, server); }); + +Deno.test("every fluid's id is listed once, without data, on the server and clients", async () => { + const sides = await both_sides((fluids) => fluids.ids()); + for (const ids of [sides.server, sides.client] as string[][]) { + assert(ids.includes("testfluids:syrup") && ids.includes("testfluids:tree_sap"), ids.join()); + assertEquals(new Set(ids).size, ids.length); + } +});