fix(client): draw armor stand custom names as name tags
Holograms are invisible armor stands carrying a CustomName. Armor stands parsed neither CustomName nor CustomNameVisible, and name tags came only from mobs, so a hologram drew nothing. Read both fields from NBT and entity data, keep them across a save, and put armor stands in the name tag pass at their bounding-box height even when their model is skipped. Co-Authored-By: cairn-code (DeepSeek V4.1 Flash) <cairn-code@users.noreply.github.com>
This commit is contained in:
@@ -583,6 +583,10 @@ pub struct ArmorStand {
|
||||
pub no_base_plate: bool,
|
||||
pub invisible: bool,
|
||||
pub marker: bool,
|
||||
/// `CustomName`: the text of a hologram.
|
||||
pub custom_name: Option<crate::text::Text>,
|
||||
/// `CustomNameVisible`.
|
||||
pub name_visible: bool,
|
||||
/// Game time of the last hit (the wobble and the second-hit window).
|
||||
pub last_hit: i64,
|
||||
}
|
||||
@@ -603,6 +607,8 @@ impl ArmorStand {
|
||||
no_base_plate: false,
|
||||
invisible: false,
|
||||
marker: false,
|
||||
custom_name: None,
|
||||
name_visible: false,
|
||||
last_hit: i64::MIN / 2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::{
|
||||
doubles, float, floats, item_from_nbt, item_to_nbt, number, put, read_doubles, text,
|
||||
uuid_from_tag,
|
||||
},
|
||||
text::Text,
|
||||
};
|
||||
use horizon_core::geometry::Vec3;
|
||||
use horizon_world::nbt::{Compound, Tag};
|
||||
@@ -555,6 +556,14 @@ fn custom_name(tag: &Tag) -> Option<String> {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// `CustomName` as text: a string keeps its legacy formatting codes, anything
|
||||
/// else is a component (`ComponentSerialization`).
|
||||
pub fn name_text(tag: &Tag) -> Text {
|
||||
match tag {
|
||||
Tag::String(s) => Text::legacy(&s.to_string_lossy()),
|
||||
t => crate::scoreboard::comp_text(&crate::scoreboard::tag_to_json(t)),
|
||||
}
|
||||
}
|
||||
pub fn mob_from_nbt(tag: &Tag, types: &HashMap<String, Arc<MobType>>, id: u32) -> Option<Mob> {
|
||||
let kind = types.get(&self::kind(tag)?)?.clone();
|
||||
let position = position(tag)?;
|
||||
@@ -1266,6 +1275,21 @@ pub fn armor_stand_to_nbt(a: &ArmorStand, uuid: [i32; 4], raw: Option<&Tag>) ->
|
||||
} else if let Tag::Compound(map) = &mut tag {
|
||||
map.remove(&"Marker".into());
|
||||
}
|
||||
if a.name_visible {
|
||||
put(&mut tag, "CustomNameVisible", Tag::Byte(1));
|
||||
} else if let Tag::Compound(map) = &mut tag {
|
||||
map.remove(&"CustomNameVisible".into());
|
||||
}
|
||||
// A name from the save keeps its own shape unless it changed.
|
||||
if let Some(name) = &a.custom_name
|
||||
&& !tag.get("CustomName").is_some_and(|t| name_text(t) == *name)
|
||||
{
|
||||
put(
|
||||
&mut tag,
|
||||
"CustomName",
|
||||
crate::scoreboard::json_to_tag(&name.component()),
|
||||
);
|
||||
}
|
||||
put(&mut tag, "Health", Tag::Float(a.health.to_bits()));
|
||||
put(&mut tag, "Fire", Tag::Short(a.fire_ticks as i16));
|
||||
// LivingEntity.addAdditionalSaveData and ArmorStand's own fields.
|
||||
@@ -1294,6 +1318,8 @@ pub fn armor_stand_from_nbt(tag: &Tag, id: u32) -> Option<ArmorStand> {
|
||||
a.velocity = motion(tag);
|
||||
a.health = float(tag.get("Health")).unwrap_or(20.0) as f32;
|
||||
a.fire_ticks = number(tag.get("Fire")).unwrap_or(0) as i32;
|
||||
a.custom_name = tag.get("CustomName").map(name_text);
|
||||
a.name_visible = flag(tag, "CustomNameVisible");
|
||||
if let Some(eq) = tag.get("equipment") {
|
||||
for (i, slot) in ArmorStand::SLOTS.iter().enumerate() {
|
||||
a.equipment[i] = eq.get(slot).and_then(item_from_nbt);
|
||||
@@ -1574,6 +1600,31 @@ mod tests {
|
||||
assert!(ominous_item_spawner_from_nbt(&raw).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn armor_stand_names_keep_their_formatting_across_a_save() {
|
||||
let tag = Tag::compound([
|
||||
(
|
||||
"Pos",
|
||||
Tag::List(vec![
|
||||
Tag::Double(0.0f64.to_bits()),
|
||||
Tag::Double(64.0f64.to_bits()),
|
||||
Tag::Double(0.0f64.to_bits()),
|
||||
]),
|
||||
),
|
||||
("CustomName", string("§6Hello")),
|
||||
("CustomNameVisible", Tag::Byte(1)),
|
||||
]);
|
||||
let a = armor_stand_from_nbt(&tag, 7).unwrap();
|
||||
let name = a.custom_name.as_ref().unwrap();
|
||||
assert_eq!(name.plain(), "Hello");
|
||||
assert!(name.spans[0].style.color.is_some());
|
||||
assert!(a.name_visible);
|
||||
let saved = armor_stand_to_nbt(&a, [0; 4], Some(&tag));
|
||||
let restored = armor_stand_from_nbt(&saved, 7).unwrap();
|
||||
assert_eq!(restored.custom_name, a.custom_name);
|
||||
assert!(restored.name_visible);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn falling_block_round_trip_keeps_impact_distance_and_settings() {
|
||||
let state = Tag::compound([("Name", string("minecraft:damaged_anvil"))]);
|
||||
|
||||
@@ -171,6 +171,64 @@ impl Game<'_> {
|
||||
discrete,
|
||||
});
|
||||
}
|
||||
// Armor stands name themselves: an invisible one with a visible name
|
||||
// is a hologram (its model is skipped, its name tag is not).
|
||||
for a in &self.armor_stands {
|
||||
let Some(name) = &a.custom_name else {
|
||||
continue;
|
||||
};
|
||||
if !a.name_visible {
|
||||
continue;
|
||||
}
|
||||
let height = match (a.marker, a.small) {
|
||||
(true, _) => 0.0,
|
||||
(false, true) => 0.9875,
|
||||
(false, false) => 1.975,
|
||||
};
|
||||
let distance_sqr =
|
||||
glam::DVec3::new(a.position.x, a.position.y, a.position.z).distance_squared(camera);
|
||||
if distance_sqr >= 64.0 * 64.0 {
|
||||
continue;
|
||||
}
|
||||
let team = self.mob_team(a.id);
|
||||
let invisible = sb::invisible_to(team, viewer, self.spectator(), false);
|
||||
let view = sb::NameView {
|
||||
discrete: false,
|
||||
distance_sqr,
|
||||
invisible_to_viewer: invisible,
|
||||
hud_hidden: self.ui.hide_gui,
|
||||
is_camera: false,
|
||||
is_vehicle: false,
|
||||
};
|
||||
if !sb::name_shown(team, viewer, view) {
|
||||
continue;
|
||||
}
|
||||
let shown = match team {
|
||||
Some(t) => t.formatted_name(name.clone()),
|
||||
None => name.clone(),
|
||||
};
|
||||
let mut lines = Vec::new();
|
||||
let mut lift = 0.0;
|
||||
let holder = self.persist.uuids.get(&a.id).map(|u| uuid_text(*u));
|
||||
if distance_sqr < 10.0 * 10.0
|
||||
&& let Some(score) = holder.and_then(|h| self.below_name_text(&h))
|
||||
{
|
||||
lines.push((score, 0.0));
|
||||
lift = f64::from(9.0f32 * 1.15 * 0.025);
|
||||
}
|
||||
lines.push((shown, lift));
|
||||
let l = self.world.light(
|
||||
a.position.x.floor() as i32,
|
||||
(a.position.y + height).floor() as i32,
|
||||
a.position.z.floor() as i32,
|
||||
);
|
||||
tags.push(NameTag {
|
||||
at: glam::DVec3::new(a.position.x, a.position.y + height + 0.5, a.position.z),
|
||||
lines,
|
||||
light: l,
|
||||
discrete: false,
|
||||
});
|
||||
}
|
||||
tags
|
||||
}
|
||||
/// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once,
|
||||
|
||||
@@ -123,6 +123,14 @@ impl Game<'_> {
|
||||
} else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) {
|
||||
match (index, value) {
|
||||
(0, Value::Byte(flags)) => a.invisible = flags & 0x20 != 0,
|
||||
// Entity.DATA_CUSTOM_NAME and DATA_CUSTOM_NAME_VISIBLE.
|
||||
(2, Value::OptText(name)) => {
|
||||
a.custom_name = name.as_ref().map(horizon_client::entity_nbt::name_text);
|
||||
}
|
||||
(2, Value::Text(name)) => {
|
||||
a.custom_name = Some(horizon_client::entity_nbt::name_text(name));
|
||||
}
|
||||
(3, Value::Bool(visible)) => a.name_visible = *visible,
|
||||
(9, Value::Float(health)) => a.health = *health,
|
||||
// ArmorStand.DATA_CLIENT_FLAGS.
|
||||
(15, Value::Byte(flags)) => {
|
||||
|
||||
Reference in New Issue
Block a user