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) {