diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 03b48df3d..d64449f55 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1906,10 +1906,14 @@ impl Game<'_> { } // LivingEntity.DATA_LIVING_ENTITY_FLAGS: for another player // too, remote_mob_data's apply reads index 8 above. - let other = self - .remote - .as_ref() - .is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id); + let mannequin = self + .mobs + .iter() + .any(|m| m.id == p.id as u32 && &*m.kind.id == "minecraft:mannequin"); + let other = mannequin + || self.remote.as_ref().is_some_and(|r| { + r.level_players.contains_key(&p.id) && r.entity_id != p.id + }); // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): another // player lies down in their bed (drawn lying in it), or gets up. if other { @@ -1960,11 +1964,13 @@ impl Game<'_> { } } // Entity.DATA_SHARED_FLAGS_ID's crouching bit: another - // player's crouch (their model and Entity.isDiscrete's name tag). - if self - .remote - .as_ref() - .is_some_and(|r| r.level_players.contains_key(&p.id)) + // player's or a mannequin's crouch (their model and + // Entity.isDiscrete's name tag). + if (mannequin + || self + .remote + .as_ref() + .is_some_and(|r| r.level_players.contains_key(&p.id))) && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { for (index, _, value) in &p.values { @@ -2781,6 +2787,53 @@ impl Game<'_> { { mob.skin = Some(skin); } + // ClientMannequin.updateSkin (PlayerSkinRenderCache.lookup): a + // mannequin's DATA_PROFILE `textures` property resolves like a + // player's, never required to be signed. + let mannequins: Vec<(u32, horizon_client::skins::Textures)> = self + .mobs + .iter() + .filter(|m| &*m.kind.id == "minecraft:mannequin" && m.profile.is_some()) + .map(|m| { + let t = m + .profile + .as_ref() + .and_then(|p| p.properties.iter().find(|p| p.name == "textures")); + let textures = t.map_or_else(Default::default, |t| { + let signature = if t.signature.is_some() { + horizon_client::skins::Signature::Signed + } else { + horizon_client::skins::Signature::Unsigned + }; + horizon_client::skins::unpack( + &t.value, + signature, + &horizon_client::skins::TEXTURE_URIS, + ) + }); + (m.id, textures) + }) + .collect(); + let mut resolved: Vec<(u32, Option)> = Vec::new(); + for (id, textures) in mannequins { + let skin = { + let mut downloads = self.skin_downloads.borrow_mut(); + horizon_client::skins::lookup(&textures, false, |kind, url| { + downloads.state(kind, url) + }) + }; + resolved.push((id, skin)); + } + for (id, skin) in resolved { + let skin = skin.filter(|s| !s.starts_with("skins/") || self.show_skin(s)); + if let Some(mob) = self + .mobs + .iter_mut() + .find(|m| m.id == id && &*m.kind.id == "minecraft:mannequin") + { + mob.skin = skin; + } + } } /// Puts a downloaded skin in an atlas skin slot: a free one, else the /// least recently shown. diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs index da786c1b9..fbfc5a4fb 100644 --- a/crates/horizon-client/src/game/remote_mob_data.rs +++ b/crates/horizon-client/src/game/remote_mob_data.rs @@ -75,6 +75,11 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin) { // Zombie's three): what VillagerProfessionLayer draws. (Species::Villager, 19, Value::Ids(data)) | (Species::ZombieVillager, 20, Value::Ids(data)) => villager_data(m, data, builtin), + // Mannequin.DATA_PROFILE (after Avatar's two): whose skin it + // wears; update_remote_skins resolves it like a tab player's. + (Species::Mannequin, 17, Value::Profile(profile)) => { + m.profile = Some(profile.clone()); + } _ => {} } } @@ -295,4 +300,22 @@ mod tests { apply(&mut e, &[(17, s::BYTE, Value::Byte(2))]); assert_eq!(e.extra.variant2, 2); } + + #[test] + fn a_mannequin_exists_and_takes_its_profile() { + let Some(mut m) = mob("minecraft:mannequin") else { + return; + }; + assert_eq!(&*m.kind.id, "minecraft:mannequin"); + let profile = horizon_protocol::entity_data::Profile { + name: Some("Gommunity".into()), + uuid: Some(1), + ..Default::default() + }; + apply( + &mut m, + &[(17, s::RESOLVABLE_PROFILE, Value::Profile(profile.clone()))], + ); + assert_eq!(m.profile, Some(profile)); + } } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 46834e9d1..1b5dddbe7 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -861,6 +861,9 @@ pub struct Mob { /// The skin texture of a player mob (`PlayerInfo.getSkin`), which /// replaces the type's default when its download is ready. pub skin: Option, + /// A mannequin's `DATA_PROFILE` (ResolvableProfile): the profile's + /// `textures` property resolves to `skin` like a player's. + pub profile: Option, } /// Events a mob tick asks the game to act on. #[derive(Clone, Debug)] @@ -1586,6 +1589,7 @@ impl Mob { rider: None, outbox: Vec::new(), skin: None, + profile: None, attrs: Attrs { speed: kind.speed, attack_damage: kind.attack_damage, diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index cb21cec8c..bbbf26738 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -92,6 +92,11 @@ fn climate(m: &Mob) -> &'static str { _ => "temperate", } } +/// `AvatarRenderer` entities: a mannequin draws exactly like a player — +/// the player model, its resolved skin and the same scale. +fn avatar(m: &Mob) -> bool { + matches!(&*m.kind.id, "minecraft:player" | "minecraft:mannequin") +} /// The model layer key and texture for the mob's body. pub fn body(m: &Mob) -> (String, String) { use Species as S; @@ -103,7 +108,7 @@ pub fn body(m: &Mob) -> (String, String) { s.to_owned() } }; - if &*m.kind.id == "minecraft:player" { + if avatar(m) { return ( "main".into(), m.skin.clone().unwrap_or_else(|| m.kind.texture.clone()), @@ -334,7 +339,7 @@ pub fn body_color(m: &Mob, alpha: f32) -> u32 { pub fn layers(m: &Mob) -> Vec { use Species as S; let mut v = Vec::new(); - if &*m.kind.id == "minecraft:player" { + if avatar(m) { return v; } let white = 0xFFFF_FFFF; @@ -754,7 +759,7 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 body += (f64::from(ticks * 3.25).cos() * std::f64::consts::PI * 0.4) as f32; } // A sleeping villager, or another player lying in their bed. - let sleep = (m.species() == Species::Villager || m.kind.id == "minecraft:player") + let sleep = (m.species() == Species::Villager || avatar(m)) .then(|| super::villager::sleep_pose(m)) .flatten(); let rotation = match sleep { @@ -873,7 +878,7 @@ fn red_overlay(m: &Mob) -> bool { m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m)) } fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 { - if m.kind.id == "minecraft:player" { + if avatar(m) { // AvatarRenderer.scale, shared by the inventory and world renderer. return glam::Mat4::from_scale(glam::Vec3::splat(0.9375)); } diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 28ea729b3..f48a21fe9 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -94,6 +94,7 @@ pub enum Species { Nautilus, ZombieNautilus, SulfurCube, + Mannequin, } /// Baby bounding boxes (each class's BABY_DIMENSIONS). #[derive(Clone, Copy, Debug)] @@ -1307,6 +1308,18 @@ pub const ALL: &[Def] = &[ None, Baby::None ), + // Mannequin: the player model; the DATA_PROFILE ResolvableProfile picks + // its skin, this texture only while that has none. + def!( + Mannequin, + "mannequin", + "player#main", + "entity/player/wide/steve", + A::Humanoid { zombie_arms: false }, + ["", "entity.generic.hurt", "entity.generic.death"], + None, + Baby::None + ), ]; pub fn def(s: Species) -> &'static Def { ALL.iter().find(|d| d.species == s).unwrap_or(&ALL[0]) @@ -1488,7 +1501,7 @@ impl Def { // Nautilus.handleAirSupply: it dries out on land. needs_water: aquatic || s == S::Nautilus, water_sensitive: matches!(s, S::Enderman | S::Blaze | S::SnowGolem), - animal: !matches!(s, S::Bat | S::SulfurCube) + animal: !matches!(s, S::Bat | S::SulfurCube | S::Mannequin) && !self.hostile_class() && !aquatic && !matches!( diff --git a/crates/horizon-protocol/src/entity_data.rs b/crates/horizon-protocol/src/entity_data.rs index 574afd300..47467de4a 100644 --- a/crates/horizon-protocol/src/entity_data.rs +++ b/crates/horizon-protocol/src/entity_data.rs @@ -34,6 +34,98 @@ pub enum Value { Particle(Particle), /// A particle list (PARTICLES: a VarInt count, then each particle). Particles(Vec), + /// A `ResolvableProfile` (a mannequin's DATA_PROFILE). + Profile(Profile), +} +/// `ResolvableProfile.STREAM_CODEC`: the GameProfile a mannequin or player +/// head resolves — either a full profile (id, name and properties on the +/// wire) or a partial one (a name and/or id to look up) — followed by a +/// `PlayerSkin.Patch` of texture overrides. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Profile { + pub name: Option, + pub uuid: Option, + /// `PropertyMap`: its `textures` property carries the skin. + pub properties: Vec, + /// `PlayerSkin.Patch`: texture asset ids overriding the profile's. + pub body: Option, + pub cape: Option, + pub elytra: Option, + /// `PlayerModelType`: true is the slim (4px arms) model. + pub slim: Option, +} +/// `ByteBufCodecs.GAME_PROFILE_PROPERTIES`. +fn read_properties(r: &mut Reader) -> Result> { + use crate::buf::Codec; + let n = r.count(16)?; + (0..n) + .map(|_| crate::packets::login::Property::read(r)) + .collect() +} +fn write_properties(p: &[crate::packets::login::Property], w: &mut Writer) { + use crate::buf::Codec; + w.varint(p.len() as i32); + p.iter() + .for_each(|p| crate::packets::login::Property::write(p, w)); +} +fn read_profile(r: &mut Reader) -> Result { + let mut p = Profile::default(); + // ByteBufCodecs.either: true is the full GameProfile, false a Partial. + if r.bool()? { + p.uuid = Some(r.uuid()?); + p.name = Some(r.string(16)?); + p.properties = read_properties(r)?; + } else { + if r.bool()? { + p.name = Some(r.string(16)?); + } + if r.bool()? { + p.uuid = Some(r.uuid()?); + } + p.properties = read_properties(r)?; + } + // PlayerSkin.Patch: three optional texture ids, then the model type. + if r.bool()? { + p.body = Some(r.string(32767)?); + } + if r.bool()? { + p.cape = Some(r.string(32767)?); + } + if r.bool()? { + p.elytra = Some(r.string(32767)?); + } + if r.bool()? { + p.slim = Some(r.bool()?); + } + Ok(p) +} +fn write_profile(p: &Profile, w: &mut Writer) { + let full = p.uuid.is_some() && p.name.is_some(); + w.bool(full); + if full { + w.uuid(p.uuid.unwrap_or(0)); + w.string(p.name.as_deref().unwrap_or("")); + } else { + w.bool(p.name.is_some()); + if let Some(name) = &p.name { + w.string(name); + } + w.bool(p.uuid.is_some()); + if let Some(uuid) = p.uuid { + w.uuid(uuid); + } + } + write_properties(&p.properties, w); + for texture in [&p.body, &p.cape, &p.elytra] { + w.bool(texture.is_some()); + if let Some(texture) = texture { + w.string(texture); + } + } + w.bool(p.slim.is_some()); + if let Some(slim) = p.slim { + w.bool(slim); + } } /// One `ParticleTypes.STREAM_CODEC` value: the `minecraft:particle_type` id, /// then the data its type carries (26.3 registry order). @@ -258,6 +350,7 @@ fn read_value(kind: i32, r: &mut Reader) -> Result> { | HUMANOID_ARM | DYE_COLOR => Value::Id(r.varint()?), PARTICLE => Value::Particle(Particle::read(r)?), + RESOLVABLE_PROFILE => Value::Profile(read_profile(r)?), PARTICLES => { let n = r.count(65536)?; let mut list = Vec::with_capacity(n.min(64)); @@ -305,6 +398,7 @@ fn write_value(kind: i32, v: &Value, w: &mut Writer) { w.varint(list.len() as i32); list.iter().for_each(|p| p.write(w)); } + Value::Profile(p) => write_profile(p, w), } let _ = kind; } @@ -455,4 +549,53 @@ mod tests { ] ); } + + /// RESOLVABLE_PROFILE (a mannequin's DATA_PROFILE): both ResolvableProfile + /// contents round-trip, and an entry after it still decodes. + #[test] + fn resolvable_profile_round_trips() { + use crate::packets::login::Property; + let full = Profile { + name: Some("Gommunity".into()), + uuid: Some(0x0123_4567_89AB_CDEF_0123_4567_89AB_CDEF), + properties: vec![Property { + name: "textures".into(), + value: "e30=".into(), + signature: Some("sig".into()), + }], + body: Some("minecraft:entity/player/wide/steve".into()), + cape: None, + elytra: None, + slim: Some(false), + }; + let partial = Profile { + name: Some("Gommunity".into()), + ..Profile::default() + }; + let mut w = Writer::new(); + EntityData::write( + &vec![ + ( + 17, + serializer::RESOLVABLE_PROFILE, + Value::Profile(full.clone()), + ), + ( + 17, + serializer::RESOLVABLE_PROFILE, + Value::Profile(partial.clone()), + ), + (18, serializer::BOOLEAN, Value::Bool(true)), + ], + &mut w, + ); + assert_eq!( + EntityData::read(&mut Reader::new(&w.data)).unwrap(), + vec![ + (17, serializer::RESOLVABLE_PROFILE, Value::Profile(full)), + (17, serializer::RESOLVABLE_PROFILE, Value::Profile(partial)), + (18, serializer::BOOLEAN, Value::Bool(true)), + ] + ); + } }