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);
}
}
}
+7
View File
@@ -71,6 +71,13 @@ export interface BlockComponent<P = any> {
on_second?(block: BlockRef, params: P, dt: number): void;
// every morning at 06:00, for every block with data in the world, near a player or not. day is ServerWorld.day
on_new_day?(block: BlockRef, params: P, day: number): void;
// a block on one of its 6 sides was placed, broken or changed its states, like neoforge's neighborChanged. after
// the shared rules' on_neighbor_change, so it sees what they changed, and can do what rules can't: read tile data,
// change other blocks. neighbor is where, and what's there now
on_neighbor_change?(block: BlockRef, params: P, neighbor: { readonly id: Id } & Position): void;
// its own states changed without it being replaced, by set_state or a rule, like a clutch another mod powers.
// before is what they were. it runs right away, before the blocks beside it hear of the change
on_state_change?(block: BlockRef, params: P, before: Readonly<Record<string, number>>): void;
// placing and neighbor rules are in shared scripts, see BlockRules in bworld/shared
}