diff --git a/crates/horizon-client/src/game/armor_stands.rs b/crates/horizon-client/src/game/armor_stands.rs index 4c0bd32e3..b1a4874b8 100644 --- a/crates/horizon-client/src/game/armor_stands.rs +++ b/crates/horizon-client/src/game/armor_stands.rs @@ -16,6 +16,19 @@ use horizon_core::{ }; use std::{collections::HashMap, path::PathBuf, sync::Arc}; +/// `EquipmentLayerRenderer.getColorForLayer` for a dyeable layer: the +/// stack's `minecraft:dyed_color` wins, else the asset's undyed colour, else +/// white. The result is in the renderer's byte order (R and B swapped). +fn layer_color(undyed: Option, stack: Option<&ItemStack>) -> u32 { + let dyed = stack + .and_then(|s| s.data()) + .and_then(|d| d.dyed_color) + .map(|c| c as u32 & 0xFF_FFFF); + dyed.or(undyed).map_or(0xFFFF_FFFF, |c| { + 0xFF00_0000 | (c & 0xFF) << 16 | (c & 0xFF00) | (c >> 16) & 0xFF + }) +} + /// Armor stand models and the humanoid layers of every equipment asset. pub(super) struct ArmorStandAssets { pub main: Option, @@ -179,9 +192,7 @@ impl Game<'_> { ) else { continue; }; - let color = undyed.map_or(0xFFFF_FFFF, |c| { - 0xFF00_0000 | (c & 0xFF) << 16 | (c & 0xFF00) | (c >> 16) & 0xFF - }); + let color = layer_color(*undyed, stack); out.push((part, [r.u0, r.v0, r.u1, r.v1], color)); // EquipmentLayerRenderer: the trim layer over the armour. if let Some(r) = stack @@ -695,9 +706,7 @@ impl Game<'_> { else { continue; }; - let color = undyed.map_or(0xFFFF_FFFF, |c| { - 0xFF00_0000 | (c & 0xFF) << 16 | (c & 0xFF00) | (c >> 16) & 0xFF - }); + let color = layer_color(*undyed, Some(stack)); // ArmorStandRenderer's HumanoidArmorLayer: the armour glint // when `stack.hasFoil()`, as on a living entity. let item_glint = self @@ -993,3 +1002,19 @@ mod burn_drop_tests { assert_eq!(drops, vec![kept]); } } +#[cfg(test)] +mod layer_color_tests { + use super::*; + #[test] + fn a_dyed_stack_overrides_the_undyed_leather_colour() { + let mut dyed = ItemStack::new("minecraft:leather_helmet", 1); + dyed.data_mut().dyed_color = Some(0x1E_90FF); + // Dyed blue, R and B swapped into the renderer's order, opaque. + assert_eq!(layer_color(Some(0xA06540), Some(&dyed)), 0xFFFF_901E); + // An undyed stack keeps the asset's brown. + let plain = ItemStack::new("minecraft:leather_helmet", 1); + assert_eq!(layer_color(Some(0xA06540), Some(&plain)), 0xFF40_65A0); + // No dye and no undyed colour: white. + assert_eq!(layer_color(None, Some(&plain)), 0xFFFF_FFFF); + } +}