fix(client): read server map art and item profiles
A server lobby draws its signs as walls of framed maps and puts player heads on its NPCs. Horizon dropped every map_item_data packet, so framed maps stayed blank, and the item component decoder rejected attack_range (30) and profile (72), which made the whole equipment and container packets that carried them undecodable. Decode both components, carry a profile into the item data the head model reads, and apply server map patches to the map texture. The map pool now holds a wall of maps so the atlas does not re-upload every map every frame. Co-Authored-By: cairn-code (DeepSeek V4.1 Flash) <cairn-code@users.noreply.github.com>
This commit is contained in:
@@ -275,6 +275,37 @@ impl Game<'_> {
|
||||
}
|
||||
self.maps.data.get_mut(&id)
|
||||
}
|
||||
/// `ClientboundMapItemDataPacket.handle`: the server's colours for a map
|
||||
/// this client does not own (lobby map art, a framed map on a wall).
|
||||
pub(super) fn remote_map_item_data(
|
||||
&mut self,
|
||||
p: &horizon_protocol::packets::play::MapItemData,
|
||||
) {
|
||||
let dimension = self.dimension_id();
|
||||
let map = self
|
||||
.maps
|
||||
.data
|
||||
.entry(p.map_id)
|
||||
.or_insert_with(|| MapData::fresh(0.0, 0.0, p.scale.max(0) as u8, dimension));
|
||||
map.locked = p.locked;
|
||||
let Some(patch) = &p.patch else {
|
||||
map.dirty = false;
|
||||
return;
|
||||
};
|
||||
for x in 0..patch.width {
|
||||
for y in 0..patch.height {
|
||||
let Some(&color) = patch.colors.get((x + y * patch.width) as usize) else {
|
||||
continue;
|
||||
};
|
||||
let (px, py) = (patch.start_x + x, patch.start_y + y);
|
||||
if (0..128).contains(&px) && (0..128).contains(&py) {
|
||||
map.update_color(px, py, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
// The server owns a remote map: only the texture has to change.
|
||||
map.dirty = false;
|
||||
}
|
||||
fn next_map_id(&mut self) -> i32 {
|
||||
let last = *self.maps.last_id.get_or_insert_with(|| {
|
||||
horizon_client::save::read_dat(&self.world_root.join("data/minecraft/maps/last_id.dat"))
|
||||
|
||||
@@ -196,6 +196,30 @@ fn face_direction(face: [i32; 3]) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
/// A `minecraft:profile` component as the item data JSON the model reads.
|
||||
fn profile_tag(p: &horizon_protocol::item::Profile) -> Tag {
|
||||
let properties = p
|
||||
.properties
|
||||
.iter()
|
||||
.map(|(name, value, signature)| {
|
||||
let mut entries: Vec<(&'static str, Tag)> = vec![
|
||||
("name", Tag::String(name.as_str().into())),
|
||||
("value", Tag::String(value.as_str().into())),
|
||||
];
|
||||
if let Some(signature) = signature {
|
||||
entries.push(("signature", Tag::String(signature.as_str().into())));
|
||||
}
|
||||
Tag::compound(entries)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let mut entries: Vec<(&'static str, Tag)> = Vec::new();
|
||||
if let Some(name) = &p.name {
|
||||
entries.push(("name", Tag::String(name.as_str().into())));
|
||||
}
|
||||
entries.push(("properties", Tag::List(properties)));
|
||||
Tag::compound(entries)
|
||||
}
|
||||
|
||||
/// A protocol number format as the scoreboard's.
|
||||
fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFormat {
|
||||
use horizon_client::scoreboard::{NumberFormat, tag_to_json};
|
||||
@@ -576,6 +600,11 @@ impl Game<'_> {
|
||||
out.data_mut().stored_enchantments = levels(list);
|
||||
}
|
||||
out.tidy();
|
||||
if let Some(Component::Profile(p)) = s.component(component::PROFILE) {
|
||||
out.data_mut()
|
||||
.other
|
||||
.push(("minecraft:profile".into(), profile_tag(p)));
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
fn to_network(&self, s: &Option<ItemStack>) -> Option<Stack> {
|
||||
@@ -2317,6 +2346,7 @@ impl Game<'_> {
|
||||
.then(|| Vec3::new(f64::from(x) + 0.5, f64::from(y), f64::from(z) + 0.5));
|
||||
}
|
||||
C::ServerData(p) => self.remote_server_data(p.icon.as_deref()),
|
||||
C::MapItemData(p) => self.remote_map_item_data(&p),
|
||||
// handleTickingState / handleTickingStep: the level's TickRateManager.
|
||||
C::TickingState(p) => {
|
||||
self.clock.set_rate(p.rate);
|
||||
|
||||
@@ -30,8 +30,10 @@ pub struct HudAtlas {
|
||||
}
|
||||
/// Atlas rows reserved for glyphs rasterized at run time.
|
||||
pub const GLYPH_ROWS: u32 = 512;
|
||||
/// 128x128 atlas slots for map textures.
|
||||
pub const MAP_SLOTS: usize = 16;
|
||||
/// 128x128 atlas slots for map textures. A lobby paints its signs as walls of
|
||||
/// framed maps, so the pool holds a wall; too few slots re-upload every map
|
||||
/// every frame. 26.3's `MapTextureManager` keeps a texture per map instead.
|
||||
pub const MAP_SLOTS: usize = 256;
|
||||
/// The atlas slot the minimap extra draws into.
|
||||
pub const MINIMAP_SPRITE: &str = "horizon/minimap";
|
||||
/// Atlas slots for downloaded player skins (`skins/<sha1>`), 64x64 each.
|
||||
|
||||
@@ -25,7 +25,10 @@ pub mod component {
|
||||
pub const REPAIR_COST: i32 = 19;
|
||||
pub const CREATIVE_SLOT_LOCK: i32 = 20;
|
||||
pub const ENCHANTMENT_GLINT_OVERRIDE: i32 = 21;
|
||||
/// `AttackRange`: six floats, in the record's field order.
|
||||
pub const ATTACK_RANGE: i32 = 30;
|
||||
pub const STORED_ENCHANTMENTS: i32 = 45;
|
||||
pub const PROFILE: i32 = 72;
|
||||
pub const DYED_COLOR: i32 = 47;
|
||||
pub const MAP_ID: i32 = 48;
|
||||
pub const MAP_POST_PROCESSING: i32 = 50;
|
||||
@@ -57,6 +60,8 @@ pub enum Component {
|
||||
Ident(String),
|
||||
Lore(Vec<Tag>),
|
||||
Enchantments(Vec<(i32, i32)>),
|
||||
AttackRange([f32; 6]),
|
||||
Profile(Profile),
|
||||
CustomModelData {
|
||||
floats: Vec<f32>,
|
||||
flags: Vec<bool>,
|
||||
@@ -81,6 +86,15 @@ fn filterable<T>(r: &mut Reader, value: impl Fn(&mut Reader) -> Result<T>) -> Re
|
||||
}
|
||||
Ok(raw)
|
||||
}
|
||||
|
||||
/// `ResolvableProfile`: an optional name and UUID and the profile's property
|
||||
/// list, followed by `PlayerSkin.Patch`'s four optional textures.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Profile {
|
||||
pub name: Option<String>,
|
||||
pub id: Option<u128>,
|
||||
pub properties: Vec<(String, String, Option<String>)>,
|
||||
}
|
||||
impl Component {
|
||||
fn read(kind: i32, r: &mut Reader) -> Result<Self> {
|
||||
use component::*;
|
||||
@@ -129,6 +143,56 @@ impl Component {
|
||||
.collect::<Result<_>>()?,
|
||||
)
|
||||
}
|
||||
ATTACK_RANGE => {
|
||||
let mut values = [0.0f32; 6];
|
||||
for v in &mut values {
|
||||
*v = r.f32()?;
|
||||
}
|
||||
Self::AttackRange(values)
|
||||
}
|
||||
PROFILE => {
|
||||
fn properties(r: &mut Reader) -> Result<Vec<(String, String, Option<String>)>> {
|
||||
let n = r.count(1024)?;
|
||||
let mut out = Vec::with_capacity(n);
|
||||
for _ in 0..n {
|
||||
let name = r.string(32767)?;
|
||||
let value = r.string(32767)?;
|
||||
let signature = if r.bool()? {
|
||||
Some(r.string(32767)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
out.push((name, value, signature));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
// Either<GameProfile, Partial>: the partial form carries
|
||||
// optional fields, the resolved one a UUID, a name and the
|
||||
// properties.
|
||||
let (name, id, properties) = if r.u8()? == 0 {
|
||||
let name = if r.bool()? { Some(r.string(64)?) } else { None };
|
||||
let id = if r.bool()? { Some(r.uuid()?) } else { None };
|
||||
(name, id, properties(r)?)
|
||||
} else {
|
||||
let id = Some(r.uuid()?);
|
||||
let name = Some(r.string(64)?);
|
||||
(name, id, properties(r)?)
|
||||
};
|
||||
// PlayerSkin.Patch: body, cape, elytra and model, each absent
|
||||
// in the profiles servers send for heads.
|
||||
for _ in 0..4 {
|
||||
if r.bool()? {
|
||||
// ClientAsset.ResourceTexture: its id and its texture path.
|
||||
r.string(32767)?;
|
||||
r.string(32767)?;
|
||||
}
|
||||
}
|
||||
Self::Profile(Profile {
|
||||
name,
|
||||
id,
|
||||
properties,
|
||||
})
|
||||
}
|
||||
CUSTOM_MODEL_DATA => {
|
||||
let n = r.count(r.remaining())?;
|
||||
let floats = (0..n).map(|_| r.f32()).collect::<Result<_>>()?;
|
||||
@@ -175,6 +239,30 @@ impl Component {
|
||||
w.varint(*level);
|
||||
}
|
||||
}
|
||||
Self::AttackRange(values) => values.iter().for_each(|v| w.f32(*v)),
|
||||
Self::Profile(p) => {
|
||||
w.u8(0);
|
||||
w.bool(p.name.is_some());
|
||||
if let Some(name) = &p.name {
|
||||
w.string(name);
|
||||
}
|
||||
w.bool(p.id.is_some());
|
||||
if let Some(id) = p.id {
|
||||
w.uuid(id);
|
||||
}
|
||||
w.varint(p.properties.len() as i32);
|
||||
for (name, value, signature) in &p.properties {
|
||||
w.string(name);
|
||||
w.string(value);
|
||||
w.bool(signature.is_some());
|
||||
if let Some(signature) = signature {
|
||||
w.string(signature);
|
||||
}
|
||||
}
|
||||
for _ in 0..4 {
|
||||
w.bool(false);
|
||||
}
|
||||
}
|
||||
Self::CustomModelData {
|
||||
floats,
|
||||
flags,
|
||||
@@ -471,4 +559,50 @@ mod tests {
|
||||
let bytes = [1, 5, 1, 0, 99];
|
||||
assert!(Slot::read(&mut Reader::new(&bytes)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attack_range_and_profiles_round_trip() {
|
||||
let mut sword = Stack::new(900, 1);
|
||||
sword.added.push((
|
||||
component::ATTACK_RANGE,
|
||||
Component::AttackRange([0.0, 3.0, 0.0, 4.0, 0.1, 1.0]),
|
||||
));
|
||||
sword.added.push((
|
||||
component::PROFILE,
|
||||
Component::Profile(Profile {
|
||||
name: Some("Player53470".into()),
|
||||
id: Some(0x7a5d_6a1a_30d7_4187_88c5_efe9_1201_8922),
|
||||
properties: vec![("textures".into(), "dGV4dHVyZXM=".into(), None)],
|
||||
}),
|
||||
));
|
||||
round::<Option<Stack>, Slot>(Some(sword));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_partial_profile_decodes_from_the_wire() {
|
||||
// count 1, item 1, one added component of kind 72 (profile): a partial
|
||||
// profile with an empty name, a UUID, one unsigned texture property
|
||||
// and an empty skin patch.
|
||||
let uuid = [
|
||||
0x7a, 0x5d, 0x6a, 0x1a, 0x30, 0xd7, 0x41, 0x87, 0x88, 0xc5, 0xef, 0xe9, 0x12, 0x01,
|
||||
0x89, 0x22,
|
||||
];
|
||||
let mut bytes = vec![1, 1, 1, 0, 72, 0, 1, 0, 1];
|
||||
bytes.extend_from_slice(&uuid);
|
||||
bytes.extend_from_slice(&[1, 8]);
|
||||
bytes.extend_from_slice(b"textures");
|
||||
bytes.extend_from_slice(&[1, b'x', 0, 0, 0, 0, 0]);
|
||||
let mut r = Reader::new(&bytes);
|
||||
let stack = Slot::read(&mut r).unwrap().unwrap();
|
||||
assert_eq!(r.remaining(), 0);
|
||||
let Some(Component::Profile(profile)) = stack.component(component::PROFILE) else {
|
||||
panic!("the profile component did not decode");
|
||||
};
|
||||
assert_eq!(profile.name.as_deref(), Some(""));
|
||||
assert_eq!(profile.id, Some(0x7a5d_6a1a_30d7_4187_88c5_efe9_1201_8922));
|
||||
assert_eq!(
|
||||
profile.properties,
|
||||
[("textures".to_owned(), "x".to_owned(), None)]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,6 +273,68 @@ packet!(Login {
|
||||
online_mode: bool => Bool,
|
||||
enforces_secure_chat: bool => Bool,
|
||||
});
|
||||
packet!(
|
||||
/// MapDecoration: a registry holder, its map position and rotation, and an
|
||||
/// optional name.
|
||||
MapDecoration {
|
||||
kind: i32 => VarInt,
|
||||
x: i8 => Byte,
|
||||
y: i8 => Byte,
|
||||
rot: i8 => Byte,
|
||||
name: Option<Tag> => Opt<Nbt>,
|
||||
}
|
||||
);
|
||||
|
||||
/// MapItemSavedData.MapPatch: an empty patch is a zero width.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MapPatch {
|
||||
pub start_x: i32,
|
||||
pub start_y: i32,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub colors: Vec<u8>,
|
||||
}
|
||||
pub struct MapPatchOpt;
|
||||
impl Codec<Option<MapPatch>> for MapPatchOpt {
|
||||
fn read(r: &mut Reader) -> Result<Option<MapPatch>> {
|
||||
let width = i32::from(r.u8()?);
|
||||
if width == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let height = i32::from(r.u8()?);
|
||||
let start_x = i32::from(r.u8()?);
|
||||
let start_y = i32::from(r.u8()?);
|
||||
let n = r.count(r.remaining())?;
|
||||
let colors = r.bytes(n)?.to_vec();
|
||||
Ok(Some(MapPatch {
|
||||
start_x,
|
||||
start_y,
|
||||
width,
|
||||
height,
|
||||
colors,
|
||||
}))
|
||||
}
|
||||
fn write(patch: &Option<MapPatch>, w: &mut Writer) {
|
||||
let Some(patch) = patch else {
|
||||
w.u8(0);
|
||||
return;
|
||||
};
|
||||
w.u8(patch.width as u8);
|
||||
w.u8(patch.height as u8);
|
||||
w.u8(patch.start_x as u8);
|
||||
w.u8(patch.start_y as u8);
|
||||
w.varint(patch.colors.len() as i32);
|
||||
w.bytes(&patch.colors);
|
||||
}
|
||||
}
|
||||
|
||||
packet!(MapItemData {
|
||||
map_id: i32 => VarInt,
|
||||
scale: i8 => Byte,
|
||||
locked: bool => Bool,
|
||||
decorations: Option<Vec<MapDecoration>> => Opt<List<MapDecoration>>,
|
||||
patch: Option<MapPatch> => MapPatchOpt,
|
||||
});
|
||||
/// VecDelta: per-axis deltas in 1/4096 blocks, linear or in timed steps.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum VecDelta {
|
||||
@@ -1425,6 +1487,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound {
|
||||
LevelEvent(LevelEvent) = LEVEL_EVENT,
|
||||
LightUpdate(LightUpdate) = LIGHT_UPDATE,
|
||||
Login(Login) = LOGIN,
|
||||
MapItemData(MapItemData) = MAP_ITEM_DATA,
|
||||
MoveEntityPos(MoveEntityPos) = MOVE_ENTITY_POS,
|
||||
MoveEntityPosRot(MoveEntityPosRot) = MOVE_ENTITY_POS_ROT,
|
||||
MoveEntityRot(MoveEntityRot) = MOVE_ENTITY_ROT,
|
||||
@@ -2490,4 +2553,26 @@ mod tests {
|
||||
assert_eq!(crate::ids::play::clientbound::UPDATE_ATTRIBUTES, 134);
|
||||
assert_eq!(Clientbound::decode(134, &body[2..]).unwrap(), p);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_item_data_carries_a_server_patch() {
|
||||
let patch = MapPatch {
|
||||
start_x: 3,
|
||||
start_y: 4,
|
||||
width: 2,
|
||||
height: 1,
|
||||
colors: vec![1, 2],
|
||||
};
|
||||
let mut w = Writer::new();
|
||||
MapPatchOpt::write(&Some(patch.clone()), &mut w);
|
||||
assert_eq!(w.data, [2, 1, 3, 4, 2, 1, 2]);
|
||||
let p = Clientbound::MapItemData(MapItemData {
|
||||
map_id: 2146483647,
|
||||
scale: 1,
|
||||
locked: true,
|
||||
decorations: None,
|
||||
patch: Some(patch),
|
||||
});
|
||||
round_trip(p, Clientbound::encode, Clientbound::decode);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user