Merge pull request #427 from ludovic111/fix/lan-chest-lids

LAN: chest and shulker box lids open and close for every player
This commit is contained in:
Ludovic Marie
2026-10-16 00:09:53 +02:00
committed by GitHub
7 changed files with 101 additions and 19 deletions
+4
View File
@@ -1010,6 +1010,9 @@ struct Game<'a> {
/// position) for the guests.
lan_block_events: Vec<([i32; 3], i32, i32, u16)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
/// `ContainerOpenersCounter`: how many players (the host and the guests)
/// have each chest open.
lid_openers: HashMap<(i32, i32, i32), i32>,
lan_acting: Option<u32>,
/// Command feedback to forward to a guest.
chat_capture: Option<Vec<horizon_client::text::Text>>,
@@ -10078,6 +10081,7 @@ pub fn run() -> Result<()> {
lan_events: Vec::new(),
lan_block_events: Vec::new(),
lan_entity_events: Vec::new(),
lid_openers: HashMap::new(),
lan_acting: None,
chat_capture: None,
cmds: Default::default(),
+10 -3
View File
@@ -4,7 +4,7 @@
//! take time run as a task advanced every frame and reply when they finish or
//! time out, so `horizon-ctl break 10 64 3` returns what the block dropped.
use super::{Game, Screen};
use horizon_client::{Result, items::ItemStack, widgets};
use horizon_client::{Result, block_entity_anim::Ticker, items::ItemStack, widgets};
use horizon_core::geometry::Vec3;
use horizon_world::registry::BlockStateId;
use serde_json::{Value, json};
@@ -309,12 +309,19 @@ impl Game<'_> {
}
fn block_json(&self, pos: [i32; 3]) -> Value {
let state = self.world.block(pos[0], pos[1], pos[2]);
json!({
let mut v = json!({
"pos": pos,
"block": &*self.res.facts.get(state).name,
"state": self.state_string(state),
"loaded": self.world.chunks.contains_key(&(pos[0] >> 4, pos[2] >> 4)),
})
});
// How far a chest's or a shulker box's lid is open, 0..1, as drawn.
match self.be_anims.ticker(state.0) {
Some(Ticker::Lid) => v["lid"] = json!(self.chest_lid(pos, 1.0)),
Some(Ticker::Shulker { .. }) => v["lid"] = json!(self.block_anim(pos, 1.0).shulker.0),
_ => {}
}
v
}
pub(super) fn stack_value(&self, s: &ItemStack) -> Value {
let mut v = json!({ "item": &*s.item, "count": s.count });
@@ -69,17 +69,19 @@ impl Game<'_> {
/// on both halves of a double chest (`CompoundContainer`).
pub(super) fn container_opener_event(&mut self, key: (i32, i32, i32), open: bool) {
// Spectators never count as openers.
if self.spectator() {
return;
if !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) {
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) => {
let count = i32::from(open);
self.block_entity_event(pos, 1, count);
self.lid_openers_changed(key, open);
if let Some(other) = self.chest_other_half(key) {
self.block_entity_event([other.0, other.1, other.2], 1, count);
self.lid_openers_changed(other, open);
}
}
Some(Ticker::Shulker { .. }) => {
@@ -90,10 +92,26 @@ impl Game<'_> {
shulker.stop_open()
};
self.block_entity_event(pos, 1, count);
self.lan_block_event(pos, 1, count);
}
_ => {}
}
}
/// `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) {
let count = self.lid_openers.entry(key).or_default();
*count = (*count + if open { 1 } else { -1 }).max(0);
let count = *count;
if count == 0 {
self.lid_openers.remove(&key);
}
let pos = [key.0, key.1, key.2];
self.block_entity_event(pos, 1, count);
self.lan_block_event(pos, 1, count);
}
/// `BellBlockEntity.updateEntities`: the living entities whose bounding
/// box meets the bell's block box inflated by 48.
fn bell_gather(&self, pos: [i32; 3]) -> Vec<u64> {
@@ -233,7 +251,8 @@ impl Game<'_> {
f64::from(pos[1]) + 0.5,
f64::from(pos[2]) + 0.5,
];
if tick.resonate {
// On a server its own copy does this and sends the sound.
if tick.resonate && self.remote.is_none() {
// The server's copy plays the resonance for everyone.
self.audio
.play("minecraft:block.bell.resonate", Some(centre), 1.0, 1.0);
@@ -249,8 +268,8 @@ impl Game<'_> {
};
self.particles.add(&options, p, [0.0; 3]);
}
// makeRaidersGlow.
for m in self.mobs.iter_mut() {
// makeRaidersGlow (the server's effect).
for m in self.mobs.iter_mut().filter(|_| self.remote.is_none()) {
if glow.contains(&u64::from(m.id)) {
m.add_effect("minecraft:glowing", 60, 0);
}
@@ -292,7 +311,8 @@ impl Game<'_> {
let initial = self.gateway_loaded(pos);
let anims = &mut self.be_anims;
let server = anims.gateway_servers.entry(pos).or_insert(initial);
let triggered = server.portal_tick();
// A server sends its copy's block event instead.
let triggered = self.remote.is_none() && server.portal_tick();
let client = anims.gateways.entry(pos).or_insert(initial);
if triggered {
client.trigger_event(1, 0);
+6
View File
@@ -640,6 +640,12 @@ impl Game<'_> {
}
}
HostEvent::Left(guest) => {
// The container they had open loses an opener.
if let Some(pos) = guest.menu.as_ref().and_then(|m| m.pos)
&& !guest.spectator()
{
self.container_openers_changed(pos, false);
}
playerdata::save(&guest, &self.world_root);
host.models.remove(&guest.conn);
if self.options.dedicated.is_some() && guest.placed {
+33 -7
View File
@@ -200,8 +200,12 @@ impl Game<'_> {
host.hub.set_game_mode(conn, new_mode);
}
host.play.insert(conn, play);
if let Some(pos) = opened {
self.lan_open_container(host, conn, pos);
if let Some(pos) = opened
&& !self.lan_open_container(host, conn, pos)
&& !host.hub.guests[i].spectator()
{
// Nothing the guest can use opened: its lid closes again.
self.container_openers_changed(pos, false);
}
if let Some(station) = opened_station {
self.lan_station_opened(host, conn, station);
@@ -221,19 +225,20 @@ impl Game<'_> {
self.creative = self.mode == GameMode::Creative;
}
/// Hand a container the guest opened over (ServerPlayer.openMenu).
fn lan_open_container(&mut self, host: &mut LanHost, conn: u32, pos: (i32, i32, i32)) {
/// Hand a container the guest opened over (ServerPlayer.openMenu);
/// false when it is not one a guest can open.
fn lan_open_container(&mut self, host: &mut LanHost, conn: u32, pos: (i32, i32, i32)) -> bool {
let Some(entity) = self.world.block_entities.get(&pos) else {
return;
return false;
};
let (menu, title) = match entity {
BlockEntity::Chest(items) => match items.len() {
27 => ("minecraft:generic_9x3", "container.chest"),
9 => ("minecraft:generic_3x3", "container.dispenser"),
5 => ("minecraft:hopper", "container.hopper"),
_ => return,
_ => return false,
},
_ => return,
_ => return false,
};
let name = &self
.res
@@ -247,6 +252,8 @@ impl Game<'_> {
_ => title,
};
let items = entity.slots().to_vec();
// ServerPlayer.openMenu closes the menu that was open.
self.guest_container_closed(host, conn);
host.hub.open_container(
conn,
menu,
@@ -254,6 +261,18 @@ impl Game<'_> {
pos,
items,
);
true
}
/// The guest's open container is closing (`AbstractContainerMenu.removed`):
/// it no longer counts among the openers of the lid.
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);
}
}
/// A packet from a guest that the hub does not handle itself.
@@ -405,6 +424,13 @@ impl Game<'_> {
self.drop_guest_items(host, gi, drops);
}
S::ContainerClose(p) => {
if host.hub.guests[gi]
.menu
.as_ref()
.is_some_and(|m| m.window == p.container)
{
self.guest_container_closed(host, conn);
}
host.hub.container_closed(conn, p.container);
let g = &mut host.hub.guests[gi];
if let Some(c) = g.carried.take() {
@@ -55,6 +55,8 @@ impl Game<'_> {
state.session.update_special_prices();
}
let title = self.merchant_display_name(&state);
// The container the trading screen replaces loses its opener.
self.guest_container_closed(host, conn);
let Some(window) = host.hub.open_merchant_window(conn, title) else {
self.merchant_closed(&mut state);
return;
+17
View File
@@ -831,6 +831,21 @@ impl Game<'_> {
}
}
C::BlockChangedAck(_) => {}
// ClientLevel.blockEvent: BlockEntity.triggerEvent (chest lids,
// shulker boxes, bells) when the block is still the one it names.
C::BlockEvent(p) => {
let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]);
let block = &self.res.facts.get(state).name;
let named = self.remote.as_ref().and_then(|r| {
r.builtin
.get("minecraft:block")?
.name(p.block)
.map(str::to_owned)
});
if named.as_deref() == Some(&**block) {
self.block_entity_event(p.pos, i32::from(p.b0), i32::from(p.b1));
}
}
C::LevelEvent(p) if p.kind == 2015 => {
self.dragon_egg_particles(p.pos, p.data);
}
@@ -1717,6 +1732,8 @@ impl Game<'_> {
// ClientLevel.tick: the border's size change.
self.border.tick();
self.tick_remote_entities();
// ClientLevel.tickBlockEntities: chest lids, shulker boxes, bells.
self.tick_block_entity_anims();
self.tick_hand();
self.tick_particles();
let Some(player) = self.player.as_mut() else {