Block visuals

This commit is contained in:
2026-10-13 23:52:30 -03:00
parent 55ad08266e
commit 6ad476ac18
19 changed files with 854 additions and 14 deletions
+18
View File
@@ -20,6 +20,7 @@ import { GuiModScreen } from "./gui/gui_mod_screens.ts";
import { clear_hooks, expose_engine, loading_mod } from "$/common/hooks.ts";
import { common_engine } from "$/common/engine_modules.ts";
import { client_engine } from "./engine_modules.ts";
import { block_visuals } from "./rendering/visuals.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
import type { LootTableJson } from "$/common/loot.ts";
@@ -53,6 +54,9 @@ export function receive_mod_message(channel: string, data: unknown) {
export let block_rules = new BlockRules(run_mod);
// whether shared scripts are being set up, the only time they can register rules
let rules_setup = false;
// whether client scripts are being set up, the only time they can register visuals, since chunk workers are told
// which blocks have them when the level starts
let client_setup = false;
function run_mod<T>(mod: string, what: string, fn: () => T): T | undefined {
try {
@@ -147,6 +151,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
inventory_overlays.length = 0;
menu_screens.clear();
net_handlers.clear();
block_visuals.clear();
mod_names.clear();
for (const listing of listings) mod_names.set(listing.id, listing.name);
@@ -172,6 +177,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
}
rules_setup = false;
client_setup = true;
for (const [i, bmod] of bmods.entries()) {
const code = bmod.scripts.client;
if (!code) continue;
@@ -183,6 +189,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
await module.setup(client_context(listings[i]));
});
}
client_setup = false;
}
function script_url(code: string) {
@@ -332,6 +339,17 @@ function client_context(listing: ModListing): ClientContext {
structuredClone(type ? recipe_book.all.filter((recipe) => recipe.type === type) : recipe_book.all),
},
tags: tag_api(),
render: {
register_visual(block, create, options) {
if (!client_setup) throw new Error(`[${mod}] visuals can only be registered during setup`);
const nid = EverythingRegistry.get_id("blocks", block);
if (nid === undefined) throw new Error(`[${mod}] unknown block ${block}`);
if (typeof create !== "function") throw new Error(`[${mod}] a visual needs a function that makes it`);
const other = block_visuals.get(nid);
if (other) throw new Error(`[${mod}] ${block} already has a visual, from ${other.mod}`);
block_visuals.set(nid, { mod, create, replaces_model: options?.replaces_model ?? false });
},
},
log: (...args) => console.log(`[${mod}]`, ...args),
};
}