Merge pull request #1633 from some-du6e/fix/remote-trims

This commit is contained in:
Ludovic Marie
2026-10-25 08:23:48 +01:00
committed by GitHub
5 changed files with 117 additions and 14 deletions
+13
View File
@@ -614,6 +614,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())
+55 -4
View File
@@ -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<i32>,
synced_id: impl Fn(&str, &str) -> Option<i32>,
s: &Option<ItemStack>,
) -> Option<Stack> {
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<i32>,
synced_id: &dyn Fn(&str, &str) -> Option<i32>,
s: &Option<ItemStack>,
) -> Option<Stack> {
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);
}
}
+1 -1
View File
@@ -351,7 +351,7 @@ impl Hub {
pub fn stack(&mut self, s: &Option<ItemStack>) -> Option<horizon_protocol::item::Stack> {
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) {
@@ -2,8 +2,9 @@
//! 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 +46,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!({
+44 -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>),
@@ -229,6 +236,21 @@ impl Component {
hidden: (0..n).map(|_| r.varint()).collect::<Result<_>>()?,
}
}
// 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)?;
@@ -239,6 +261,10 @@ impl Component {
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,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, 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),
@@ -661,10 +688,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}");