Fluid data

This commit is contained in:
2026-10-25 14:24:16 -03:00
parent 17d83acfc8
commit fc27771a92
12 changed files with 387 additions and 31 deletions
+65
View File
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// fluids with their data (see FluidVariant in mod_api/shared.ts) and the rules shared scripts give them, which name and
// color a fluid by its data. each side has its own FluidRules from the same scripts, so the server's tooltips and a
// client's drawing agree
import type { FluidApi, FluidRules as Rules, FluidVariant } from "./mod_api/shared.ts";
import { EverythingRegistry } from "./everything_registry.ts";
import type { FluidJson } from "./mod_data.ts";
import { color as color_schema } from "./schema.ts";
import { name_from_id, same_data } from "./utils.ts";
export class FluidRules {
#rules = new Map<string, { mod: string; rules: Rules }>();
// run runs a mod's code, reporting what it throws instead of letting it take the game down
constructor(readonly run: <T>(mod: string, what: string, fn: () => T) => T | undefined) {}
register(mod: string, id: string, rules: Rules) {
if (!id.startsWith(`${mod}:`)) throw new Error(`[${mod}] fluid ${id} must be in the namespace "${mod}"`);
if (!EverythingRegistry.get("fluids", id)) throw new Error(`[${mod}] unknown fluid ${id}`);
if (this.#rules.has(id)) throw new Error(`[${mod}] the rules of fluid ${id} are registered twice`);
this.#rules.set(id, { mod, rules });
}
name(fluid: FluidVariant): string {
const json = known_fluid(fluid.id);
const name = this.#ask(fluid, "name", "a name", (value) => typeof value === "string" && value.length > 0);
return name ?? json.name ?? name_from_id(fluid.id);
}
color(fluid: FluidVariant): string {
const json = known_fluid(fluid.id);
const valid = (value: unknown) => color_schema.safeParse(value).success;
return this.#ask(fluid, "color", 'a color like "#c42d39"', valid) ?? json.color;
}
// ctx.fluids for server and client scripts
api(): FluidApi {
return {
exists: (id) => EverythingRegistry.get("fluids", id) !== undefined,
name: (fluid) => this.name(fluid),
color: (fluid) => this.color(fluid),
same: (a, b) => a.id === b.id && same_data(a.data, b.data),
};
}
// what the fluid's rule says, or undefined when it has none, says nothing, or says something that isn't one
#ask(fluid: FluidVariant, rule: "name" | "color", wanted: string, valid: (value: unknown) => boolean) {
const entry = this.#rules.get(fluid.id);
const fn = entry?.rules[rule];
if (!entry || !fn) return undefined;
return this.run(entry.mod, `the ${rule} rule of ${fluid.id}`, () => {
const value = fn.call(entry.rules, fluid.data);
if (value !== undefined && !valid(value)) throw new Error(`gave ${JSON.stringify(value)}, not ${wanted}`);
return value;
});
}
}
function known_fluid(id: string): FluidJson {
const json = EverythingRegistry.get<FluidJson>("fluids", id);
if (!json) throw new Error(`unknown fluid ${id}`);
return json;
}
+15 -2
View File
@@ -2,10 +2,21 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// what client scripts get, see docs/modding/gui/index.md
import type { Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type { FluidApi, Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type { LootTableJson } from "../loot.ts";
export type { Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi, TagRegistry } from "./shared.ts";
export type {
FluidApi,
FluidVariant,
Id,
ItemStack,
KeyCode,
ModInfo,
Position,
Season,
TagApi,
TagRegistry,
} from "./shared.ts";
export interface ClientContext {
mod: ModInfo;
@@ -59,6 +70,8 @@ export interface ClientContext {
// whether it's solid like stone, like the server's ctx.blocks.solid
solid(id: Id): boolean;
};
// every fluid, named and colored by its rules, see docs/modding/resources/fluids.md
fluids: FluidApi;
// 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: {
+16 -2
View File
@@ -2,13 +2,25 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// what server scripts get, see docs/modding/
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type { EventSignal, Face, FluidApi, 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";
export type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, TagApi, TagRegistry } from "./shared.ts";
export type {
EventSignal,
Face,
FluidApi,
FluidVariant,
Id,
ItemStack,
ModInfo,
Position,
Season,
TagApi,
TagRegistry,
} from "./shared.ts";
export interface ServerContext {
mod: ModInfo;
@@ -25,6 +37,8 @@ export interface ServerContext {
containers: ContainerApi;
items: ItemApi;
blocks: BlockApi;
// every fluid, named and colored by its rules, see docs/modding/resources/fluids.md
fluids: FluidApi;
recipes: RecipeApi;
tags: TagApi;
loot: LootApi;
+42
View File
@@ -16,6 +16,35 @@ export interface ItemStack {
data?: unknown;
}
// a fluid with its data, like the juice in a bottle, see docs/modding/resources/fluids.md. it has no amount: whatever
// holds fluid keeps that beside it, like a tank's { fluid, amount }
export interface FluidVariant {
readonly id: Id;
// any json value, which tells fluids of one id apart, like which fruit a juice is from
readonly data?: unknown;
}
// ctx.fluids in server and client scripts: every fluid any mod defines, the same on the server and every client
export interface FluidApi {
exists(id: Id): boolean;
// what players see, from the fluid's rules for its data, or else its json
name(fluid: FluidVariant): string;
// a color like "#c42d39", from the fluid's rules for its data, or else its json
color(fluid: FluidVariant): string;
// whether they're one fluid: the same id, and the same data whatever order its keys are in. no data is the same as
// {}, so a fluid whose data was emptied is the plain fluid again
same(a: FluidVariant, b: FluidVariant): boolean;
}
// what a fluid's data makes it, registered by a shared script, like neoforge's FluidType.getDescription and
// FluidTintSource.colorAsStack without the amount. data is the variant's, undefined for none, and isn't to be changed.
// returning undefined keeps what the fluid's json says
export interface FluidRules {
name?(data: unknown): string | undefined;
// a color like "#c42d39"
color?(data: unknown): string | undefined;
}
// the registries tags are for, see docs/modding/resources/tags.md
export type TagRegistry = "blocks" | "items" | "entities" | "biomes";
@@ -75,6 +104,19 @@ export interface SharedContext {
// whether it's solid like stone, like the server's ctx.blocks.solid
solid(id: Id): boolean;
};
// every item any mod registered, like the client's ctx.items, for rules that read one, like a juice named after the
// fruit it's from
readonly items: {
exists(id: Id): boolean;
// what players see, like "Cooked Rice"
name(id: Id): string;
// the components its json lists and their params, a copy
components(id: Id): Record<Id, unknown>;
};
readonly fluids: {
// the rules of one of the mod's fluids, which the server and every client name and color it by
register(id: Id, rules: FluidRules): void;
};
readonly components: {
// the rules of a block component, the same id the server script registers the rest of it under
// deno-lint-ignore no-explicit-any
+48 -4
View File
@@ -6,8 +6,10 @@ import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from ".
import { block_item_texture, cube_face_texture } from "./block_models.ts";
import { FULL_SHAPE, type Shape } from "./block_shapes.ts";
import type { ItemStack } from "./inventory.ts";
import type { SharedContext } from "./mod_api/shared.ts";
import type { FluidInCell } from "./physics.ts";
import { list_matches } from "./tags.ts";
import { is_object } from "./schema.ts";
export function point_inside_rec(
point_x: number,
@@ -80,14 +82,56 @@ export function item_texture(
return typeof info.texture_id === "function" ? info.texture_id(item) : info.texture_id;
}
// what players see an item called: its name, or one made from its id, so bworld:iron_ingot is "Iron Ingot"
// what players see an item called: its name, or one made from its id
export function display_name(item_id: string): string {
const name = EverythingRegistry.get<ItemRegistry>("items", item_id)?.name;
if (name) return name;
const words = (item_id.split(":")[1] ?? item_id).split("_").filter((word) => word.length > 0);
return EverythingRegistry.get<ItemRegistry>("items", item_id)?.name || name_from_id(item_id);
}
// ctx.items for shared scripts, and the part of the client's that's the same
export function shared_items_api(mod: string): SharedContext["items"] {
const known = (id: string) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`[${mod}] unknown item ${id}`);
return item;
};
return {
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
name: (id) => (known(id), display_name(id)),
components: (id) => structuredClone(known(id).components ?? {}),
};
}
// a name for what didn't give one, so bworld:iron_ingot is "Iron Ingot"
export function name_from_id(id: string): string {
const words = (id.split(":")[1] ?? id).split("_").filter((word) => word.length > 0);
return words.map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
}
// whether two stacks' or fluids' data are the same, whatever order their keys are in. no data is the same as {}, and a
// key set to undefined as one that isn't there, the way they'd be once saved or sent
export function same_data(a: unknown, b: unknown): boolean {
return (is_empty_data(a) && is_empty_data(b)) || same_json(a, b);
}
function is_empty_data(value: unknown) {
return value === undefined || (is_object(value) && defined_keys(value).length === 0);
}
function same_json(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (Array.isArray(a) || Array.isArray(b)) {
return Array.isArray(a) && Array.isArray(b) && a.length === b.length &&
a.every((value, i) => same_json(value, b[i]));
}
if (!is_object(a) || !is_object(b)) return false;
const keys = defined_keys(a);
return keys.length === defined_keys(b).length && keys.every((key) => same_json(a[key], b[key]));
}
function defined_keys(value: Record<string, unknown>) {
return Object.keys(value).filter((key) => value[key] !== undefined);
}
export function get_state_value(value: number, block_info: BlockRegistry, name: string) {
if (!block_info.states) {
return undefined;