Make server control knockback

This commit is contained in:
2026-09-30 10:54:09 -03:00
parent 4fbcca6851
commit dc70ada7c4
8 changed files with 99 additions and 53 deletions
+24 -11
View File
@@ -466,7 +466,16 @@ export class GameServer {
teleport(player: ServerPlayer, x: number, y: number, z: number) {
Object.assign(player, { x, y, z });
player.fall_distance = 0;
this.#send(player, { type: "teleport", x, y, z });
player.stop_knockback();
this.#place(player);
}
// tells their client where they are now (and how fast they're going, while the server moves them), and everyone
// else where they are
#place(player: ServerPlayer, motion?: [number, number, number]) {
const { x, y, z } = player;
player.teleport_id += 1;
this.#send(player, { type: "teleport", id: player.teleport_id, x, y, z, ...(motion && { motion }) });
this.#update_view(player);
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw: player.yaw, pitch: player.pitch }, player);
}
@@ -709,6 +718,11 @@ export class GameServer {
if (![x, y, z, yaw, pitch].every(is_number)) {
return;
}
// the server is moving them, or it's from before the last time it did
const stale = is_number(message.teleport) && message.teleport < player.teleport_id;
if (player.knockback_ticks !== undefined || stale) {
return;
}
this.#on_move(player, x, y, z);
Object.assign(player, { x, y, z, yaw, pitch });
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw, pitch }, player);
@@ -1381,8 +1395,14 @@ export class GameServer {
#tick_vitals() {
for (const player of this.#players.values()) {
// burning, effects, and falling out of the world
// burning, effects, falling out of the world, and being knocked back
const knocked_back = player.knockback_ticks !== undefined;
player.tick();
if (knocked_back && player.alive) {
// the last one hands what's left of the slide to their client
this.#place(player, [player.vx, player.vy, player.vz]);
if (player.knockback_ticks === undefined) player.stop_knockback();
}
player.vitals.tick(
player.health,
(amount) => this.heal_player(player, amount),
@@ -1399,6 +1419,8 @@ export class GameServer {
const ticks = Math.max(1, this.#tick - player.last_move_tick);
player.last_move_tick = this.#tick;
const horizontal = Math.hypot(dx, dz);
// knockback only lifts them off the ground
player.on_ground = this.#on_ground(x, y, z);
// teleports and the first move after joining aren't walking
if (horizontal > TELEPORT_DISTANCE || Math.abs(dy) > TELEPORT_DISTANCE) {
player.fall_distance = 0;
@@ -1421,8 +1443,6 @@ export class GameServer {
if (dy < 0 && !in_liquid) {
player.fall_distance -= dy;
}
// knockback only lifts them off the ground
player.on_ground = this.#on_ground(x, y, z);
if (player.on_ground) {
const damage = Math.ceil(player.fall_distance - SAFE_FALL);
player.fall_distance = 0;
@@ -1530,13 +1550,6 @@ export class GameServer {
}
};
// knockback, their client moves them
const motion = player.take_motion();
if (motion) {
const [vx, vy, vz] = motion;
this.#send(player, { type: "set_motion", vx, vy, vz });
}
// with lore, so it's resent when only the lore changed
const inventory = player.inventory.to_synced_data();
sync("inventory", inventory, () => ({ type: "container", container: "inventory", items: inventory }));