New hooks

This commit is contained in:
2026-10-20 13:22:03 -03:00
parent c7a2392591
commit 0af35d1e17
7 changed files with 253 additions and 19 deletions
+18 -3
View File
@@ -4,7 +4,14 @@
// the block rules shared scripts register (see BlockRules in mod_api/shared.ts), run the same way on the server and on
// clients: what states a placed block gets, and how blocks change with the blocks beside them. each side has its own
// BlockRules, with its own world: the server's is the real one, a client's is what it shows and guesses ahead with
import type { BlockInteraction, BlockPlacement, BlockRules as Rules, Face, SharedWorld } from "./mod_api/shared.ts";
import type {
BlockInteraction,
BlockPlacement,
BlockRules as Rules,
Face,
SharedBlock,
SharedWorld,
} from "./mod_api/shared.ts";
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
import { default_block_value, set_state_value } from "./utils.ts";
@@ -124,8 +131,15 @@ export class BlockRules {
}
// tells the blocks beside x y z it changed, so they can change with it like a fence joining it: their rules'
// on_neighbor_change. the world's set_state calls this again for what they change, until nothing changes
notify_neighbors(get_block: SharedWorld["get_block"], x: number, y: number, z: number) {
// on_neighbor_change, then after for each, which the server runs its components' on_neighbor_change in. the
// world's set_state calls this again for what they change, until nothing changes
notify_neighbors(
get_block: SharedWorld["get_block"],
x: number,
y: number,
z: number,
after?: (x: number, y: number, z: number, neighbor: SharedBlock) => void,
) {
const neighbor = { x, y, z, id: get_block(x, y, z) ?? "bworld:air" };
for (const [dx, dy, dz] of NEIGHBORS) {
const [nx, ny, nz] = [x + dx, y + dy, z + dz];
@@ -137,6 +151,7 @@ export class BlockRules {
const at = { x: nx, y: ny, z: nz, id };
this.run(mod, `${rules_id} on_neighbor_change`, () => rules.on_neighbor_change!(at, params, neighbor));
}
after?.(nx, ny, nz, neighbor);
}
}
}