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 <noreply@anthropic.com>
This commit is contained in:
some-du6e
2026-10-24 14:44:17 -06:00
parent 9223b401e7
commit 4ad05aa94a
3 changed files with 55 additions and 9 deletions
+13
View File
@@ -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())
@@ -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<dyn std::error::Error>> {
let typed: Vec<i32> = 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!({
+39 -6
View File
@@ -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<Stack>),
/// `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<u8>),
@@ -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}");