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
+17 -2
View File
@@ -873,6 +873,7 @@ export class GameServer {
if (is_block_position(message.x, message.y, message.z)) {
this.#break_block(player, message.x, message.y, message.z);
}
this.#ack(player, message.seq);
break;
case "hit_block":
if (is_block_position(message.x, message.y, message.z)) {
@@ -899,6 +900,7 @@ export class GameServer {
: [0.5, 0.5, 0.5];
this.#use_block(player, message.x, message.y, message.z, message.face, hit);
}
this.#ack(player, message.seq);
break;
case "select_slot":
if (is_int(message.slot) && message.slot >= 0 && message.slot < 9) {
@@ -1044,11 +1046,14 @@ export class GameServer {
const before_interact = { ...interact, cancel: false };
this.mods.before.block_interact.emit(before_interact);
if (before_interact.cancel) {
// the client may have guessed the shared rules' on_interact, or a placement
this.#correct(player, x, y, z);
this.#correct(player, tx, ty, tz);
return;
}
let handled = false;
// the shared rules first, the ones the client guessed, then the server script's
let handled = this.mods.rules.interact(used, x, y, z, { face, hit: [hit[0], hit[1], hit[2]] }, player.yaw);
for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) {
if (component.on_interact) {
handled = run_guarded(mod, `${component_id} on_interact`, () =>
@@ -1058,7 +1063,9 @@ export class GameServer {
}
if (handled) {
this.mods.after.block_interact.emit(interact);
// the client didn't guess a placement for interactive blocks, but it might have for others
// the client guessed the shared rules' on_interact from what it sees, which might not be what's here. it didn't
// guess a placement for interactive blocks, but it might have for others
this.#correct(player, x, y, z);
this.#correct(player, tx, ty, tz);
return;
}
@@ -1848,6 +1855,14 @@ export class GameServer {
}
// tell the player what's really at a position, undoing anything their client guessed
// the client's break_block or use_block with seq is handled, after the set_blocks it caused, even when it's
// ignored: the client holds back what the server says about the blocks it guessed until this comes
#ack(player: ServerPlayer, seq: unknown) {
if (is_int(seq) && seq > 0) {
this.#send(player, { type: "block_ack", seq });
}
}
#correct(player: ServerPlayer, x: number, y: number, z: number) {
if (y < 0 || y >= CHUNK_HEIGHT || !player.chunks.has_block(x, z)) {
return;