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
+40 -6
View File
@@ -581,18 +581,52 @@ export class GameServer {
this.world.dirty = true;
}
// changes a block's state bits without anything else happening, see ServerWorld.get_block_value
// changes a block's state bits without replacing it, see ServerWorld.get_block_value: its on_state_change, then the
// blocks beside it hear of it
set_block_state(x: number, y: number, z: number, state: number) {
const id = this.world.get_block_id(x, y, z);
if (id !== AIR_ID && this.world.get_block_value(x, y, z) >>> 16 !== state) {
this.world.set_block(x, y, z, id, state);
this.#notify_neighbors(x, y, z);
const value = this.world.get_block_value(x, y, z);
if (id === AIR_ID || value >>> 16 === state) return;
const components = this.mods.components_with(id, "on_state_change");
// only worked out for blocks that want it, states change often
const before = components.length > 0 ? block_states(value, this.world.get_block_info(x, y, z)!) : undefined;
this.world.set_block(x, y, z, id, state);
if (before) {
const block = this.mods.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_state_change`,
() => component.on_state_change!(block, params, before),
);
}
}
this.#notify_neighbors(x, y, z);
}
// tells the blocks around one that changed, so they can change with it like a fence joining it, see BlockRules
// tells the blocks around one that changed, so they can change with it like a fence joining it: their rules (see
// BlockRules), then their components' on_neighbor_change
#notify_neighbors(x: number, y: number, z: number) {
this.mods.rules.notify_neighbors((bx, by, bz) => this.world.get_block_id(bx, by, bz), x, y, z);
this.mods.rules.notify_neighbors(
(bx, by, bz) => this.world.get_block_id(bx, by, bz),
x,
y,
z,
(nx, ny, nz, neighbor) => {
// read again, a rule's state change can have had a component replace it
const id = this.world.get_block_id(nx, ny, nz);
const components = this.mods.components_with(id, "on_neighbor_change");
if (components.length === 0) return;
const block = this.mods.block_ref(nx, ny, nz, id);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_neighbor_change`,
() => component.on_neighbor_change!(block, params, neighbor),
);
}
},
);
}
set_block(x: number, y: number, z: number, id: string, player?: ServerPlayer, state?: number) {
+14
View File
@@ -479,6 +479,19 @@ export class ModRuntime {
}
#ticking_components_of = new Map<string, BlockComponentOf[]>();
// the ones with a handler, for handlers that run often, like on_neighbor_change. cached like components_of
components_with(block_id: string, handler: keyof BlockComponent): readonly BlockComponentOf[] {
let of_handler = this.#components_with.get(handler);
if (!of_handler) this.#components_with.set(handler, of_handler = new Map());
let components = of_handler.get(block_id);
if (!components) {
components = this.components_of(block_id).filter(({ component }) => component[handler]);
of_handler.set(block_id, components);
}
return components;
}
#components_with = new Map<keyof BlockComponent, Map<string, BlockComponentOf[]>>();
// components on an item, with their params from its json
item_components_of(item_id: string): { mod: string; id: string; component: ItemComponent; params: unknown }[] {
const item = EverythingRegistry.get<ItemRegistry>("items", item_id);
@@ -604,6 +617,7 @@ export class ModRuntime {
this.block_components.set(id, { mod, component: component as BlockComponent });
this.#components_of.clear();
this.#ticking_components_of.clear();
this.#components_with.clear();
},
register_item: (id, component) => {
setup_only("components");