Keep sprint state independent of held sprint keys

Let PlayerCommand control the server sprint flag, matching vanilla, so releasing Ctrl cannot leave the client's speed modifier and FOV stuck. Cover the packet sequence and FOV recovery in regression tests.

Co-Authored-By: Codex (GPT-6) <noreply@openai.com>
This commit is contained in:
levzzz
2026-10-25 15:56:53 +05:00
parent fbfec6435f
commit 57ee081038
4 changed files with 110 additions and 2 deletions
+2 -1
View File
@@ -469,7 +469,8 @@ impl Game<'_> {
"flying": p.flying,
"fall_flying": p.fall_flying,
"sneaking": self.keys.contains(&KeyCode::ShiftLeft),
"sprinting": self.keys.contains(&KeyCode::ControlLeft),
"sprinting": p.sprinting,
"fov_modifier": self.view.fov_modifier,
"health": p.health.health(),
"absorption": p.health.absorption(),
// The hurt animation's ticks left (the camera's hurt tilt).
+3 -1
View File
@@ -1984,7 +1984,9 @@ impl Hub {
S::PlayerInput(p) => {
self.guests[i].input = p.flags;
self.guests[i].sneaking = p.flags & 32 != 0;
self.guests[i].sprinting = p.flags & 64 != 0 && p.flags & 1 != 0;
// handlePlayerInput only records keys and updates crouching.
// Sprinting persists after the sprint key is released (or
// starts with a double tap); PlayerCommand changes it.
}
// OPEN_INVENTORY (the ridden mount's inventory) and
// START_FALL_FLYING (checked against the guest's chest slot
+59
View File
@@ -278,6 +278,65 @@ fn run<T: Send + 'static>(
handle.join().unwrap().unwrap()
}
#[test]
fn sprint_commands_survive_keyboard_input_changes() {
let Some(resources) = resources() else {
return;
};
let mut s = server(&resources);
let port = s.hub.lan.port;
let _bot = run(&mut s, move || connect(port, "Sprinter", true));
let deadline = Instant::now() + Duration::from_secs(5);
while s.hub.guests.is_empty() {
s.tick(&[]);
assert!(Instant::now() < deadline, "guest did not join");
thread::yield_now();
}
let id = s.hub.guests[0].entity_id;
s.hub.guests[0].loaded = true;
s.hub.guests[0]
.inbox
.push_back(S::PlayerCommand(play::PlayerCommand {
id,
action: 1,
data: 0,
}));
s.tick(&[]);
assert!(s.hub.guests[0].sprinting);
// LocalPlayer keeps sprinting after Ctrl is released while W remains
// down. Double-tap sprint also sends forward without the sprint key.
for flags in [65, 1, 0, 33] {
s.hub.guests[0]
.inbox
.push_back(S::PlayerInput(play::PlayerInput { flags }));
s.tick(&[]);
let g = &s.hub.guests[0];
assert!(
g.sprinting,
"keyboard input {flags} cleared the sprint flag"
);
assert_eq!(g.input, flags);
assert_eq!(g.sneaking, flags & 32 != 0);
}
s.hub.guests[0]
.inbox
.push_back(S::PlayerCommand(play::PlayerCommand {
id,
action: 2,
data: 0,
}));
s.tick(&[]);
assert!(!s.hub.guests[0].sprinting);
s.hub.guests[0]
.inbox
.push_back(S::PlayerInput(play::PlayerInput { flags: 65 }));
s.tick(&[]);
assert!(
!s.hub.guests[0].sprinting,
"keyboard input started a sprint"
);
}
#[test]
fn status_and_join() {
let Some(resources) = resources() else {
+46
View File
@@ -6162,5 +6162,51 @@ CHECKS += ['falling']
CHECK_FNS.update(falling=check_falling)
def check_sprint_fov(host, guest, out):
"""Releasing Ctrl must preserve a sprint until W is released, then reset FOV.
PlayerInput reports keys; PlayerCommand starts/stops the sprint. Confusing
them clears the client's sprint flag while leaving its speed modifier on.
"""
seen = {}
for game in (host, guest):
game('command', command='gamemode creative')
game('command', command='effect clear @s')
game.raw('options-set', 'fovEffectScale', '1.0')
x, y, z = map(math.floor, pos_of(game))
game('command', command=f'fill {x - 2} {y} {z - 2} {x + 2} {y + 3} {z + 20} minecraft:air')
game('command', command=f'fill {x - 2} {y - 1} {z - 2} {x + 2} {y - 1} {z + 20} minecraft:stone')
game('command', command=f'tp @s {x + 0.5} {y} {z + 0.5} 0 0')
game.wait(20)
try:
game.raw('key', 'w', 'down')
game.raw('key', 'ctrl', 'down')
game.wait(10)
started = game('state')['player']
game.raw('key', 'ctrl', 'up')
game.wait(10)
released_ctrl = game('state')['player']
game.raw('key', 'w', 'up')
game.wait(20)
stopped = game('state')['player']
finally:
game.raw('key', 'ctrl', 'up')
game.raw('key', 'w', 'up')
seen[game.name] = states = {
label: {k: p[k] for k in ('sprinting', 'fov_modifier')}
for label, p in [('started', started), ('released_ctrl', released_ctrl), ('stopped', stopped)]
}
for label in ('started', 'released_ctrl'):
assert states[label]['sprinting'], f'{game.name} {label}: {states}'
assert abs(states[label]['fov_modifier'] - 1.15) < 0.002, f'{game.name} {label}: {states}'
assert not stopped['sprinting'], f'{game.name}: {states}'
assert abs(stopped['fov_modifier'] - 1.0) < 0.002, f'{game.name}: {states}'
return seen
CHECKS += ['sprint-fov']
CHECK_FNS['sprint-fov'] = check_sprint_fov
if __name__ == '__main__':
main()