sharing contexts
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what mods' shared phases get, built the same way on the server and every client so they register the same rules and
|
||||
// recipes. each side gives it its own world, rules and recipe book
|
||||
import type { ModInfo, SharedContext, SharedWorld } from "./mod_api/shared.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
|
||||
import { AIR_ID } from "./protocol.ts";
|
||||
import { is_solid } from "./block_shapes.ts";
|
||||
import type { BlockRules } from "./block_rules.ts";
|
||||
import { fluid_exists, type FluidRules, same_fluid } from "./fluids.ts";
|
||||
import { type RecipeBook, registrar, shared_recipes_api } from "./mod_loader.ts";
|
||||
import { tag_api } from "./tags.ts";
|
||||
import { shared_items_api } from "./utils.ts";
|
||||
|
||||
export interface SharedSide {
|
||||
side: "server" | "client";
|
||||
// what the rules read and change: the real world on the server, what a client shows on a client
|
||||
world: SharedWorld;
|
||||
block_rules: BlockRules;
|
||||
fluid_rules: FluidRules;
|
||||
recipes: () => RecipeBook;
|
||||
// whether the shared phases are running, the only time they can register
|
||||
running: () => boolean;
|
||||
}
|
||||
|
||||
export function shared_context(mod: ModInfo, side: SharedSide): SharedContext {
|
||||
const register = registrar(mod.id, "shared", side.running);
|
||||
return {
|
||||
mod,
|
||||
side: side.side,
|
||||
tags: tag_api(),
|
||||
blocks: blocks_api(),
|
||||
items: shared_items_api(mod.id, side.fluid_rules),
|
||||
fluids: {
|
||||
exists: fluid_exists,
|
||||
same: same_fluid,
|
||||
register(id, rules) {
|
||||
register.new_id("fluid rules", id, side.fluid_rules.has(id));
|
||||
if (!fluid_exists(id)) register.fail(`unknown fluid ${id}`);
|
||||
side.fluid_rules.register(mod.id, id, rules);
|
||||
},
|
||||
},
|
||||
recipes: shared_recipes_api(mod.id, side.recipes, register),
|
||||
world: side.world,
|
||||
components: {
|
||||
register_block(id, rules) {
|
||||
register.new_id("component", id, side.block_rules.has(id));
|
||||
side.block_rules.register(mod.id, id, rules);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ctx.blocks, the part every side has
|
||||
export function blocks_api(): SharedContext["blocks"] {
|
||||
const known = (id: string) => {
|
||||
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (!block) throw new Error(`unknown block ${id}`);
|
||||
return block;
|
||||
};
|
||||
return {
|
||||
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,
|
||||
components: (id) => structuredClone(known(id).components ?? {}),
|
||||
solid: (id) => id !== AIR_ID && is_solid(known(id)),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user