A server's attribute updates reach its player; new boss bars start visible

The client of a server (single player on its server thread, LAN guests)
ignored ClientboundUpdateAttributesPacket for its own player, so a new
maximum health never reached it: its hearts stayed at ten and the health it
was sent was clamped to 20. It now applies each synced instance's base and
modifiers (handleUpdateAttributes) and re-applies the received health.

BossBar::new started hidden; ServerBossEvent starts visible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-23 08:18:29 +02:00
parent 13772b4e5b
commit f88fba8d05
5 changed files with 102 additions and 4 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ impl BossBar {
style: "progress".into(),
value: 0,
max: 100,
visible: false,
visible: true,
player: false,
}
}
@@ -834,6 +834,12 @@ impl Game<'_> {
"velocity": [p.velocity.x, p.velocity.y, p.velocity.z],
"on_ground": p.ground.on_ground,
"health": p.health.health(),
// The maximum the HUD's hearts last read (its cached value).
"max_health": p
.attributes
.lookup(p.attributes.key("minecraft:max_health").as_ref())
.and_then(|h| p.attributes.instance(&h).ok())
.map(|i| i.cached_value()),
"food": p.health.food.food,
})
});
+9
View File
@@ -2128,6 +2128,15 @@ impl Game<'_> {
m.swing();
}
}
// The player's own instances: its maximum health (the hearts the
// HUD draws and the health it may hold), reach, speed and so on.
// Then the received health again, which the old maximum clamped.
C::UpdateAttributes(p) if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) => {
if let Some(pl) = self.player.as_mut() {
horizon_client::lan::attributes::apply(&mut pl.attributes, &p.values);
}
self.apply_remote_health();
}
C::UpdateMobEffect(p) => self.remote_effect(&p),
C::RemoveMobEffect(p) => {
if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id)
+43 -1
View File
@@ -5,7 +5,7 @@
//! changed since (`getAttributesToSync`), to the trackers and the player itself.
use horizon_protocol::packets::play::{AttributeModifier, AttributeSnapshot};
use horizon_world::attributes::{
Instance, Operation,
Instance, Modifier, Operation,
entity::{AttributeId, EntityAttributes},
live::LiveAttributes,
};
@@ -70,6 +70,48 @@ pub fn take_dirty<G: FnMut() -> i32>(map: &mut LiveAttributes<G>) -> Vec<Attribu
.collect()
}
/// `ClientPacketListener.handleUpdateAttributes`: each instance the server
/// synced takes its base and exactly its modifiers (`removeModifiers`, then
/// `addTransientModifier` for each). An attribute the map doesn't know is
/// skipped, as vanilla warns and skips it.
pub fn apply<G: FnMut() -> i32>(map: &mut LiveAttributes<G>, values: &[AttributeSnapshot]) {
for s in values {
let Some(key) = map
.keys()
.iter()
.find(|k| k.attribute().registry_id() == s.attribute)
.cloned()
else {
continue;
};
let Ok(Some(handle)) = map.get_instance(Some(key)) else {
continue;
};
let modifiers: Vec<_> = s
.modifiers
.iter()
.filter_map(|m| Modifier::new(&m.id, m.amount, operation(m.operation)).ok())
.collect();
let _ = map.edit(&handle, |instance, observer| {
instance.set_base_value(s.base, observer)?;
instance.remove_all(observer)?;
for m in modifiers {
instance.add_transient(m, observer)?;
}
Ok(())
});
}
}
/// `AttributeModifier.Operation.BY_ID`.
fn operation(id: i32) -> Operation {
match id {
1 => Operation::AddMultipliedBase,
2 => Operation::AddMultipliedTotal,
_ => Operation::AddValue,
}
}
/// The client-syncable instances of a generic attribute map.
pub fn generic_syncable(map: &EntityAttributes) -> impl Iterator<Item = AttributeSnapshot> + '_ {
let catalog = map.catalog();
+43 -2
View File
@@ -213,7 +213,7 @@ ROOT = Path(__file__).resolve().parents[1]
GUEST_NAME = 'Guest'
# The host plays single player on its server thread (--server-thread).
SERVER_THREAD = False
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater']
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater']
class Game:
@@ -2711,6 +2711,46 @@ def check_sparks(host, guest, out):
return result
def hearts(game):
"""The player's maximum and current health as its own client holds them."""
p = game('status')['player']
return p.get('max_health'), round(p['health'], 1)
def check_hud(host, guest, out):
result = {}
for label, player in (('host', host), ('guest', guest)):
player('command', command='gamemode survival')
try:
for maximum in (40, 10):
player('command', command=f'attribute @s minecraft:max_health base set {maximum}')
player.wait(10)
player('command', command='effect give @s minecraft:instant_health 1 10')
seen = None
for _ in range(15):
player.wait(2)
seen = hearts(player)
if seen == (maximum, maximum):
break
result[f'{label} {maximum}'] = seen
player.shot(out, f'hearts-{label}-{maximum}')
finally:
player('command', command='attribute @s minecraft:max_health base reset')
player('command', command='effect give @s minecraft:instant_health 1 10')
# BossBarCommands' new bar starts visible (ServerBossEvent's constructor).
host('command', command='bossbar add lan_e2e:bar "LAN bar"')
try:
host('command', command='bossbar get lan_e2e:bar visible')
host.wait(5)
result['bar'] = [line for line in host.raw('messages', 5)['chat'] if 'lan_e2e:bar' in line or 'LAN bar' in line]
finally:
host('command', command='bossbar remove lan_e2e:bar')
wrong = {k: v for k, v in result.items() if k != 'bar' and v != (int(k.split()[1]),) * 2}
assert not wrong, f'hearts: {wrong}\n{result}'
assert any('currently visible' in line for line in result['bar']), f'bar: {result["bar"]}'
return result
def check_blast(host, guest, out):
guest('command', command='gamemode survival')
guest('command', command='clear @s')
@@ -4046,7 +4086,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour,
'looks': check_looks,
'leads': check_leads,
'thrown': check_thrown,
'sparks': check_sparks}
'sparks': check_sparks,
'hud': check_hud}
def run(ctl, out, resources, checks):