sharing contexts

This commit is contained in:
2026-11-14 13:49:21 -03:00
parent 313098892e
commit a69db0cd2a
13 changed files with 192 additions and 208 deletions
+27 -78
View File
@@ -14,14 +14,15 @@ import {
shared_items_api,
} from "@/common/utils.ts";
import { LootTables } from "./loot.ts";
import { collision_shape_at, covers_face, is_solid, raycast_blocks, type Shape } from "@/common/block_shapes.ts";
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "@/common/block_shapes.ts";
import { AIR, CHUNK_SIZE, faces, ID_MASK } from "@/common/constants.ts";
import { physics } from "@/common/physics.ts";
import { clock } from "@/common/time.ts";
import { BlockRules } from "@/common/block_rules.ts";
import { fluid_exists, FluidRules, same_fluid } from "@/common/fluids.ts";
import { FluidRules } from "@/common/fluids.ts";
import { interact, item_handler, tank_handler, transfer, use_container } from "./fluid_handlers.ts";
import type { SharedContext } from "@/common/mod_api/shared.ts";
import { blocks_api, shared_context } from "@/common/shared_context.ts";
import { tag_api } from "@/common/tags.ts";
import type {
BlockComponent,
@@ -56,8 +57,8 @@ import {
ModLoadError,
type Phase,
recipe_lookup,
registrar,
run_phase,
shared_recipes_api,
} from "@/common/mod_loader.ts";
import {
argument,
@@ -307,24 +308,9 @@ export class ModRuntime {
}
#shared_context({ mod, version, config }: ScriptSource): SharedContext {
const { world, blocks } = this.#context(mod, version, config);
return {
mod: { id: mod, version },
const { world } = this.#context(mod, version, config);
return shared_context({ id: mod, version }, {
side: "server",
tags: tag_api(),
blocks,
items: shared_items_api(mod, this.fluid_rules),
fluids: {
exists: fluid_exists,
same: same_fluid,
register: (id, rules) => {
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
this.fluid_rules.register(mod, id, rules);
},
},
recipes: shared_recipes_api(mod, () => this.game.recipes, () => this.#shared_setup, (message) => {
throw new ModLoadError(mod, message);
}),
world: {
get_block: world.get_block,
get_state: world.get_state,
@@ -333,16 +319,11 @@ export class ModRuntime {
biome_at: world.biome_at,
season_at: world.season_at,
},
components: {
register_block: (id, rules) => {
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
if (!id.startsWith(`${mod}:`)) {
throw new ModLoadError(mod, `component ${id} must be in the namespace "${mod}"`);
}
this.rules.register(mod, id, rules);
},
},
};
block_rules: this.rules,
fluid_rules: this.fluid_rules,
recipes: () => this.game.recipes,
running: () => this.#shared_setup,
});
}
// every effect mods define, from their json. they're made before scripts run, since the world loads first and
@@ -694,16 +675,7 @@ export class ModRuntime {
}
#context(mod: string, version: string, config: Record<string, unknown>): ServerContext {
const setup_only = (what: string) => {
if (!this.#setting_up) {
throw new Error(`[${mod}] ${what} can only be registered during setup`);
}
};
const own = (id: string, what: string) => {
if (!id.startsWith(`${mod}:`)) {
throw new ModLoadError(mod, `${what} ${id} must be in the namespace "${mod}"`);
}
};
const register = registrar(mod, "server", () => this.#setting_up);
const game = () => this.game;
const ctx: ServerContext = {
@@ -711,36 +683,24 @@ export class ModRuntime {
config,
components: {
register_block: (id, component) => {
setup_only("components");
own(id, "component");
if (this.block_components.has(id)) {
throw new ModLoadError(mod, `component ${id} is registered twice`);
}
register.new_id("component", id, this.block_components.has(id));
this.block_components.set(id, { mod, component: component as BlockComponent });
this.#components_of.clear();
this.#ticking_components_of.clear();
this.#components_with.clear();
},
register_item: (id, component) => {
setup_only("components");
own(id, "component");
if (this.item_components.has(id)) {
throw new ModLoadError(mod, `component ${id} is registered twice`);
}
register.new_id("component", id, this.item_components.has(id));
this.item_components.set(id, { mod, component: component as ItemComponent });
},
register_effect: (id, component) => {
setup_only("components");
own(id, "component");
if (this.effect_components.has(id)) {
throw new ModLoadError(mod, `component ${id} is registered twice`);
}
register.new_id("component", id, this.effect_components.has(id));
this.effect_components.set(id, { mod, component: component as EffectComponent });
},
},
commands: {
register: (name_or_node: string | CommandNode<CommandSource>, command?: Command) => {
setup_only("commands");
register.running("command");
const node = typeof name_or_node === "string"
? simple_command(name_or_node, command!)
: name_or_node as unknown as CommandNode<ServerCommandSource>;
@@ -1011,23 +971,12 @@ export class ModRuntime {
},
},
blocks: {
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,
...blocks_api(),
toughness: (id) => {
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`unknown block ${id}`);
return block.toughness ?? Infinity;
},
components: (id) => {
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`unknown block ${id}`);
return structuredClone(block.components ?? {});
},
solid: (id) => {
if (id === AIR_ID) return false;
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`unknown block ${id}`);
return is_solid(block);
},
},
fluids: {
...this.fluid_rules.api(),
@@ -1062,19 +1011,19 @@ export class ModRuntime {
return left;
},
register_condition: (id, condition) => {
setup_only("loot conditions");
register.running("loot condition");
this.loot.register_condition(mod, id, condition);
},
register_function: (id, fn) => {
setup_only("loot functions");
register.running("loot function");
this.loot.register_function(mod, id, fn);
},
modify: (table, fn) => {
setup_only("loot modifiers");
register.running("loot modifier");
this.loot.modify(mod, table, fn);
},
register_table: (id, table) => {
setup_only("loot tables");
register.running("loot table");
this.loot.register_table(mod, id, table);
},
},
@@ -1094,12 +1043,12 @@ export class ModRuntime {
},
menus: {
extend_player_menu: (build) => {
setup_only("extend_player_menu");
register.running("player menu slot");
this.#player_menu_builders.push({ mod, build });
},
register: <Args>(id: string, build: (menu: MenuBuilder, args: Args, player: Player) => void) => {
own(id, "menu type");
if (this.#menu_types.has(id)) throw new Error(`[${mod}] menu type ${id} is already registered`);
register.own("menu type", id);
if (this.#menu_types.has(id)) register.fail(`menu type ${id} is registered twice`);
this.#menu_types.add(id);
const type: MenuType<Args> = {
id,
@@ -1115,13 +1064,13 @@ export class ModRuntime {
},
net: {
on: (channel, handler) => {
own(channel, "channel");
register.own("channel", channel);
const handlers = this.#net_handlers.get(channel) ?? [];
handlers.push({ mod, handler: handler as (player: Player, data: unknown) => void });
this.#net_handlers.set(channel, handlers);
},
send: (player, channel, data) => {
own(channel, "channel");
register.own("channel", channel);
const server_player = this.#server_players.get(player);
if (!server_player || !game().players().includes(server_player)) {
throw new Error(`${player.name} isn't online`);
@@ -1129,7 +1078,7 @@ export class ModRuntime {
game().send_mod_message(server_player, channel, data);
},
broadcast: (channel, data) => {
own(channel, "channel");
register.own("channel", channel);
for (const player of game().players()) game().send_mod_message(player, channel, data);
},
},