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
+19 -3
View File
@@ -172,11 +172,27 @@ Deno.test("players hit each other, knocking them back and flashing red for every
send(1, { type: "attack_entity", id: bob.id });
assertEquals(bob.health, 19);
ticks(1);
const motion = take(2).find((m) => m.type === "set_motion");
assert(motion && motion.vz > 0, "bob's client is told he's knocked back, away from alice");
assert(take(1).some((m) => m.type === "entity_event" && m.id === bob.id && m.event === "hurt"));
// bob's client sends nothing, like in a background tab, and the server knocks him back anyway
ticks(1);
assert(bob.z > 2.5 && bob.y > 100, "up and away from alice");
const teleport = take(2).find((m) => m.type === "teleport");
assert(teleport.motion[2] > 0, "his client keeps him going between updates");
assert(take(1).some((m) => m.type === "player_move" && m.id === bob.id), "alice sees him move");
const knocked_to = bob.z;
send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0, teleport: teleport.id });
assertEquals(bob.z, knocked_to, "his own moves wait until he lands");
ticks(20);
assertEquals(bob.knockback_ticks, undefined, "landed");
assert(bob.on_ground && bob.z > knocked_to);
const last = take(2).filter((m) => m.type === "teleport").at(-1);
send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0, teleport: last.id - 1 });
assert(bob.z > knocked_to, "a move sent before hearing of the last one is ignored");
send(2, { type: "move", x: 0.5, y: 100, z: 5, yaw: 0, pitch: 0, teleport: last.id });
assertEquals(bob.z, 5, "his client moves him again");
send(1, { type: "attack_entity", id: alice.id });
assertEquals(alice.health, 20, "not yourself");