Curios and artifacts !

This commit is contained in:
2026-09-30 15:22:32 -03:00
parent 342b45d593
commit 3af5f6e54b
45 changed files with 729 additions and 43 deletions
+32 -1
View File
@@ -20,6 +20,20 @@ let client: Client | undefined;
// what mods draw on inventory screens, see GuiInventoryScreen
export const inventory_overlays: { mod: string; overlay: InventoryOverlay }[] = [];
// mod channels, see ctx.net: who listens on each channel
const net_handlers = new Map<string, { mod: string; handler: (data: unknown) => void }[]>();
// the server sent a mod message
export function receive_mod_message(channel: string, data: unknown) {
for (const { mod, handler } of net_handlers.get(channel) ?? []) {
try {
handler(data);
} catch (e) {
console.error(`[${mod}] a ${channel} handler threw:`, e);
}
}
}
// the loaded mods' recipes, and their names by id
let recipe_book = new RecipeBook();
const mod_names = new Map<string, string>();
@@ -54,6 +68,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
const recipes = register_mod_data(bmods.map((bmod) => ({ id: bmod.manifest.id, data: bmod.data })));
recipe_book = recipes;
inventory_overlays.length = 0;
net_handlers.clear();
mod_names.clear();
for (const listing of listings) mod_names.set(listing.id, listing.name);
@@ -89,6 +104,11 @@ function client_context(listing: ModListing): ClientContext {
const not_yet_fn = (name: string) => () => {
throw new Error(`[${mod}] ctx.ui.${name} isn't implemented yet (custom screens, see docs/modding_plan.md)`);
};
const own = (channel: string) => {
if (!channel.startsWith(`${mod}:`)) {
throw new Error(`[${mod}] channel ${channel} must be in the namespace "${mod}"`);
}
};
const known_item = (id: string) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`[${mod}] unknown item ${id}`);
@@ -106,7 +126,18 @@ function client_context(listing: ModListing): ClientContext {
},
hud: not_yet("hud", "HUD elements") as ClientContext["hud"],
input: not_yet("input", "keybinds") as ClientContext["input"],
net: not_yet("net", "mod channels") as ClientContext["net"],
net: {
on(channel, handler) {
own(channel);
const handlers = net_handlers.get(channel) ?? [];
handlers.push({ mod, handler: handler as (data: unknown) => void });
net_handlers.set(channel, handlers);
},
send(channel, data) {
own(channel);
need_client().connection.send({ type: "mod", channel, data });
},
},
player: {
get name() {
return need_client().connection.name;