Recipe viewer
This commit is contained in:
+45
-4
@@ -1,11 +1,12 @@
|
||||
// loads the mods the server lists: downloads their .bmod files, registers their data, then runs their client scripts.
|
||||
// see "Delivery to clients" in MODS.md
|
||||
import { AIR } from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientContext } from "$/common/mod_api/client.ts";
|
||||
import { ModListing, ModLoadError, register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientContext, InventoryOverlay } from "$/common/mod_api/client.ts";
|
||||
import { ModListing, ModLoadError, RecipeBook, register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { type Bmod, read_bmod } from "$/common/bmod.ts";
|
||||
import type { OreJson } from "$/common/mod_data.ts";
|
||||
import { display_name } from "$/common/utils.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { Client } from "./client.ts";
|
||||
import { code_url, fetch_verified } from "./handshake.ts";
|
||||
@@ -19,6 +20,13 @@ export const worldgen_mods: { scripts: { mod: string; url: string }[]; ores: Ore
|
||||
// set once the game is running, mods can't look at it during setup
|
||||
let client: Client | undefined;
|
||||
|
||||
// what mods draw on inventory screens, see GuiInventoryScreen
|
||||
export const inventory_overlays: { mod: string; overlay: InventoryOverlay }[] = [];
|
||||
|
||||
// the loaded mods' recipes, and their names by id
|
||||
let recipe_book = new RecipeBook();
|
||||
const mod_names = new Map<string, string>();
|
||||
|
||||
export function set_mods_client(game_client: Client) {
|
||||
client = game_client;
|
||||
}
|
||||
@@ -47,6 +55,10 @@ 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;
|
||||
mod_names.clear();
|
||||
for (const listing of listings) mod_names.set(listing.id, listing.name);
|
||||
|
||||
worldgen_mods.ores = recipes.ores;
|
||||
worldgen_mods.scripts = bmods.flatMap((bmod) =>
|
||||
@@ -80,9 +92,24 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return client;
|
||||
};
|
||||
|
||||
const not_yet_fn = (name: string) => () => {
|
||||
throw new Error(`[${mod}] ctx.ui.${name} isn't implemented yet (Custom screens in MODS.md)`);
|
||||
};
|
||||
const known_item = (id: string) => {
|
||||
const item = EverythingRegistry.get<ItemRegistry>("items", id);
|
||||
if (!item) throw new Error(`[${mod}] unknown item ${id}`);
|
||||
return item;
|
||||
};
|
||||
|
||||
return {
|
||||
mod: { id: mod, version: listing.version },
|
||||
ui: not_yet("ui", "step 7") as ClientContext["ui"],
|
||||
ui: {
|
||||
register_screen: not_yet_fn("register_screen"),
|
||||
open: not_yet_fn("open"),
|
||||
add_inventory_overlay(overlay) {
|
||||
inventory_overlays.push({ mod, overlay });
|
||||
},
|
||||
},
|
||||
hud: not_yet("hud", "step 7") as ClientContext["hud"],
|
||||
input: not_yet("input", "step 8") as ClientContext["input"],
|
||||
net: not_yet("net", "step 8") as ClientContext["net"],
|
||||
@@ -105,6 +132,20 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return need_client().level.time;
|
||||
},
|
||||
},
|
||||
items: {
|
||||
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
|
||||
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
|
||||
name: (id) => (known_item(id), display_name(id)),
|
||||
mod(id) {
|
||||
known_item(id);
|
||||
const namespace = id.split(":")[0];
|
||||
return mod_names.get(namespace) ?? namespace;
|
||||
},
|
||||
},
|
||||
recipes: {
|
||||
list: (type) =>
|
||||
structuredClone(type ? recipe_book.all.filter((recipe) => recipe.type === type) : recipe_book.all),
|
||||
},
|
||||
log: (...args) => console.log(`[${mod}]`, ...args),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user