More fluid apis
This commit is contained in:
+14
-3
@@ -117,6 +117,12 @@ export function predict_neighbors(x: number, y: number, z: number) {
|
||||
let recipe_book = new RecipeBook();
|
||||
const mod_names = new Map<string, string>();
|
||||
|
||||
// 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<LootTableJson>("loot_tables", id)),
|
||||
},
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user