Merge pull request #1796 from ludovic111/fix/potion-contents-over-network
🤖 Agent: merged after verify and the parity harnesses this change reaches passed on its head.
This commit is contained in:
@@ -1276,6 +1276,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)
|
||||
}
|
||||
|
||||
|
||||
@@ -700,6 +700,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
|
||||
@@ -761,6 +771,20 @@ impl Game<'_> {
|
||||
fn to_network(&self, s: &Option<ItemStack>) -> Option<Stack> {
|
||||
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,
|
||||
|
||||
@@ -532,6 +532,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
|
||||
@@ -596,6 +612,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);
|
||||
|
||||
@@ -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<Tag>),
|
||||
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<i32>,
|
||||
color: Option<i32>,
|
||||
rest: Vec<u8>,
|
||||
},
|
||||
Profile(Profile),
|
||||
CustomModelData {
|
||||
floats: Vec<f32>,
|
||||
@@ -162,6 +170,22 @@ impl Component {
|
||||
.collect::<Result<_>>()?,
|
||||
)
|
||||
}
|
||||
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::<Option<Stack>, 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::<Option<Stack>, Slot>(None);
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user