Predict interactions with blocks

This commit is contained in:
2026-10-10 14:21:27 -03:00
parent 7fe9c15b67
commit 79ff6f9ad5
15 changed files with 355 additions and 73 deletions
+22 -1
View File
@@ -1,7 +1,7 @@
// 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 { BlockPlacement, BlockRules as Rules, SharedWorld } from "./mod_api/shared.ts";
import type { BlockInteraction, BlockPlacement, BlockRules as Rules, SharedWorld } from "./mod_api/shared.ts";
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
import { default_block_value, set_state_value } from "./utils.ts";
@@ -75,6 +75,27 @@ export class BlockRules {
return value;
}
// a player right clicked the block at x y z: its rules' on_interact, every one of them. returns whether any did
// something. what they change goes through the side's world, so on a client it's a guess the server corrects
interact(
block: BlockRegistry,
x: number,
y: number,
z: number,
interaction: Omit<BlockInteraction, "facing">,
yaw: number,
): boolean {
const at = { x, y, z, id: block.id };
const full: BlockInteraction = { ...interaction, facing: horizontal_facing(yaw) };
let handled = false;
for (const { mod, id, rules, params } of this.#of(block)) {
if (!rules.on_interact) continue;
handled = this.run(mod, `${id} on_interact`, () => rules.on_interact!(at, params, full)) === true ||
handled;
}
return handled;
}
// 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) {