From 9e21868dfa012b3a46d3242d03e56845fb6fb5c5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 13:46:45 +0100 Subject: [PATCH] Potions keep their potion across the connection A server's client (single player on the server thread, LAN guests, a dedicated server's players) saw every potion as an "Uncraftable Potion" in the water colour: the server never wrote `minecraft:potion_contents` and the client read past it. A potion taken from the creative menu also reached the server without its potion, so it splashed as water. PotionContents now goes as vanilla's stream codec writes it: the potion by its `minecraft:potion` id and the custom colour, with the custom effects and name kept as their bytes. The server writes it for every stack it sends, the client reads it into the item's components, and a creative pick and the server's read of it keep the potion too. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan_actions.rs | 7 ++ crates/horizon-client/src/game/remote.rs | 24 +++++ crates/horizon-client/src/lan/guest.rs | 34 +++++++ crates/horizon-protocol/src/item.rs | 90 +++++++++++++++++++ crates/horizon-protocol/src/item/skip.rs | 8 +- 5 files changed, 158 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..4964bc687 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1231,6 +1231,13 @@ impl Game<'_> { } } } + if let Some(Component::Potion { + potion: Some(id), .. + }) = s.component(component::POTION_CONTENTS) + && let Some(potion) = host.hub.registry_name("minecraft:potion", *id) + { + out = out.with_potion(potion); + } Some(out) } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 5a71b13a8..c723400a8 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -672,6 +672,16 @@ impl Game<'_> { ("mob_factor", Tag::Float(v[5].to_bits())), ]), Component::Profile(p) => profile_tag(p), + // PotionContents' NBT: the potion by name and the custom colour. + Component::Potion { potion, color, .. } => { + let potion = potion.and_then(|id| r.builtin.get("minecraft:potion")?.name(id)); + Tag::compound( + potion + .map(|name| ("potion", Tag::String(name.into()))) + .into_iter() + .chain(color.map(|c| ("custom_color", Tag::Int(c)))), + ) + } // ItemStackTemplate.CODEC: id, count and components. Component::Stacks(stacks) => Tag::List( stacks @@ -733,6 +743,20 @@ impl Game<'_> { fn to_network(&self, s: &Option) -> Option { let s = s.as_ref()?; let mut stack = Stack::new(self.item_id(&s.item), s.count as i32); + // A creative pick keeps its potion (PotionContents by its builtin id). + if let Some(id) = s.potion().and_then(|p| { + let potions = self.remote.as_ref()?.builtin.get("minecraft:potion")?; + potions.id(&horizon_world::datafix::dynamic::namespaced(p)) + }) { + stack.added.push(( + horizon_protocol::item::component::POTION_CONTENTS, + horizon_protocol::item::Component::Potion { + potion: Some(id), + color: None, + rest: Vec::new(), + }, + )); + } if s.damage > 0 { stack.added.push(( horizon_protocol::item::component::DAMAGE, diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 440b58284..bbab454d7 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -521,6 +521,22 @@ fn network_stack( horizon_protocol::item::Component::Int(i32::from(mode)), )); } + // PotionContents: the potion by its `minecraft:potion` id. + if let Some(id) = (d.potion.as_deref()).and_then(|potion| { + synced_id( + "minecraft:potion", + &horizon_world::datafix::dynamic::namespaced(potion), + ) + }) { + stack.added.push(( + horizon_protocol::item::component::POTION_CONTENTS, + horizon_protocol::item::Component::Potion { + potion: Some(id), + color: None, + rest: Vec::new(), + }, + )); + } // A loaded crossbow's ammunition. if !d.charged.is_empty() { let charged = d @@ -573,6 +589,24 @@ mod tests { use super::*; use horizon_protocol::item::{Component, component}; + #[test] + fn potions_go_as_potion_registry_ids() { + let synced_id = |registry: &str, name: &str| match (registry, name) { + ("minecraft:potion", "minecraft:swiftness") => Some(14), + _ => None, + }; + let potion = ItemStack::new("minecraft:splash_potion", 1).with_potion("swiftness"); + let stack = stack_to_network(|_| 7, synced_id, &Some(potion)).unwrap(); + assert_eq!( + stack.component(component::POTION_CONTENTS), + Some(&Component::Potion { + potion: Some(14), + color: None, + rest: Vec::new(), + }) + ); + } + #[test] fn trims_go_as_synced_registry_references() { let mut chestplate = ItemStack::new("minecraft:diamond_chestplate", 1); diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 6b6b845f8..2c04b5c61 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -35,6 +35,7 @@ pub mod component { pub const MAP_ID: i32 = 48; pub const MAP_POST_PROCESSING: i32 = 50; pub const CHARGED_PROJECTILES: i32 = 51; + pub const POTION_CONTENTS: i32 = 53; pub const WRITABLE_BOOK_CONTENT: i32 = 56; pub const WRITTEN_BOOK_CONTENT: i32 = 57; pub const TRIM: i32 = 58; @@ -65,6 +66,13 @@ pub enum Component { Lore(Vec), Enchantments(Vec<(i32, i32)>), AttackRange([f32; 6]), + /// `PotionContents`: the potion's `minecraft:potion` id and the custom + /// colour; the custom effects and name stay their encoded bytes. + Potion { + potion: Option, + color: Option, + rest: Vec, + }, Profile(Profile), CustomModelData { floats: Vec, @@ -162,6 +170,22 @@ impl Component { .collect::>()?, ) } + POTION_CONTENTS => { + let potion = if r.bool()? { Some(r.varint()?) } else { None }; + let color = if r.bool()? { Some(r.i32()?) } else { None }; + let start = r.position(); + skip::potion_effects_and_name(r)?; + // No custom effects and no name: an empty tail. + let rest = match r.since(start) { + [0, 0] => Vec::new(), + bytes => bytes.to_vec(), + }; + Self::Potion { + potion, + color, + rest, + } + } ATTACK_RANGE => { let mut values = [0.0f32; 6]; for v in &mut values { @@ -283,6 +307,27 @@ impl Component { } } Self::AttackRange(values) => values.iter().for_each(|v| w.f32(*v)), + Self::Potion { + potion, + color, + rest, + } => { + w.bool(potion.is_some()); + if let Some(id) = potion { + w.varint(*id); + } + w.bool(color.is_some()); + if let Some(color) = color { + w.i32(*color); + } + // An empty tail is no custom effects and no name. + if rest.is_empty() { + w.varint(0); + w.bool(false); + } else { + w.bytes(rest); + } + } Self::Profile(p) => { w.u8(0); w.bool(p.name.is_some()); @@ -532,6 +577,51 @@ mod tests { assert_eq!(r.remaining(), 0); } + #[test] + fn potion_contents_round_trip() { + let mut arrow = Stack::new(7, 3); + arrow.added.push(( + component::POTION_CONTENTS, + Component::Potion { + potion: Some(31), + color: None, + rest: Vec::new(), + }, + )); + round::, Slot>(Some(arrow)); + // A custom colour, one custom effect (speed II for 600 ticks, shown, + // no hidden effect) and a custom name survive as they came. + let mut w = Writer::new(); + w.bool(false); + w.bool(true); + w.i32(0x00FF_8800); + w.varint(1); + for v in [1, 1, 600] { + w.varint(v); + } + for b in [false, true, true, false] { + w.bool(b); + } + w.bool(true); + w.string("Tea"); + let mut r = Reader::new(&w.data); + let read = Component::read(component::POTION_CONTENTS, &mut r).unwrap(); + assert_eq!(r.remaining(), 0); + let Component::Potion { + potion, + color, + rest, + } = &read + else { + panic!("{read:?}"); + }; + assert_eq!((*potion, *color), (None, Some(0x00FF_8800))); + assert!(!rest.is_empty()); + let mut again = Writer::new(); + read.write(component::POTION_CONTENTS, &mut again); + assert_eq!(again.data, w.data); + } + #[test] fn stacks_round_trip() { round::, Slot>(None); diff --git a/crates/horizon-protocol/src/item/skip.rs b/crates/horizon-protocol/src/item/skip.rs index 152d0f0f1..aa436cc31 100644 --- a/crates/horizon-protocol/src/item/skip.rs +++ b/crates/horizon-protocol/src/item/skip.rs @@ -87,7 +87,6 @@ pub(super) fn component(kind: i32, r: &mut Reader) -> Result<()> { } // BundleContents: templates. 52 => list(r, template), - 53 => potion_contents(r), // SuspiciousStewEffects: effect, duration. 55 => list(r, |r| { varint(r)?; @@ -410,10 +409,9 @@ fn kinetic_weapon(r: &mut Reader) -> Result<()> { optional(r, sound)?; optional(r, sound) } -/// PotionContents: potion, customColor, customEffects, customName. -fn potion_contents(r: &mut Reader) -> Result<()> { - optional(r, varint)?; - optional(r, i32)?; +/// PotionContents' customEffects and customName (`Component::read` reads +/// the potion and colour before them). +pub(super) fn potion_effects_and_name(r: &mut Reader) -> Result<()> { list(r, mob_effect)?; optional(r, string) }