diff --git a/MODS.md b/MODS.md index 76d5afc..d73c103 100644 --- a/MODS.md +++ b/MODS.md @@ -65,6 +65,7 @@ deno task build && deno task server # packs the mods and starts a se - [Recipes](docs/modding/resources/recipes.md): crafting, smelting, smithing, stonecutting and your own recipe types - [Tags](docs/modding/resources/tags.md): lists of blocks, items or entities that every mod can add to - [Loot Tables](docs/modding/resources/loot_tables.md): what blocks, mobs, fishing and chests give +- [Fluids](docs/modding/resources/fluids.md): fluids that can be held, apart from blocks that flow **Everything else** diff --git a/common/bmod.ts b/common/bmod.ts index 109cc3d..0922e69 100644 --- a/common/bmod.ts +++ b/common/bmod.ts @@ -11,6 +11,7 @@ import { type EffectJson, type EntityJson, type EntityModelJson, + type FluidJson, type ItemJson, type ManifestJson, type OreJson, @@ -21,6 +22,7 @@ import { validate_effect, validate_entity, validate_entity_model, + validate_fluid, validate_item, validate_manifest, validate_model, @@ -79,8 +81,8 @@ export class BmodError extends Error { // the zip's layout: // manifest.json the mod's manifest, with "scripts" and "credits" pointing into the zip -// data.json every block, model, item, recipe, ore, entity, entity model, effect, tag, loot table and -// biome, and the mod's config +// data.json every block, model, item, recipe, ore, entity, entity model, effect, fluid, tag, loot +// table and biome, and the mod's config // scripts/.js // textures/.png the texture : // textures/.json its animation, if it has one @@ -177,6 +179,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod { problems, ) as EntityModelJson[], effects: checked(raw_data.effects, "effects", validate_effect, problems) as EffectJson[], + fluids: checked(raw_data.fluids, "fluids", validate_fluid, problems) as FluidJson[], tags: checked(raw_data.tags, "tags", validate_tag, problems) as TagJson[], loot_tables: checked(raw_data.loot_tables, "loot_tables", validate_loot_table, problems) as LootTableJson[], biomes: checked(raw_data.biomes, "biomes", validate_biome, problems) as BiomeJson[], @@ -206,7 +209,15 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod { problems.push("data.json: config must be an object"); } // a mod only registers ids in its own namespace - const registered = [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models]; + const registered = [ + ...data.blocks, + ...data.models, + ...data.items, + ...data.entities, + ...data.entity_models, + ...data.effects, + ...data.fluids, + ]; const worldgen = [ ...data.noises!, ...data.density_functions!, @@ -214,7 +225,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod { ...data.biome_sources!, ...data.features!, ]; - for (const { id } of [...registered, ...data.effects, ...data.loot_tables!, ...data.biomes!, ...worldgen]) { + for (const { id } of [...registered, ...data.loot_tables!, ...data.biomes!, ...worldgen]) { if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`); } diff --git a/common/mod_check.ts b/common/mod_check.ts index c689555..48cd9ac 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -9,6 +9,7 @@ import { type EffectJson, type EntityJson, type EntityModelJson, + type FluidJson, type ItemJson, type OreJson, type RecipeJson, @@ -58,6 +59,7 @@ export interface LoadedMod { entities: { file: string; json: EntityJson }[]; entity_models: { file: string; json: EntityModelJson }[]; effects: { file: string; json: EffectJson }[]; + fluids: { file: string; json: FluidJson }[]; tags: { file: string; json: TagJson }[]; loot_tables: { file: string; json: LootTableJson }[]; biomes: { file: string; json: BiomeJson }[]; @@ -88,6 +90,7 @@ export function mod_data(mod: LoadedMod): ModData { entities: mod.entities.map((e) => e.json), entity_models: mod.entity_models.map((m) => m.json), effects: mod.effects.map((e) => e.json), + fluids: mod.fluids.map((f) => f.json), tags: mod.tags.map((t) => t.json), loot_tables: mod.loot_tables.map((t) => t.json), biomes: mod.biomes.map((b) => b.json), @@ -159,8 +162,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable(); const entity_model_jsons = new Map(); const effect_owners = new Map(); + const fluid_owners = new Map(); const tag_owners = new Map>(); - const fluids = new Set(); + // blocks that flow, which waterloggable blocks can hold + const fluid_blocks = new Set(); const loot_tables = new Set(); const biome_owners = new Map(); const add = (map: Map, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]); @@ -181,7 +186,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable 1) { + errors.push(`${file}: fluid ${json.id} is also defined by ${fluid_owners.get(json.id)!.join(", ")}`); + } + } for (const { file, json } of mod.spawns) { check_namespace(file, json.entity); const entity = entity_jsons.get(json.entity); diff --git a/common/mod_data.ts b/common/mod_data.ts index 9991b3f..fbcb65b 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -25,6 +25,7 @@ import { expand_items, list_matches } from "./tags.ts"; import { GAME_VERSION } from "./constants.ts"; import { by_kind, + color, find_problems, id, id_or_tag, @@ -456,6 +457,18 @@ const effect_schema = z.object({ export type EffectJson = z.infer; +// a kind of fluid, like neoforge's FluidType: what it is apart from any block, container or amount. a block flows in +// the world with its own "fluid" field, which is another thing +const fluid_schema = z.object({ + id, + // what players see, from the id when not set like an item's + name: text.optional(), + // for whatever shows it, like a container's tinted overlay + color, +}); + +export type FluidJson = z.infer; + // a box from origin to origin + size, textured like minecraft's box uv: see the renderer's FACES const entity_cube = z.object({ origin: vec3, @@ -869,6 +882,10 @@ export function validate_effect(json: unknown): Problems { return find_problems(effect_schema, json); } +export function validate_fluid(json: unknown): Problems { + return find_problems(fluid_schema, json); +} + export function validate_entity_model(json: unknown): Problems { return find_problems(entity_model_schema, json); } diff --git a/common/mod_loader.ts b/common/mod_loader.ts index 5fd9c52..756538d 100644 --- a/common/mod_loader.ts +++ b/common/mod_loader.ts @@ -11,6 +11,7 @@ import { EffectJson, EntityJson, EntityModelJson, + FluidJson, grid_recipe_from_json, GridRecipe, is_mod_recipe, @@ -45,6 +46,8 @@ export interface ModData { entity_models: EntityModelJson[]; // status effects, see EffectJson effects: EffectJson[]; + // kinds of fluid, see FluidJson + fluids: FluidJson[]; // tag files, see common/tags.ts tags: TagJson[]; // see common/loot.ts. missing from mods packed before there were loot tables @@ -103,8 +106,9 @@ export function is_registered(registry: TagRegistry, id: string): boolean { return (registry === "blocks" && id === "bworld:air") || EverythingRegistry.get(registry, id) !== undefined; } -// registers every mod's blocks, items, entities, effects, tags and recipes, in the order given (dependencies first). tags load -// once every mod's content has, since a tag can hold anything, and recipes after them, since they can use tags +// registers every mod's blocks, items, entities, effects, fluids, tags and recipes, in the order given (dependencies +// first). tags load once every mod's content has, since a tag can hold anything, and recipes after them, since they can +// use tags export function register_mod_data(mods: { id: string; data: ModData }[]): RecipeBook { const recipes = new RecipeBook(); @@ -147,6 +151,9 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe for (const json of data.effects) { EverythingRegistry.register("effects", json.id, json); } + for (const json of data.fluids) { + EverythingRegistry.register("fluids", json.id, json); + } for (const json of data.loot_tables ?? []) { EverythingRegistry.register("loot_tables", json.id, json); } diff --git a/common/schema.ts b/common/schema.ts index 2d0da3a..bdf5712 100644 --- a/common/schema.ts +++ b/common/schema.ts @@ -21,6 +21,7 @@ export const loot_table_id = z.string('must be a loot table id, like "my_mod:blo // text players see export const text = z.string().min(1); export const vec3 = z.tuple([z.number(), z.number(), z.number()], "must be [x, y, z]"); +export const color = z.string('must be a color like "#79c05a"').regex(/^#[0-9a-fA-F]{6}$/); // any json object, for what's only stored, like components export const json_object = z.record(z.string(), z.unknown()); diff --git a/common/worldgen/biomes.ts b/common/worldgen/biomes.ts index dd552a4..443892f 100644 --- a/common/worldgen/biomes.ts +++ b/common/worldgen/biomes.ts @@ -5,7 +5,7 @@ // gets is the generator's biome rules (worldgen/biome_source), these say what a biome looks like once it's picked import * as z from "zod"; import { EverythingRegistry } from "../everything_registry.ts"; -import { find_problems, id, type Problems } from "../schema.ts"; +import { color, find_problems, id, type Problems } from "../schema.ts"; import { farming_day } from "../time.ts"; export const SEASONS = ["spring", "summer", "autumn", "winter"] as const; @@ -27,8 +27,6 @@ const surface_schema = z.object({ export type SurfaceJson = z.infer; -const color = z.string('must be a color like "#79c05a"').regex(/^#[0-9a-fA-F]{6}$/); - const biome_schema = z.object({ id, // a season it's always in, like winter for snowy biomes. when not set it follows the calendar, see calendar_season diff --git a/common/worldgen_bundle.ts b/common/worldgen_bundle.ts index 7f216a8..943fa08 100644 --- a/common/worldgen_bundle.ts +++ b/common/worldgen_bundle.ts @@ -93,6 +93,7 @@ export async function load_bundle(bundle: WorldgenBundle): Promise entities: [], entity_models: [], effects: [], + fluids: [], loot_tables: [], }, }))); diff --git a/docs/modding/concepts/registries.md b/docs/modding/concepts/registries.md index e252de0..b2b8f9c 100644 --- a/docs/modding/concepts/registries.md +++ b/docs/modding/concepts/registries.md @@ -1,7 +1,7 @@ # Registries -Every block, item, block model, entity, entity model, status effect and texture has an id, and is registered under it -once, while mods load. After that the game refers to things only by their id. +Every block, item, block model, entity, entity model, status effect, fluid and texture has an id, and is registered +under it once, while mods load. After that the game refers to things only by their id. ## Identifiers @@ -32,9 +32,9 @@ The server and every client load mods the same way, in the same order: 1. **Order.** Dependencies come before the mods that need them, and otherwise mods load alphabetically. Mods that depend on each other in a circle are an error. The server sends its order to clients, and they follow it. -2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models, entities and effects. - Then every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which can use - tags. +2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models, entities, effects and + fluids. Then every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which + can use tags. 3. **Scripts.** Server scripts' `setup` runs on the server, and client scripts' on each client. Worldgen scripts' `setup` runs on the server before it makes chunks. diff --git a/docs/modding/gettingstarted/modfiles.md b/docs/modding/gettingstarted/modfiles.md index 4b24b3b..6ba04a8 100644 --- a/docs/modding/gettingstarted/modfiles.md +++ b/docs/modding/gettingstarted/modfiles.md @@ -18,6 +18,7 @@ mods/ models/*.geo.json # entity models entities/*.json effects/*.json # status effects, see entities/effects.md + fluids/*.json # fluids that can be held, see resources/fluids.md recipes/*.json tags///*.json # tags, see resources/tags.md loot_tables/**/*.json # loot tables, see resources/loot_tables.md diff --git a/docs/modding/resources/fluids.md b/docs/modding/resources/fluids.md new file mode 100644 index 0000000..50f2091 --- /dev/null +++ b/docs/modding/resources/fluids.md @@ -0,0 +1,28 @@ +# Fluids + +Fluids are what bottles and tanks hold, like juice or oil. They work like NeoForge's `FluidType`s: a fluid is defined +once, apart from any block, container or amount. + +This isn't the same as a [block with `fluid`](../blocks/index.md#fluids), like water, which flows in the world. A fluid +here has no block, so it can't be placed. + +## Defining a Fluid + +`fluids/maple_syrup.json`: + +```json +{ + "format_version": 1, + "fluid": { + "id": "copper_tools:maple_syrup", + "name": "Maple Syrup", + "color": "#a0522d" + } +} +``` + +| Field | Required | What it does | +| ------- | -------- | ------------------------------------------------------------------------------------------- | +| `id` | yes | The fluid's id, in the mod's [namespace](../concepts/registries.md#identifiers). | +| `name` | no | What players see. `copper_tools:maple_syrup` is "Maple Syrup" when not set, like an item's. | +| `color` | yes | Its color, like `"#a0522d"`, for whatever shows it. | diff --git a/server/load_bmods.ts b/server/load_bmods.ts index 7e2dd0a..69f0fd1 100644 --- a/server/load_bmods.ts +++ b/server/load_bmods.ts @@ -179,6 +179,7 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod { entities: entries(bmod.data.entities), entity_models: entries(bmod.data.entity_models), effects: entries(bmod.data.effects), + fluids: entries(bmod.data.fluids), tags: entries(bmod.data.tags), loot_tables: entries(bmod.data.loot_tables ?? []), biomes: entries(bmod.data.biomes ?? []), diff --git a/tests/bmod_test.ts b/tests/bmod_test.ts index cc22b03..93500cb 100644 --- a/tests/bmod_test.ts +++ b/tests/bmod_test.ts @@ -429,6 +429,7 @@ Deno.test("a mod with only data packs and reads", () => { entities: [], entity_models: [], effects: [], + fluids: [], tags: [], }, scripts: {}, diff --git a/tests/fluid_registry_test.ts b/tests/fluid_registry_test.ts new file mode 100644 index 0000000..6cccb74 --- /dev/null +++ b/tests/fluid_registry_test.ts @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +import { assert, assertEquals } from "@std/assert"; +import { EverythingRegistry } from "$/common/everything_registry.ts"; +import type { FluidJson } from "$/common/mod_data.ts"; +import { read_bmod } from "$/common/bmod.ts"; +import { register_mod_data } from "$/common/mod_loader.ts"; +import { load_and_check } from "$/tools/check_mods.ts"; +import { pack_mod } from "$/tools/pack_mod.ts"; + +// a mod with only a manifest and the given fluids/ files, by file name +function fluid_mod(fluids: Record) { + const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" }); + Deno.mkdirSync(`${dir}/testfluids/fluids`, { recursive: true }); + Deno.writeTextFileSync( + `${dir}/testfluids/manifest.json`, + JSON.stringify({ format_version: 1, id: "testfluids", name: "Test Fluids", version: "1.0.0" }), + ); + for (const [file, fluid] of Object.entries(fluids)) { + Deno.writeTextFileSync(`${dir}/testfluids/fluids/${file}`, JSON.stringify({ format_version: 1, fluid })); + } + return dir; +} + +Deno.test("a mod's fluids/ file registers a fluid, packed into its .bmod", async () => { + const syrup = { id: "testfluids:syrup", name: "Maple Syrup", color: "#a0522d" }; + const dir = fluid_mod({ "syrup.json": syrup }); + const mods = load_and_check(dir); + assertEquals(mods[0].report.errors, []); + const bmod = read_bmod(await pack_mod(mods[0]), "testfluids.bmod"); + EverythingRegistry.clear(); + register_mod_data([{ id: "testfluids", data: bmod.data }]); + assertEquals(EverythingRegistry.get("fluids", "testfluids:syrup"), syrup); + Deno.removeSync(dir, { recursive: true }); +}); + +Deno.test("a fluid with a bad color fails loading", () => { + const dir = fluid_mod({ "syrup.json": { id: "testfluids:syrup", color: "brown" } }); + const errors = load_and_check(dir)[0].report.errors; + assert(errors.some((e) => e.startsWith("fluids/syrup.json:") && e.includes("color")), errors.join("\n")); + Deno.removeSync(dir, { recursive: true }); +}); diff --git a/tests/mod_config_test.ts b/tests/mod_config_test.ts index f46ff22..acff6ef 100644 --- a/tests/mod_config_test.ts +++ b/tests/mod_config_test.ts @@ -83,6 +83,7 @@ Deno.test("a server changes a mod's settings with config/.json, and play entities: [], entity_models: [], effects: [], + fluids: [], tags: [], config: { speed: 16, ore: { size: 4, depth: 30 } }, }; diff --git a/tests/tags_test.ts b/tests/tags_test.ts index 203de0f..d370e42 100644 --- a/tests/tags_test.ts +++ b/tests/tags_test.ts @@ -215,6 +215,7 @@ Deno.test("tags survive packing into a .bmod", () => { entities: [], entity_models: [], effects: [], + fluids: [], tags: [tag], }; const manifest: ManifestJson = { format_version: 1, id: "x", name: "X", version: "1.0.0" }; diff --git a/tools/check_mods.ts b/tools/check_mods.ts index 0a58d15..fc58b28 100644 --- a/tools/check_mods.ts +++ b/tools/check_mods.ts @@ -14,6 +14,7 @@ import { validate_block, validate_effect, validate_entity, + validate_fluid, validate_item, validate_manifest, validate_model, @@ -94,6 +95,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod { entities: [], entity_models: [], effects: [], + fluids: [], tags: [], loot_tables: [], biomes: [], @@ -175,6 +177,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod { load_data("recipes", "recipe", validate_recipe, mod.recipes); load_data("entities", "entity", validate_entity, mod.entities); load_data("effects", "effect", validate_effect, mod.effects); + load_data("fluids", "fluid", validate_fluid, mod.fluids); load_data("worldgen/biomes", "biome", validate_biome, mod.biomes); load_data("worldgen/noise", "noise", validate_noise, mod.noises); load_data("worldgen/density_function", "density_function", validate_density_function, mod.density_functions); diff --git a/tools/worldgen_preview.ts b/tools/worldgen_preview.ts index 4cbc50b..35a8be3 100644 --- a/tools/worldgen_preview.ts +++ b/tools/worldgen_preview.ts @@ -33,6 +33,7 @@ register_mod_data([{ entities: jsons(bworld.entities), entity_models: jsons(bworld.entity_models), effects: jsons(bworld.effects), + fluids: jsons(bworld.fluids), tags: jsons(bworld.tags), loot_tables: jsons(bworld.loot_tables), biomes: jsons(bworld.biomes),