From b3a499d85d720acf6d133f47f79806edfa485560 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 15:18:05 +0100 Subject: [PATCH 1/3] Play chest, barrel and shulker box sounds with a random pitch from the right spot --- crates/horizon-client/src/game.rs | 22 ++------------ crates/horizon-client/src/game/storage.rs | 35 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 1d86fe990..147aefe36 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3077,16 +3077,7 @@ impl Game<'_> { None }; if let Some(sound) = sound { - self.audio.play( - sound, - Some([ - f64::from(key.0) + 0.5, - f64::from(key.1) + 0.5, - f64::from(key.2) + 0.5, - ]), - 0.5, - 1.0, - ); + self.container_sound(sound, key, &name); } } else if self.open_station(pos, &name) { // Workstation menus: game/stations.rs. @@ -5654,16 +5645,7 @@ impl Game<'_> { None }; if let Some(sound) = sound { - self.audio.play( - sound, - Some([ - f64::from(key.0) + 0.5, - f64::from(key.1) + 0.5, - f64::from(key.2) + 0.5, - ]), - 0.5, - 1.0, - ); + self.container_sound(sound, key, &name); } self.world.modified.insert((key.0 >> 4, key.2 >> 4)); } diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 1bcb6a524..3456c4ab0 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -3,6 +3,7 @@ //! player's `PlayerEnderChestContainer`, saved as `EnderItems`). use super::Game; use horizon_client::{containers::BlockEntity, items::ItemStack, text::Text}; +use horizon_core::random::RandomSource; pub(super) fn is_shulker_box(name: &str) -> bool { name.ends_with("shulker_box") @@ -107,6 +108,40 @@ impl Game<'_> { ); paired.then_some((other, kind == "right")) } + /// The open or close sound of a container block (`ChestBlockEntity.playSound`, + /// `BarrelBlockEntity.playSound`, `ShulkerBoxBlockEntity.startOpen`): volume + /// 0.5 and pitch 0.9 to 1.0. A double chest sounds once from the middle of + /// both halves, a barrel from the face it opens on. + pub(super) fn container_sound(&mut self, sound: &str, key: (i32, i32, i32), name: &str) { + let mut at = [ + f64::from(key.0) + 0.5, + f64::from(key.1) + 0.5, + f64::from(key.2) + 0.5, + ]; + if let Some((other, _)) = self.chest_partner(key) { + at[0] = f64::from(key.0 + other.0) / 2.0 + 0.5; + at[2] = f64::from(key.2 + other.2) / 2.0 + 0.5; + } else if name == "minecraft:barrel" + && let Some(state) = self + .pack + .assets + .registry + .state(self.world.block(key.0, key.1, key.2)) + && let Some(facing) = state.properties.get("facing") + { + let (dx, dy, dz) = match facing.as_str() { + "up" => (0.0, 0.5, 0.0), + "down" => (0.0, -0.5, 0.0), + f => { + let (x, z) = facing_step(f); + (f64::from(x) / 2.0, 0.0, f64::from(z) / 2.0) + } + }; + at = [at[0] + dx, at[1] + dy, at[2] + dz]; + } + let pitch = self.world.random.next_float() * 0.1 + 0.9; + self.audio.play(sound, Some(at), 0.5, pitch); + } /// The other half of a double chest at `key`, for neighbour updates. pub(super) fn chest_other_half(&self, key: (i32, i32, i32)) -> Option<(i32, i32, i32)> { self.chest_partner(key).map(|p| p.0) From 7102727197458311255f1c8ccf9f903cdb611d50 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 15:24:26 +0100 Subject: [PATCH 2/3] Play the container close sound when a server-thread player closes it --- crates/horizon-client/src/game.rs | 21 +------------------ crates/horizon-client/src/game/lan_actions.rs | 5 ++++- crates/horizon-client/src/game/storage.rs | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 147aefe36..d0e060c13 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5627,26 +5627,7 @@ impl Game<'_> { Some(horizon_client::mobs::Who::Player), None, ); - let name = self - .res - .facts() - .get(self.world.block(key.0, key.1, key.2)) - .name - .clone(); - let sound = if storage::is_shulker_box(&name) { - Some("minecraft:block.shulker_box.close") - } else if &*name == "minecraft:ender_chest" { - Some("minecraft:block.ender_chest.close") - } else if name.contains("chest") { - Some("minecraft:block.chest.close") - } else if &*name == "minecraft:barrel" { - Some("minecraft:block.barrel.close") - } else { - None - }; - if let Some(sound) = sound { - self.container_sound(sound, key, &name); - } + self.container_close_sound(key); self.world.modified.insert((key.0 >> 4, key.2 >> 4)); } self.close_ender_chest(); diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 0ad14bbcc..41ada973c 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -854,13 +854,16 @@ impl Game<'_> { } /// The guest's open container is closing (`AbstractContainerMenu.removed`): - /// it no longer counts among the openers of the lid. + /// it no longer counts among the openers of the lid, and the lid's close + /// sound is played for everyone around (`ChestBlockEntity.onClose`, a + /// `playSound` with no player), the guest included. pub(super) fn guest_container_closed(&mut self, host: &mut LanHost, conn: u32) { if let Some(g) = host.hub.guest(conn) && let Some(pos) = g.menu.as_ref().and_then(|m| m.pos) && !g.spectator() { self.container_openers_changed(pos, false); + self.container_close_sound(pos); } } diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 3456c4ab0..d733f1cca 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -142,6 +142,27 @@ impl Game<'_> { let pitch = self.world.random.next_float() * 0.1 + 0.9; self.audio.play(sound, Some(at), 0.5, pitch); } + /// The close sound of the container block at `key`, if it has one. + pub(super) fn container_close_sound(&mut self, key: (i32, i32, i32)) { + let name = self + .res + .facts() + .get(self.world.block(key.0, key.1, key.2)) + .name + .clone(); + let sound = if is_shulker_box(&name) { + "minecraft:block.shulker_box.close" + } else if &*name == "minecraft:ender_chest" { + "minecraft:block.ender_chest.close" + } else if name.contains("chest") { + "minecraft:block.chest.close" + } else if &*name == "minecraft:barrel" { + "minecraft:block.barrel.close" + } else { + return; + }; + self.container_sound(sound, key, &name); + } /// The other half of a double chest at `key`, for neighbour updates. pub(super) fn chest_other_half(&self, key: (i32, i32, i32)) -> Option<(i32, i32, i32)> { self.chest_partner(key).map(|p| p.0) From 18f4dd92aea541b501a154f341b36135210ac4d1 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 16:24:42 +0100 Subject: [PATCH 3/3] Play container open and close sounds only when the opener count crosses zero --- crates/horizon-client/src/game.rs | 33 ++++++------------ .../src/game/block_entity_anims.rs | 34 +++++++++++-------- crates/horizon-client/src/game/lan_actions.rs | 7 ++-- crates/horizon-client/src/game/storage.rs | 30 ++++++++++++---- 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index d0e060c13..73dcfc455 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3019,15 +3019,9 @@ impl Game<'_> { self.craft_table = Some(pos); } else if &*name == "minecraft:ender_chest" { self.open_ender_chest(key); - self.container_opener_event(key, true); - let at = [ - f64::from(key.0) + 0.5, - f64::from(key.1) + 0.5, - f64::from(key.2) + 0.5, - ]; - let pitch = self.world.random.next_float() * 0.1 + 0.9; - self.audio - .play("minecraft:block.ender_chest.open", Some(at), 0.5, pitch); + if self.container_opener_event(key, true) { + self.container_edge_sound(key, true); + } } else if &*name == "minecraft:beacon" && self.open_station(pos, &name) { self.world.block_entities.get_or_insert_with(key, || { BlockEntity::for_block(&name).unwrap_or(BlockEntity::Chest(Vec::new())) @@ -3043,7 +3037,7 @@ impl Game<'_> { ]; self.resolve_container_maps(key, centre); self.container = Some(key); - self.container_opener_event(key, true); + let first = self.container_opener_event(key, true); self.post_block_game_event( "minecraft:container_open", [key.0, key.1, key.2], @@ -3067,17 +3061,8 @@ impl Game<'_> { if self.guarded_by_piglins(&name) { self.anger_piglins(true); } - let sound = if storage::is_shulker_box(&name) { - Some("minecraft:block.shulker_box.open") - } else if name.contains("chest") { - Some("minecraft:block.chest.open") - } else if &*name == "minecraft:barrel" { - Some("minecraft:block.barrel.open") - } else { - None - }; - if let Some(sound) = sound { - self.container_sound(sound, key, &name); + if first { + self.container_edge_sound(key, true); } } else if self.open_station(pos, &name) { // Workstation menus: game/stations.rs. @@ -5620,14 +5605,16 @@ impl Game<'_> { if let Some(key) = self.container.take() { // A cart's or chest boat's items go back to it. self.close_cart_container(key); - self.container_opener_event(key, false); + let last = self.container_opener_event(key, false); self.post_block_game_event( "minecraft:container_close", [key.0, key.1, key.2], Some(horizon_client::mobs::Who::Player), None, ); - self.container_close_sound(key); + if last { + self.container_edge_sound(key, false); + } self.world.modified.insert((key.0 >> 4, key.2 >> 4)); } self.close_ender_chest(); diff --git a/crates/horizon-client/src/game/block_entity_anims.rs b/crates/horizon-client/src/game/block_entity_anims.rs index 3d452596e..4d48b0874 100644 --- a/crates/horizon-client/src/game/block_entity_anims.rs +++ b/crates/horizon-client/src/game/block_entity_anims.rs @@ -71,23 +71,24 @@ impl Game<'_> { /// A player opened (`open`) or closed the container at `key`: the opener /// count block event (`ContainerOpenersCounter` / `ShulkerBoxBlockEntity` /// `startOpen`/`stopOpen`) for chests, ender chests and shulker boxes, - /// on both halves of a double chest (`CompoundContainer`). - pub(super) fn container_opener_event(&mut self, key: (i32, i32, i32), open: bool) { + /// on both halves of a double chest (`CompoundContainer`). True when the + /// count went from 0 to 1 or back to 0: the only moments the container + /// plays its open or close sound. + pub(super) fn container_opener_event(&mut self, key: (i32, i32, i32), open: bool) -> bool { // Spectators never count as openers. - if !self.spectator() { - self.container_openers_changed(key, open); - } + !self.spectator() && self.container_openers_changed(key, open) } /// `container_opener_event` for a player who is not a spectator (a guest). - pub(super) fn container_openers_changed(&mut self, key: (i32, i32, i32), open: bool) { + pub(super) fn container_openers_changed(&mut self, key: (i32, i32, i32), open: bool) -> bool { let pos = [key.0, key.1, key.2]; let state = self.world.block(pos[0], pos[1], pos[2]).0; match self.be_anims.ticker(state) { Some(Ticker::Lid) => { - self.lid_openers_changed(key, open); + let edge = self.lid_openers_changed(key, open); if let Some(other) = self.chest_other_half(key) { self.lid_openers_changed(other, open); } + edge } _ if self .res @@ -97,26 +98,27 @@ impl Game<'_> { .as_ref() == "minecraft:barrel" => { - self.barrel_openers_changed(key, open); + self.barrel_openers_changed(key, open) } Some(Ticker::Shulker { .. }) => { let shulker = self.be_anims.shulkers.entry(pos).or_default(); - let (count, _) = if open { + let (count, edge) = if open { shulker.start_open() } else { shulker.stop_open() }; self.block_entity_event(pos, 1, count); self.lan_block_event(pos, 1, count); + edge } - _ => {} + _ => false, } } /// `ContainerOpenersCounter.incrementOpeners` / `decrementOpeners` of the /// chest at `key` (the host and the guests are counted), then /// `ChestBlockEntity.signalOpenCount`: block event 1 with the count, here - /// and for the guests. - fn lid_openers_changed(&mut self, key: (i32, i32, i32), open: bool) { + /// and for the guests. True when the count crossed zero. + fn lid_openers_changed(&mut self, key: (i32, i32, i32), open: bool) -> bool { let (before, count) = self.count_opener(key, open); let pos = [key.0, key.1, key.2]; self.block_entity_event(pos, 1, count); @@ -134,12 +136,15 @@ impl Game<'_> { b.update_neighbors_at(level, ticks, p.offset(0, -1, 0), block); }); } + (before == 0) != (count == 0) } /// `BarrelBlockEntity.openersCounter`: the first opener opens the barrel /// (`open=true`), the last one to leave closes it (`updateBlockState`). - fn barrel_openers_changed(&mut self, key: (i32, i32, i32), open: bool) { + /// True when the count crossed zero. + fn barrel_openers_changed(&mut self, key: (i32, i32, i32), open: bool) -> bool { let (before, count) = self.count_opener(key, open); - if (before == 0) != (count == 0) { + let edge = (before == 0) != (count == 0); + if edge { self.set_bool_prop( key, "open", @@ -147,6 +152,7 @@ impl Game<'_> { horizon_world::level::flags::UPDATE_ALL, ); } + edge } /// One opener more (`open`) or less at `key`: the count before and after /// (`ContainerOpenersCounter.incrementOpeners` / `decrementOpeners`). diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 41ada973c..796e5f89c 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -856,14 +856,15 @@ impl Game<'_> { /// The guest's open container is closing (`AbstractContainerMenu.removed`): /// it no longer counts among the openers of the lid, and the lid's close /// sound is played for everyone around (`ChestBlockEntity.onClose`, a - /// `playSound` with no player), the guest included. + /// `playSound` with no player), the guest included, once the last opener + /// has left. pub(super) fn guest_container_closed(&mut self, host: &mut LanHost, conn: u32) { if let Some(g) = host.hub.guest(conn) && let Some(pos) = g.menu.as_ref().and_then(|m| m.pos) && !g.spectator() + && self.container_openers_changed(pos, false) { - self.container_openers_changed(pos, false); - self.container_close_sound(pos); + self.container_edge_sound(pos, false); } } diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index d733f1cca..7db0ce82c 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -142,8 +142,10 @@ impl Game<'_> { let pitch = self.world.random.next_float() * 0.1 + 0.9; self.audio.play(sound, Some(at), 0.5, pitch); } - /// The close sound of the container block at `key`, if it has one. - pub(super) fn container_close_sound(&mut self, key: (i32, i32, i32)) { + /// The open or close sound of the container block at `key`, if it has one. + /// Callers play it only when the opener count crosses zero + /// (`ContainerOpenersCounter.onOpen` / `onClose`). + pub(super) fn container_edge_sound(&mut self, key: (i32, i32, i32), open: bool) { let name = self .res .facts() @@ -151,13 +153,29 @@ impl Game<'_> { .name .clone(); let sound = if is_shulker_box(&name) { - "minecraft:block.shulker_box.close" + if open { + "minecraft:block.shulker_box.open" + } else { + "minecraft:block.shulker_box.close" + } } else if &*name == "minecraft:ender_chest" { - "minecraft:block.ender_chest.close" + if open { + "minecraft:block.ender_chest.open" + } else { + "minecraft:block.ender_chest.close" + } } else if name.contains("chest") { - "minecraft:block.chest.close" + if open { + "minecraft:block.chest.open" + } else { + "minecraft:block.chest.close" + } } else if &*name == "minecraft:barrel" { - "minecraft:block.barrel.close" + if open { + "minecraft:block.barrel.open" + } else { + "minecraft:block.barrel.close" + } } else { return; };