Fluid registry
This commit is contained in:
@@ -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**
|
||||
|
||||
|
||||
+15
-4
@@ -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/<side>.js
|
||||
// textures/<name>.png the texture <mod id>:<name>
|
||||
// textures/<name>.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`);
|
||||
}
|
||||
|
||||
|
||||
+14
-3
@@ -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<st
|
||||
const entity_model_owners = new Map<string, string[]>();
|
||||
const entity_model_jsons = new Map<string, EntityModelJson>();
|
||||
const effect_owners = new Map<string, string[]>();
|
||||
const fluid_owners = new Map<string, string[]>();
|
||||
const tag_owners = new Map<TagRegistry, Set<string>>();
|
||||
const fluids = new Set<string>();
|
||||
// blocks that flow, which waterloggable blocks can hold
|
||||
const fluid_blocks = new Set<string>();
|
||||
const loot_tables = new Set<string>();
|
||||
const biome_owners = new Map<string, string[]>();
|
||||
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
|
||||
@@ -181,7 +186,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
for (const { json } of mod.blocks) {
|
||||
add(block_owners, json.id, mod.id);
|
||||
if (json.item !== false) add(item_owners, json.id, mod.id);
|
||||
if (json.fluid) fluids.add(json.id);
|
||||
if (json.fluid) fluid_blocks.add(json.id);
|
||||
}
|
||||
// one without a texture is the item of the mod's block, which is already counted
|
||||
for (const { json } of mod.items) if (json.texture !== undefined) add(item_owners, json.id, mod.id);
|
||||
@@ -198,6 +203,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
entity_model_jsons.set(json.id, json);
|
||||
}
|
||||
for (const { json } of mod.effects) add(effect_owners, json.id, mod.id);
|
||||
for (const { json } of mod.fluids) add(fluid_owners, json.id, mod.id);
|
||||
for (const { json } of mod.loot_tables ?? []) loot_tables.add(json.id);
|
||||
for (const { json } of mod.biomes ?? []) add(biome_owners, json.id, mod.id);
|
||||
for (const { json } of mod.noises ?? []) add(worldgen_owners.noise, json.id, mod.id);
|
||||
@@ -279,7 +285,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
if (json.loot_table) uses(file, json.loot_table, "loot table");
|
||||
if (json.waterloggable) {
|
||||
uses(file, json.waterloggable, "block");
|
||||
if (block_owners.has(json.waterloggable) && !fluids.has(json.waterloggable)) {
|
||||
if (block_owners.has(json.waterloggable) && !fluid_blocks.has(json.waterloggable)) {
|
||||
errors.push(`${file}: waterloggable ${json.waterloggable} isn't a fluid`);
|
||||
}
|
||||
}
|
||||
@@ -474,6 +480,11 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
}
|
||||
if (json.icon !== undefined) uses(file, json.icon, "texture");
|
||||
}
|
||||
for (const { file, json } of mod.fluids) {
|
||||
if ((fluid_owners.get(json.id)?.length ?? 0) > 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);
|
||||
|
||||
@@ -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<typeof effect_schema>;
|
||||
|
||||
// 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<typeof fluid_schema>;
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -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<EffectJson>("effects", json.id, json);
|
||||
}
|
||||
for (const json of data.fluids) {
|
||||
EverythingRegistry.register<FluidJson>("fluids", json.id, json);
|
||||
}
|
||||
for (const json of data.loot_tables ?? []) {
|
||||
EverythingRegistry.register<LootTableJson>("loot_tables", json.id, json);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -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<typeof surface_schema>;
|
||||
|
||||
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
|
||||
|
||||
@@ -93,6 +93,7 @@ export async function load_bundle(bundle: WorldgenBundle): Promise<LoadedBundle>
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
effects: [],
|
||||
fluids: [],
|
||||
loot_tables: [],
|
||||
},
|
||||
})));
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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/<registry>/<namespace>/*.json # tags, see resources/tags.md
|
||||
loot_tables/**/*.json # loot tables, see resources/loot_tables.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. |
|
||||
@@ -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 ?? []),
|
||||
|
||||
@@ -429,6 +429,7 @@ Deno.test("a mod with only data packs and reads", () => {
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
effects: [],
|
||||
fluids: [],
|
||||
tags: [],
|
||||
},
|
||||
scripts: {},
|
||||
|
||||
@@ -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<string, unknown>) {
|
||||
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<FluidJson>("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 });
|
||||
});
|
||||
@@ -83,6 +83,7 @@ Deno.test("a server changes a mod's settings with config/<mod id>.json, and play
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
effects: [],
|
||||
fluids: [],
|
||||
tags: [],
|
||||
config: { speed: 16, ore: { size: 4, depth: 30 } },
|
||||
};
|
||||
|
||||
@@ -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" };
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user