From 4ad05aa94a4a1a7c4c9b4b39717a238af5d6584c Mon Sep 17 00:00:00 2001 From: some-du6e <103129948+some-du6e@users.noreply.github.com> Date: Sat, 24 Oct 2026 14:44:17 -0600 Subject: [PATCH 1/3] Show armour trims on items from a server Decode the trim component as its material and pattern registry ids when both are references (a direct holder stays read past) and name them through the server's synced trim_material and trim_pattern registries, as enchantments are, so worn and held trimmed armour draws its trim in multiplayer. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 13 ++++++ .../examples/item_wire_probe.rs | 6 +-- crates/horizon-protocol/src/item.rs | 45 ++++++++++++++++--- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 0e6127178..8cf3d52a0 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -598,6 +598,19 @@ impl Game<'_> { let value = match c { // Read past, not decoded: the item keeps its prototype's value. Component::Raw(_) => continue, + // Trim materials and patterns are synced registries, as enchantments are. + Component::Trim { material, pattern } => { + let (Some(material), Some(pattern)) = ( + r.registries.name("minecraft:trim_material", *material), + r.registries.name("minecraft:trim_pattern", *pattern), + ) else { + continue; + }; + Tag::compound([ + ("material", Tag::String(material.into())), + ("pattern", Tag::String(pattern.into())), + ]) + } Component::Int(v) if *kind == component::RARITY => { let rarity = ["common", "uncommon", "rare", "epic"]; Tag::String(rarity.get(*v as usize).copied().unwrap_or("common").into()) diff --git a/crates/horizon-protocol/examples/item_wire_probe.rs b/crates/horizon-protocol/examples/item_wire_probe.rs index 19760ca1a..097a9f40d 100644 --- a/crates/horizon-protocol/examples/item_wire_probe.rs +++ b/crates/horizon-protocol/examples/item_wire_probe.rs @@ -2,8 +2,8 @@ //! the oracle's stacks (JSON lines with `label` and `hex`), decodes each with //! `item::Slot` and prints, per stack, whether every byte was consumed, the //! patch's component ids (added, then removed), which of them Horizon -//! decoded into its own types (the rest it reads past, keeping their bytes) -//! and whether encoding the decoded stack gives the same bytes back. +//! decoded into its own lossy types (the rest it reads past keeping their +//! bytes, or decodes into ids it writes back unchanged, like trims) and whether encoding the decoded stack gives the same bytes back. //! //! cargo run -p horizon-protocol --example item_wire_probe -- vanilla.jsonl rust.jsonl use horizon_protocol::{ @@ -45,7 +45,7 @@ fn main() -> Result<(), Box> { let typed: Vec = stack .iter() .flat_map(|s| &s.added) - .filter(|(_, c)| !matches!(c, Component::Raw(_))) + .filter(|(_, c)| !matches!(c, Component::Raw(_) | Component::Trim { .. })) .map(|(k, _)| *k) .collect(); json!({ diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 0aa940a38..2872c0228 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -37,6 +37,7 @@ pub mod component { pub const CHARGED_PROJECTILES: i32 = 51; pub const WRITABLE_BOOK_CONTENT: i32 = 56; pub const WRITTEN_BOOK_CONTENT: i32 = 57; + pub const TRIM: i32 = 58; } /// A written book's content (`WrittenBookContent.STREAM_CODEC`): the @@ -80,6 +81,12 @@ pub enum Component { WrittenBook(WrittenBook), /// `ChargedProjectiles`: a loaded crossbow's ammunition, as templates. Stacks(Vec), + /// `ArmorTrim` naming its material and pattern by registry id (`trim_material`, + /// `trim_pattern`); a trim with a direct holder stays [`Component::Raw`]. + Trim { + material: i32, + pattern: i32, + }, /// A component Horizon reads past without interpreting it: its encoded /// bytes, written back as they came. Raw(Vec), @@ -232,13 +239,28 @@ impl Component { other => { let start = r.position(); skip::component(other, r)?; - Self::Raw(r.since(start).to_vec()) + let bytes = r.since(start); + // Two references: each holder is just its id plus one. + let mut ids = Reader::new(bytes); + match (other, ids.varint(), ids.varint()) { + (TRIM, Ok(material @ 1..), Ok(pattern @ 1..)) if ids.remaining() == 0 => { + Self::Trim { + material: material - 1, + pattern: pattern - 1, + } + } + _ => Self::Raw(bytes.to_vec()), + } } }) } fn write(&self, kind: i32, w: &mut Writer) { match self { Self::Raw(bytes) => w.bytes(bytes), + Self::Trim { material, pattern } => { + w.varint(material + 1); + w.varint(pattern + 1); + } Self::Int(v) if kind == component::DYED_COLOR => w.i32(*v), Self::Int(v) => w.varint(*v), Self::Unit => {} @@ -646,7 +668,7 @@ mod tests { #[test] fn components_read_past_keep_their_bytes() { // Vanilla 26.3's encoding (tools/test_item_wire_parity.py) of a chestplate's - // armour trim, a banner's patterns (one direct), a pot's sherds and a stew's + // armour trim (decoded: both holders are references), a banner's patterns (one direct), a pot's sherds and a stew's // effects: one server's inventory, recipe book and advancements had them. for (hex, kind) in [ ("01bd0801003a0502", 58), @@ -661,10 +683,21 @@ mod tests { let mut r = Reader::new(&bytes); let stack = Slot::read(&mut r).unwrap(); assert_eq!(r.remaining(), 0, "component {kind}"); - assert!(matches!( - stack.as_ref().and_then(|s| s.component(kind)), - Some(Component::Raw(_)) - )); + let expected = |c: &Component| match kind { + 58 => { + *c == Component::Trim { + material: 4, + pattern: 1, + } + } + _ => matches!(c, Component::Raw(_)), + }; + assert!( + stack + .as_ref() + .and_then(|s| s.component(kind)) + .is_some_and(expected) + ); let mut w = Writer::new(); Slot::write(&stack, &mut w); assert_eq!(w.data, bytes, "component {kind}"); From f07d66c7cfa9b298b0e36b6035e2d9fe7ffc5eda Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:44:21 +0200 Subject: [PATCH 2/3] Trims: their own arm in Component::read The trim decode sat inside the read-past fallback and matched the kind there; it now has a TRIM arm, and the fallback only reads past. Long comment lines wrapped. Co-Authored-By: Claude Opus 5.5 --- .../examples/item_wire_probe.rs | 3 +- crates/horizon-protocol/src/item.rs | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/horizon-protocol/examples/item_wire_probe.rs b/crates/horizon-protocol/examples/item_wire_probe.rs index 097a9f40d..eb613b520 100644 --- a/crates/horizon-protocol/examples/item_wire_probe.rs +++ b/crates/horizon-protocol/examples/item_wire_probe.rs @@ -3,7 +3,8 @@ //! `item::Slot` and prints, per stack, whether every byte was consumed, the //! patch's component ids (added, then removed), which of them Horizon //! decoded into its own lossy types (the rest it reads past keeping their -//! bytes, or decodes into ids it writes back unchanged, like trims) and whether encoding the decoded stack gives the same bytes back. +//! bytes, or decodes into ids it writes back unchanged, like trims) and +//! whether encoding the decoded stack gives the same bytes back. //! //! cargo run -p horizon-protocol --example item_wire_probe -- vanilla.jsonl rust.jsonl use horizon_protocol::{ diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 2872c0228..6b6b845f8 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -236,21 +236,25 @@ impl Component { hidden: (0..n).map(|_| r.varint()).collect::>()?, } } + // ArmorTrim.STREAM_CODEC: two holders, each a reference's id plus + // one. A direct material or pattern has no registry name: read past. + TRIM => { + let start = r.position(); + skip::component(TRIM, r)?; + let bytes = r.since(start); + let mut ids = Reader::new(bytes); + match (ids.varint(), ids.varint()) { + (Ok(material @ 1..), Ok(pattern @ 1..)) if ids.remaining() == 0 => Self::Trim { + material: material - 1, + pattern: pattern - 1, + }, + _ => Self::Raw(bytes.to_vec()), + } + } other => { let start = r.position(); skip::component(other, r)?; - let bytes = r.since(start); - // Two references: each holder is just its id plus one. - let mut ids = Reader::new(bytes); - match (other, ids.varint(), ids.varint()) { - (TRIM, Ok(material @ 1..), Ok(pattern @ 1..)) if ids.remaining() == 0 => { - Self::Trim { - material: material - 1, - pattern: pattern - 1, - } - } - _ => Self::Raw(bytes.to_vec()), - } + Self::Raw(r.since(start).to_vec()) } }) } @@ -668,8 +672,9 @@ mod tests { #[test] fn components_read_past_keep_their_bytes() { // Vanilla 26.3's encoding (tools/test_item_wire_parity.py) of a chestplate's - // armour trim (decoded: both holders are references), a banner's patterns (one direct), a pot's sherds and a stew's - // effects: one server's inventory, recipe book and advancements had them. + // armour trim (decoded: both holders are references), a banner's patterns + // (one direct), a pot's sherds and a stew's effects: one server's + // inventory, recipe book and advancements had them. for (hex, kind) in [ ("01bd0801003a0502", 58), ("01890b01004a02200e000b6d696e6563726166743a7801790b", 74), From a26297058a38c8817f07fc5ba6a7107cbc16f450 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:15:25 +0200 Subject: [PATCH 3/3] Trims: a Horizon host sends them too The host wrote no trim component, so single player on its server thread, LAN guests and dedicated-server players saw armour without its trim. network_stack now takes the connection's synced-registry lookup for any registry and writes Component::Trim when both names are in it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lan/guest.rs | 59 ++++++++++++++++++++++++-- crates/horizon-client/src/lan/hub.rs | 2 +- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 256f3a5cf..440b58284 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -432,18 +432,21 @@ impl Guest { } /// The off hand slot is conventionally 40 in saves and 45 in the menu. +/// `synced_id` names an entry of a synced registry (enchantments, trim +/// materials and patterns) by its id on this connection. pub fn stack_to_network( item_id: impl Fn(&str) -> i32, - enchantment_id: impl Fn(&str) -> Option, + synced_id: impl Fn(&str, &str) -> Option, s: &Option, ) -> Option { - network_stack(&item_id, &enchantment_id, s) + network_stack(&item_id, &synced_id, s) } fn network_stack( item_id: &dyn Fn(&str) -> i32, - enchantment_id: &dyn Fn(&str) -> Option, + synced_id: &dyn Fn(&str, &str) -> Option, s: &Option, ) -> Option { + let enchantment_id = |name: &str| synced_id("minecraft:enchantment", name); let s = s.as_ref()?; let mut stack = Stack::new(item_id(&s.item), s.count as i32); if s.damage > 0 { @@ -481,6 +484,25 @@ fn network_stack( horizon_protocol::item::Component::Int(rgb), )); } + // ArmorTrim: both holders as references into the synced registries. + if let Some((material, pattern)) = &d.trim { + let id = |registry: &str, name: &str| { + if name.contains(':') { + synced_id(registry, name) + } else { + synced_id(registry, &format!("minecraft:{name}")) + } + }; + if let (Some(material), Some(pattern)) = ( + id("minecraft:trim_material", material), + id("minecraft:trim_pattern", pattern), + ) { + stack.added.push(( + horizon_protocol::item::component::TRIM, + horizon_protocol::item::Component::Trim { material, pattern }, + )); + } + } if let Some(id) = d.map_id { stack.added.push(( horizon_protocol::item::component::MAP_ID, @@ -504,7 +526,7 @@ fn network_stack( let charged = d .charged .iter() - .filter_map(|c| network_stack(item_id, enchantment_id, &Some(c.clone()))) + .filter_map(|c| network_stack(item_id, synced_id, &Some(c.clone()))) .collect(); stack.added.push(( horizon_protocol::item::component::CHARGED_PROJECTILES, @@ -545,3 +567,32 @@ fn network_stack( } Some(stack) } + +#[cfg(test)] +mod tests { + use super::*; + use horizon_protocol::item::{Component, component}; + + #[test] + fn trims_go_as_synced_registry_references() { + let mut chestplate = ItemStack::new("minecraft:diamond_chestplate", 1); + chestplate.data_mut().trim = Some(("minecraft:gold".into(), "coast".into())); + let synced_id = |registry: &str, name: &str| match (registry, name) { + ("minecraft:trim_material", "minecraft:gold") => Some(4), + ("minecraft:trim_pattern", "minecraft:coast") => Some(1), + _ => None, + }; + let stack = stack_to_network(|_| 7, synced_id, &Some(chestplate.clone())).unwrap(); + assert_eq!( + stack.component(component::TRIM), + Some(&Component::Trim { + material: 4, + pattern: 1 + }) + ); + // A name the connection's registry lacks sends no trim. + chestplate.data_mut().trim = Some(("minecraft:x".into(), "coast".into())); + let stack = stack_to_network(|_| 7, synced_id, &Some(chestplate)).unwrap(); + assert_eq!(stack.component(component::TRIM), None); + } +} diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 4f93b6ff9..c7c1512a7 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -351,7 +351,7 @@ impl Hub { pub fn stack(&mut self, s: &Option) -> Option { let id = s.as_ref().map(|s| self.item_id(&s.item))?; let sync = self.sync.clone(); - stack_to_network(|_| id, |name| sync.id("minecraft:enchantment", name), s) + stack_to_network(|_| id, |registry, name| sync.id(registry, name), s) } pub fn broadcast(&self, packet: &C) {