Creative: a stack keeps its components when it crosses the wire
`Game::to_network` wrote damage alone into SetCreativeModeSlot, and `item_from_network` read back damage and enchantments, so anything else a creative stack carried was gone once the server echoed the inventory: a named sword lost its name, trimmed armour its trim, an enchanted book its stored enchantments and its repair cost. `item_sync` is the pair for the components Horizon models: `encode` writes them with the synced registries' ids (an unknown name is left out rather than sent as id 0, which is a different entry) and `apply` reads them back into the same `StackData` fields `item_data::decode_components` fills. Potions are not carried yet: `minecraft:potion_contents` has no id or codec in horizon-protocol, so a creative potion still arrives as an uncraftable one (#1832). Nor are containers, charged projectiles, written books and a map id, which the packet models but this patch does not write. Refs #1832 Co-Authored-By: Hermes Agent (Nous Research) <noreply@nousresearch.com>
This commit is contained in:
@@ -1216,21 +1216,22 @@ impl Game<'_> {
|
||||
host: &LanHost,
|
||||
stack: &Option<horizon_protocol::item::Stack>,
|
||||
) -> Option<ItemStack> {
|
||||
use horizon_protocol::item::{Component, component};
|
||||
let s = stack.as_ref()?;
|
||||
let name = host
|
||||
.hub
|
||||
.item_name(s.item)
|
||||
.unwrap_or_else(|| "minecraft:air".into());
|
||||
let mut out = ItemStack::new(&name, s.count.max(1) as u32);
|
||||
out.damage = s.damage().max(0) as u32;
|
||||
if let Some(Component::Enchantments(list)) = s.component(component::ENCHANTMENTS) {
|
||||
for (id, level) in list {
|
||||
if let Some(e) = host.hub.enchantment_name(*id) {
|
||||
out.enchant(&e, *level as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
// The same components the encoder wrote, so a creative slot keeps what
|
||||
// the client held instead of just damage and enchantments.
|
||||
horizon_client::item_sync::apply(
|
||||
s,
|
||||
&mut out,
|
||||
|id| host.hub.enchantment_name(id).map(|n| n.into()),
|
||||
|id| host.hub.trim_name("minecraft:trim_material", id),
|
||||
|id| host.hub.trim_name("minecraft:trim_pattern", id),
|
||||
);
|
||||
out.tidy();
|
||||
Some(out)
|
||||
}
|
||||
|
||||
|
||||
@@ -761,10 +761,14 @@ 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);
|
||||
if s.damage > 0 {
|
||||
stack.added.push((
|
||||
horizon_protocol::item::component::DAMAGE,
|
||||
horizon_protocol::item::Component::Int(s.damage as i32),
|
||||
// The components the client models, with the synced registries' ids:
|
||||
// without them the server's echo hands back a bare item.
|
||||
if let Some(r) = self.remote.as_ref() {
|
||||
stack.added.extend(horizon_client::item_sync::encode(
|
||||
s,
|
||||
|name| r.registries.id("minecraft:enchantment", name),
|
||||
|name| r.registries.id("minecraft:trim_material", name),
|
||||
|name| r.registries.id("minecraft:trim_pattern", name),
|
||||
));
|
||||
}
|
||||
Some(stack)
|
||||
|
||||
@@ -291,7 +291,7 @@ fn filterable_text(t: &Tag) -> Option<String> {
|
||||
None => read_text(t),
|
||||
}
|
||||
}
|
||||
fn read_text(t: &Tag) -> Option<String> {
|
||||
pub(crate) fn read_text(t: &Tag) -> Option<String> {
|
||||
match t {
|
||||
Tag::String(s) => Some(s.to_string_lossy()),
|
||||
// A component's root `text` may be empty with the visible words in
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
//! The data components a stack keeps when it crosses the wire, and how the
|
||||
//! other side reads them back. A creative slot's packet carries a component
|
||||
//! patch, and before this the encoder wrote damage alone, so everything else
|
||||
//! (enchantments, a custom name, trim, repair cost) was gone once the server
|
||||
//! echoed the inventory back.
|
||||
use crate::item_data::{Levels, StackData};
|
||||
use crate::items::ItemStack;
|
||||
use horizon_protocol::item::{Component, Stack, component};
|
||||
use horizon_world::nbt::Tag;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// `Slot.STREAM_CODEC`'s patch for the components Horizon models, with the
|
||||
/// registries' ids for enchantments and trim. A name the registry does not
|
||||
/// know is left out: id 0 is a different entry, not a missing one.
|
||||
pub fn encode(
|
||||
stack: &ItemStack,
|
||||
enchantment: impl Fn(&str) -> Option<i32>,
|
||||
trim_material: impl Fn(&str) -> Option<i32>,
|
||||
trim_pattern: impl Fn(&str) -> Option<i32>,
|
||||
) -> Vec<(i32, Component)> {
|
||||
let mut out = Vec::new();
|
||||
if stack.damage > 0 {
|
||||
out.push((component::DAMAGE, Component::Int(stack.damage as i32)));
|
||||
}
|
||||
let Some(d) = stack.data() else {
|
||||
return out;
|
||||
};
|
||||
let levels = |levels: &Levels| -> Option<Component> {
|
||||
let list: Vec<(i32, i32)> = levels
|
||||
.iter()
|
||||
.filter_map(|(name, level)| Some((enchantment(name)?, *level as i32)))
|
||||
.collect();
|
||||
(!list.is_empty()).then_some(Component::Enchantments(list))
|
||||
};
|
||||
if let Some(c) = levels(&d.enchantments) {
|
||||
out.push((component::ENCHANTMENTS, c));
|
||||
}
|
||||
if let Some(c) = levels(&d.stored_enchantments) {
|
||||
out.push((component::STORED_ENCHANTMENTS, c));
|
||||
}
|
||||
if d.repair_cost > 0 {
|
||||
out.push((component::REPAIR_COST, Component::Int(d.repair_cost as i32)));
|
||||
}
|
||||
if let Some(name) = &d.custom_name {
|
||||
// A plain string is a valid text component, and `decode_components`
|
||||
// reads it back as one.
|
||||
out.push((
|
||||
component::CUSTOM_NAME,
|
||||
Component::Text(Tag::String(name.as_str().into())),
|
||||
));
|
||||
}
|
||||
if let Some((material, pattern)) = &d.trim
|
||||
&& let (Some(material), Some(pattern)) = (trim_material(material), trim_pattern(pattern))
|
||||
{
|
||||
out.push((component::TRIM, Component::Trim { material, pattern }));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The same patch read back into `out`, the fields `item_data::decode_components`
|
||||
/// fills from the equivalent `components` compound.
|
||||
pub fn apply(
|
||||
stack: &Stack,
|
||||
out: &mut ItemStack,
|
||||
enchantment: impl Fn(i32) -> Option<Arc<str>>,
|
||||
trim_material: impl Fn(i32) -> Option<Arc<str>>,
|
||||
trim_pattern: impl Fn(i32) -> Option<Arc<str>>,
|
||||
) {
|
||||
out.damage = stack.damage().max(0) as u32;
|
||||
let read_levels = |kind: i32, into: &mut Levels| {
|
||||
if let Some(Component::Enchantments(list)) = stack.component(kind) {
|
||||
for (id, level) in list {
|
||||
if let Some(name) = enchantment(*id) {
|
||||
into.push((name, (*level).max(0) as u32));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let data: &mut StackData = out.data_mut();
|
||||
read_levels(component::ENCHANTMENTS, &mut data.enchantments);
|
||||
read_levels(
|
||||
component::STORED_ENCHANTMENTS,
|
||||
&mut data.stored_enchantments,
|
||||
);
|
||||
if let Some(Component::Int(v)) = stack.component(component::REPAIR_COST) {
|
||||
data.repair_cost = (*v).max(0) as u32;
|
||||
}
|
||||
if let Some(Component::Text(t)) = stack.component(component::CUSTOM_NAME) {
|
||||
data.custom_name = crate::item_data::read_text(t);
|
||||
}
|
||||
if let Some(Component::Trim { material, pattern }) = stack.component(component::TRIM)
|
||||
&& let (Some(material), Some(pattern)) = (trim_material(*material), trim_pattern(*pattern))
|
||||
{
|
||||
data.trim = Some((material, pattern));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn named(name: &str) -> Option<i32> {
|
||||
Some(match name {
|
||||
"minecraft:sharpness" => 3,
|
||||
"minecraft:gold" => 7,
|
||||
"minecraft:sentry" => 2,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
fn name_of(id: i32) -> Option<Arc<str>> {
|
||||
Some(Arc::from(match id {
|
||||
3 => "minecraft:sharpness",
|
||||
7 => "minecraft:gold",
|
||||
2 => "minecraft:sentry",
|
||||
_ => return None,
|
||||
}))
|
||||
}
|
||||
|
||||
fn stack_with_data() -> ItemStack {
|
||||
let mut stack = ItemStack::new("minecraft:diamond_sword", 1);
|
||||
stack.damage = 5;
|
||||
let d = stack.data_mut();
|
||||
d.enchantments.push((Arc::from("minecraft:sharpness"), 4));
|
||||
d.stored_enchantments
|
||||
.push((Arc::from("minecraft:sharpness"), 2));
|
||||
d.repair_cost = 9;
|
||||
d.custom_name = Some("Keeper".to_owned());
|
||||
d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry")));
|
||||
stack
|
||||
}
|
||||
|
||||
/// Every modelled component survives the packet, so the server's echo does
|
||||
/// not hand back a bare item.
|
||||
#[test]
|
||||
fn a_stack_keeps_its_components_across_the_wire() {
|
||||
let stack = stack_with_data();
|
||||
let mut packet = Stack::new(1, 1);
|
||||
packet.added = encode(&stack, named, named, named);
|
||||
|
||||
let mut back = ItemStack::new("minecraft:diamond_sword", 1);
|
||||
apply(&packet, &mut back, name_of, name_of, name_of);
|
||||
back.tidy();
|
||||
|
||||
assert_eq!(back.damage, 5);
|
||||
let d = back.data().expect("components");
|
||||
assert_eq!(d.enchantments, vec![(Arc::from("minecraft:sharpness"), 4)]);
|
||||
assert_eq!(
|
||||
d.stored_enchantments,
|
||||
vec![(Arc::from("minecraft:sharpness"), 2)]
|
||||
);
|
||||
assert_eq!(d.repair_cost, 9);
|
||||
assert_eq!(d.custom_name.as_deref(), Some("Keeper"));
|
||||
assert_eq!(
|
||||
d.trim,
|
||||
Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry")))
|
||||
);
|
||||
}
|
||||
|
||||
/// A name the registry does not know is a missing id, not id 0, which would
|
||||
/// name the registry's first entry instead.
|
||||
#[test]
|
||||
fn an_unknown_enchantment_is_left_out_rather_than_sent_as_zero() {
|
||||
let mut stack = ItemStack::new("minecraft:book", 1);
|
||||
stack
|
||||
.data_mut()
|
||||
.enchantments
|
||||
.push((Arc::from("mod:unknown"), 1));
|
||||
assert!(encode(&stack, named, named, named).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -2293,6 +2293,14 @@ impl Hub {
|
||||
.name("minecraft:enchantment", id)
|
||||
.map(str::to_owned)
|
||||
}
|
||||
/// An entry of a synced registry by id, for the components a packet
|
||||
/// carries by id (`minecraft:trim_material`, `minecraft:trim_pattern`).
|
||||
pub fn trim_name(&self, registry: &str, id: i32) -> Option<std::sync::Arc<str>> {
|
||||
self.sync
|
||||
.dynamic
|
||||
.name(registry, id)
|
||||
.map(std::sync::Arc::from)
|
||||
}
|
||||
|
||||
/// ServerLevel.removePlayerImmediately, then addRespawnedPlayer: the other players
|
||||
/// lose the dead player's entity (ServerEntity.removePairing) and are sent the
|
||||
|
||||
@@ -96,6 +96,7 @@ pub mod inv_sort;
|
||||
pub mod inventory_effects;
|
||||
pub mod item_data;
|
||||
pub mod item_render;
|
||||
pub mod item_sync;
|
||||
pub mod item_tooltip;
|
||||
pub mod items;
|
||||
pub mod java_text;
|
||||
|
||||
@@ -102,6 +102,12 @@ impl Dynamic {
|
||||
.get(i)
|
||||
.map(|(n, _)| n.as_str())
|
||||
}
|
||||
/// The id of an entry by name, the reverse of `name`. A registry has one
|
||||
/// entry per id, so the first match is the only one.
|
||||
pub fn id(&self, registry: &str, name: &str) -> Option<i32> {
|
||||
let list = self.registries.get(registry)?;
|
||||
list.iter().position(|(n, _)| n == name).map(|i| i as i32)
|
||||
}
|
||||
pub fn data(&self, registry: &str, id: i32) -> Option<&Tag> {
|
||||
let i = usize::try_from(id).ok()?;
|
||||
self.registries.get(registry)?.get(i)?.1.as_ref()
|
||||
|
||||
Reference in New Issue
Block a user