diff --git a/tests/test_remote_mining.py b/tests/test_remote_mining.py index e6c9940f4..cf974afc1 100644 --- a/tests/test_remote_mining.py +++ b/tests/test_remote_mining.py @@ -52,9 +52,11 @@ impl World { u32::from(!self.broken.contains(&[x, y, z])) } } -struct Audio; +struct Audio { played: Vec } impl Audio { - fn play(&self, _: &str, _: Option<[f64; 3]>, _: f32, _: f32) {} + fn play(&mut self, name: &str, _: Option<[f64; 3]>, _: f32, _: f32) { + self.played.push(name.to_string()); + } } mod play { #[derive(Debug, PartialEq)] @@ -78,7 +80,8 @@ impl Game { world: World::default(), res: Resources { facts: Facts { hardness, name: "block".into(), requires_tool: false, sounds: Sounds { hit: "hit".into(), volume: 1.0, pitch: 1.0 } }, speed, tool_allows_creative: true }, - player: Some(Player { ground: Ground { on_ground: true } }), audio: Audio, + player: Some(Player { ground: Ground { on_ground: true } }), + audio: Audio { played: Vec::new() }, hit: Hit { pos: [0, 64, 0], face: 1 }, sequence: 0, packets: vec![], } } @@ -92,7 +95,13 @@ impl Game { fn predict_break(&mut self, pos: [i32; 3], _: u32, _: i32) { self.world.broken.push(pos); } fn send(&mut self, S::PlayerAction(packet): S) { self.packets.push(packet); } fn crack_particle(&mut self, _: [i32; 3], _: u8, _: u32) {} - fn breaking_block_effects(&mut self, _: [i32; 3], _: u8, _: bool) {} + // ClientLevel.addBreakingBlockEffects: the hit sound once, when the dig + // starts; the server's level events play it while the block is mined. + fn breaking_block_effects(&mut self, _: [i32; 3], _: u8, play_sound: bool) { + if play_sound { + self.audio.play("hit", None, 1.0, 1.0); + } + } METHOD } fn main() { @@ -118,10 +127,14 @@ fn main() { let mut game = Game::new(1.0, 15.0, false); game.remote_mining(true); assert!(game.world.broken.is_empty()); + // The dig plays the hit sound once, when it starts (#2277): the server's + // destroy progress level events play it while the block is mined. + assert_eq!(game.audio.played, ["hit"]); game.remote_mining(false); assert!(game.world.broken.is_empty()); game.remote_mining(false); assert_eq!(game.world.broken.len(), 1); + assert_eq!(game.audio.played, ["hit"], "the client must not play the mining sound in the loop"); assert_eq!(game.packets.iter().map(|p| p.action).collect::>(), [0, 3]); assert_eq!(game.remote.as_ref().unwrap().destroy_delay, 5); game.hit.pos[0] = 1; diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 69a22fe48..219c88945 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -7852,5 +7852,45 @@ CHECKS += ['dig_stop'] CHECK_FNS['dig_stop'] = check_dig_stop +def check_dig_sounds(host, guest, out): + """#2277: the mining sound plays once per cadence, for everyone. + + Vanilla plays the block's hit sound from the server's destroy progress + level events (2019 and 2020), so the digger and the players around hear + the same hits. Horizon's digger heard every hit twice: once from its own + client and once from the server, which is what the report called a sound + played too often. + """ + result = {} + spots = apart(host, guest) + for label, player in (('host', host), ('guest', guest)): + other = guest if label == 'host' else host + x, y, z = spots[label] + cell = (x, y + 1, z + 2) + player('command', command='gamemode survival') + player('command', command='clear @s') + host('command', command=f'fill {x - 1} {y} {z + 1} {x + 1} {y + 3} {z + 4} minecraft:air') + host('command', command=f'setblock {cell[0]} {cell[1]} {cell[2]} minecraft:stone') + player.wait(10) + before = {g.name: heard(g, 'minecraft:block.stone.hit') for g in (host, guest)} + player.raw('look-at', f'{cell[0] + 0.5:.2f}', f'{cell[1] + 0.5:.2f}', f'{cell[2] + 0.5:.2f}') + player.raw('mouse', 'left', 'down') + player.wait(40) + player.raw('mouse', 'left', 'up') + player.wait(5) + hits = {g.name: heard(g, 'minecraft:block.stone.hit') - before[g.name] for g in (host, guest)} + result[label] = hits + assert host.raw('block', *cell).get('block') == 'minecraft:stone', f'{label}: the stone broke: {result}' + assert hits[other.name] >= 3, f'{label}: the player nearby heard no mining sound: {result}' + assert hits[player.name] <= hits[other.name] + 3, \ + f'{label}: the digger heard the mining sound twice: {result}' + host('command', command=f'setblock {cell[0]} {cell[1]} {cell[2]} minecraft:air') + return result + + +CHECKS += ['dig_sounds'] +CHECK_FNS['dig_sounds'] = check_dig_sounds + + if __name__ == '__main__': main()