Merge pull request #1934 from socksprox/feat/player-head-skins
🤖 Agent: Thanks socksprox! Player heads now show their owner's skin, fetched like vanilla's SkullBlockRenderer from the profile's textures property; TEXTURE_URIS limits it to textures.minecraft.net, so there's no new host.
This commit is contained in:
@@ -367,6 +367,7 @@ pub fn main() -> Result<()> {
|
||||
lid: &|_| open,
|
||||
sign: &|_| sign.as_ref(),
|
||||
banner: &|_| layers.clone(),
|
||||
skull: &|_| None,
|
||||
glyph: &|c| atlas.glyph_cell(c),
|
||||
};
|
||||
let mut verts = Vec::new();
|
||||
|
||||
@@ -730,6 +730,10 @@ pub struct Scene<'a> {
|
||||
pub sign: &'a dyn Fn([i32; 3]) -> Option<&'a crate::signs::SignData>,
|
||||
/// Banner layers at a position: pattern texture and RGB tint, bottom first.
|
||||
pub banner: &'a dyn Fn([i32; 3]) -> Vec<(String, u32)>,
|
||||
/// The skin sprite a `player_head` shows at a position (`SkullBlockRenderer`
|
||||
/// profile → skin): a downloaded `skins/<sha1>` slot or the profile's
|
||||
/// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (`DefaultPlayerSkin.getDefaultTexture`, slim steve).
|
||||
pub skull: &'a dyn Fn([i32; 3]) -> Option<&'a str>,
|
||||
/// Font glyph cell (none for blank glyphs such as the space) and advance.
|
||||
pub glyph: &'a dyn Fn(char) -> (Option<Rect>, f32),
|
||||
}
|
||||
@@ -1279,6 +1283,13 @@ impl BlockEntityRenderer {
|
||||
SkullType::Piglin => ("piglin_head", "entity/piglin/piglin"),
|
||||
SkullType::Player => ("player_head", "entity/player/slim/steve"),
|
||||
};
|
||||
// SkullBlockRenderer.getRenderType: the profile's skin when it
|
||||
// resolves, `SKIN_BY_TYPE` (steve) otherwise.
|
||||
let texture = if *kind == SkullType::Player {
|
||||
(scene.skull)(pos).unwrap_or(texture)
|
||||
} else {
|
||||
texture
|
||||
};
|
||||
let (Some(model), Some(paint)) =
|
||||
(self.models.get(model), paint(texture, 0xFFFFFF, packed))
|
||||
else {
|
||||
@@ -1886,6 +1897,7 @@ mod tests {
|
||||
lid: &|_| 0.0,
|
||||
sign: &|_| None,
|
||||
banner: &|_| Vec::new(),
|
||||
skull: &|_| None,
|
||||
glyph: &|_| (None, 0.0),
|
||||
};
|
||||
// Compare the cached world path with the original model renderer,
|
||||
|
||||
@@ -83,6 +83,60 @@ pub enum BlockEntity {
|
||||
Beehive(Box<crate::beehive::Beehive>),
|
||||
/// `CommandBlockEntity` of the three command blocks (`crate::command_block`).
|
||||
CommandBlock(Box<crate::command_block::CommandBlock>),
|
||||
/// `SkullBlockEntity` of every skull and head block.
|
||||
Skull(Skull),
|
||||
}
|
||||
/// `SkullBlockEntity`: the player profile the head shows (kept as loaded:
|
||||
/// a compound, or a string when it names a player only), the note block
|
||||
/// sound and `custom_name`.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Skull {
|
||||
pub profile: Option<horizon_world::nbt::Tag>,
|
||||
pub note_block_sound: Option<String>,
|
||||
pub custom_name: Option<horizon_world::nbt::Tag>,
|
||||
/// What `profile` draws, read from it once rather than every frame.
|
||||
pub skin: Option<SkullSkin>,
|
||||
}
|
||||
impl Skull {
|
||||
pub fn new(
|
||||
profile: Option<horizon_world::nbt::Tag>,
|
||||
note_block_sound: Option<String>,
|
||||
custom_name: Option<horizon_world::nbt::Tag>,
|
||||
) -> Self {
|
||||
let skin = profile.as_ref().map(SkullSkin::of);
|
||||
Self {
|
||||
profile,
|
||||
note_block_sound,
|
||||
custom_name,
|
||||
skin,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `SkullBlockRenderer.getPlayerSkin` of a head's profile: its skin's URL
|
||||
/// with the `skins/<sha1>` sprite it downloads to, and the
|
||||
/// `DefaultPlayerSkin` drawn until that sprite is there (steve for a
|
||||
/// profile with no id).
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SkullSkin {
|
||||
pub download: Option<(String, String)>,
|
||||
pub fallback: &'static str,
|
||||
}
|
||||
impl SkullSkin {
|
||||
pub fn of(profile: &horizon_world::nbt::Tag) -> Self {
|
||||
let download = crate::skins::profile_skin_url_nbt(profile).and_then(|url| {
|
||||
let location = crate::skins::skin_location(&crate::skins::texture_hash(&url)?);
|
||||
Some((url, location))
|
||||
});
|
||||
// DefaultPlayerSkin.get(id), or getDefaultSkin (slim steve) without an id.
|
||||
let fallback = crate::skins::profile_uuid_nbt(profile)
|
||||
.map_or("entity/player/slim/steve", crate::tab_list::default_skin);
|
||||
Self { download, fallback }
|
||||
}
|
||||
}
|
||||
/// The skull and head blocks (`SkullBlock`, `WallSkullBlock`): every
|
||||
/// `*_skull` and `*_head` but the piston's head.
|
||||
fn is_skull(name: &str) -> bool {
|
||||
name.ends_with("_skull") || (name.ends_with("_head") && name != "minecraft:piston_head")
|
||||
}
|
||||
/// `CrafterBlockEntity`: nine slots that can be disabled, the powered flag
|
||||
/// and the ticks left in the crafting animation.
|
||||
@@ -238,6 +292,9 @@ impl BlockEntity {
|
||||
if name.ends_with("_banner") {
|
||||
return Some(Self::Banner(Banner::default()));
|
||||
}
|
||||
if is_skull(name) {
|
||||
return Some(Self::Skull(Skull::default()));
|
||||
}
|
||||
if name == "minecraft:beacon" {
|
||||
return Some(Self::Beacon(Box::new(Beacon {
|
||||
last_check_y: i32::MIN / 2,
|
||||
@@ -283,6 +340,7 @@ impl BlockEntity {
|
||||
Self::Vault(_) => "minecraft:vault",
|
||||
Self::Beehive(_) => "minecraft:beehive",
|
||||
Self::CommandBlock(_) => "minecraft:command_block",
|
||||
Self::Skull(_) => "minecraft:skull",
|
||||
}
|
||||
}
|
||||
pub fn contents(&self) -> Vec<ItemStack> {
|
||||
@@ -298,7 +356,8 @@ impl BlockEntity {
|
||||
| Self::TrialSpawner(_)
|
||||
| Self::Vault(_)
|
||||
| Self::Beehive(_)
|
||||
| Self::CommandBlock(_) => Vec::new(),
|
||||
| Self::CommandBlock(_)
|
||||
| Self::Skull(_) => Vec::new(),
|
||||
Self::Lectern(l) => l.book.iter().cloned().collect(),
|
||||
Self::Sculk(_) => Vec::new(),
|
||||
Self::Banner(_) => Vec::new(),
|
||||
@@ -315,7 +374,8 @@ impl BlockEntity {
|
||||
| Self::Beacon(_)
|
||||
| Self::Lectern(_)
|
||||
| Self::Sculk(_)
|
||||
| Self::Banner(_) => &[],
|
||||
| Self::Banner(_)
|
||||
| Self::Skull(_) => &[],
|
||||
Self::Crafter(c) => &c.items,
|
||||
Self::CreakingHeart(_)
|
||||
| Self::Spawner(_)
|
||||
@@ -335,7 +395,8 @@ impl BlockEntity {
|
||||
| Self::Beacon(_)
|
||||
| Self::Lectern(_)
|
||||
| Self::Sculk(_)
|
||||
| Self::Banner(_) => &mut [],
|
||||
| Self::Banner(_)
|
||||
| Self::Skull(_) => &mut [],
|
||||
Self::Crafter(c) => &mut c.items,
|
||||
Self::CreakingHeart(_)
|
||||
| Self::Spawner(_)
|
||||
|
||||
@@ -5164,7 +5164,8 @@ impl Game<'_> {
|
||||
| BlockEntity::TrialSpawner(_)
|
||||
| BlockEntity::Vault(_)
|
||||
| BlockEntity::Beehive(_)
|
||||
| BlockEntity::CommandBlock(_),
|
||||
| BlockEntity::CommandBlock(_)
|
||||
| BlockEntity::Skull(_),
|
||||
) => {}
|
||||
None => {
|
||||
v.extend((0..self.craft_size * self.craft_size).map(|i| 100 + i));
|
||||
@@ -5433,7 +5434,8 @@ impl Game<'_> {
|
||||
| BlockEntity::TrialSpawner(_)
|
||||
| BlockEntity::Vault(_)
|
||||
| BlockEntity::Beehive(_)
|
||||
| BlockEntity::CommandBlock(_),
|
||||
| BlockEntity::CommandBlock(_)
|
||||
| BlockEntity::Skull(_),
|
||||
) => {}
|
||||
None if self.mount_active() => self.draw_mount_background(batch, ox, oy),
|
||||
None => {
|
||||
@@ -6950,6 +6952,7 @@ impl Game<'_> {
|
||||
_ => None,
|
||||
},
|
||||
banner: &|p| self.banner_layers_at(p),
|
||||
skull: &|p| self.skull_skin_at(p),
|
||||
glyph: &|c| self.gui.atlas.glyph_cell(c),
|
||||
};
|
||||
// Block entities outside the view frustum are skipped (with a
|
||||
|
||||
@@ -8,8 +8,10 @@ use horizon_client::{
|
||||
block_entity_anim::{BellEntity, EndGateway, Shulker, ShulkerStatus, Ticker},
|
||||
block_entity_render::BlockAnim,
|
||||
collisions::{SceneCollisions, no_collision_empty},
|
||||
containers::BlockEntity,
|
||||
particles::{ParticleOptions, ParticleType},
|
||||
save::number,
|
||||
skins,
|
||||
};
|
||||
use horizon_core::geometry::{Aabb, Vec3};
|
||||
use horizon_world::nbt::Tag;
|
||||
@@ -392,4 +394,25 @@ impl Game<'_> {
|
||||
pub(super) fn chest_lid(&self, pos: [i32; 3], partial: f32) -> f32 {
|
||||
self.be_anims.lid(pos, partial)
|
||||
}
|
||||
/// `SkullBlockRenderer.getPlayerSkin`: the skin sprite of the profile of
|
||||
/// the player head at `pos`. The skin download is started here; while it
|
||||
/// is in flight (or when it cannot be got) the profile's default skin
|
||||
/// stands in. A head with no profile answers None: `SKIN_BY_TYPE`.
|
||||
pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option<&str> {
|
||||
let Some(BlockEntity::Skull(skull)) =
|
||||
self.world.block_entities.get(&(pos[0], pos[1], pos[2]))
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let skin = skull.skin.as_ref()?;
|
||||
if let Some((url, location)) = &skin.download {
|
||||
self.skin_downloads
|
||||
.borrow_mut()
|
||||
.state(skins::Kind::Skin, url);
|
||||
if self.gui.atlas.sprites.contains_key(location) {
|
||||
return Some(location);
|
||||
}
|
||||
}
|
||||
Some(skin.fallback)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1463,6 +1463,7 @@ impl Game<'_> {
|
||||
lid: &lid,
|
||||
sign: &|_| None,
|
||||
banner: &|_| Vec::new(),
|
||||
skull: &|_| None,
|
||||
glyph: &|c| self.gui.atlas.glyph_cell(c),
|
||||
};
|
||||
self.res
|
||||
|
||||
@@ -937,6 +937,20 @@ pub fn entity_tag(entity: &BlockEntity) -> Option<Tag> {
|
||||
match entity {
|
||||
BlockEntity::Sign(sign) => Some(Tag::compound(sign.encode())),
|
||||
BlockEntity::Spawner(s) => Some(s.update_tag()),
|
||||
// SkullBlockEntity: the profile is the whole reason a head differs.
|
||||
BlockEntity::Skull(s) => {
|
||||
let mut entries = Vec::new();
|
||||
if let Some(profile) = &s.profile {
|
||||
entries.push(("profile", profile.clone()));
|
||||
}
|
||||
if let Some(sound) = &s.note_block_sound {
|
||||
entries.push(("note_block_sound", Tag::String(sound.as_str().into())));
|
||||
}
|
||||
if let Some(name) = &s.custom_name {
|
||||
entries.push(("custom_name", name.clone()));
|
||||
}
|
||||
Some(Tag::compound(entries))
|
||||
}
|
||||
BlockEntity::Jukebox(_) | BlockEntity::Lectern(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
|
||||
@@ -553,6 +553,17 @@ pub fn encode_block_entity(
|
||||
entries.push(("creaking", uuid_tag(uuid)));
|
||||
}
|
||||
}
|
||||
BlockEntity::Skull(s) => {
|
||||
if let Some(profile) = &s.profile {
|
||||
entries.push(("profile", profile.clone()));
|
||||
}
|
||||
if let Some(sound) = &s.note_block_sound {
|
||||
entries.push(("note_block_sound", string(sound)));
|
||||
}
|
||||
if let Some(name) = &s.custom_name {
|
||||
entries.push(("custom_name", name.clone()));
|
||||
}
|
||||
}
|
||||
BlockEntity::Spawner(s) => entries.extend(s.save()),
|
||||
BlockEntity::TrialSpawner(s) => entries.extend(s.save()),
|
||||
BlockEntity::Vault(v) => entries.extend(v.save()),
|
||||
@@ -628,6 +639,18 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
|
||||
};
|
||||
return Some((pos, BlockEntity::Beacon(Box::new(beacon))));
|
||||
}
|
||||
// A skull's block entity id is not its block's name (every head block).
|
||||
if id == "minecraft:skull" {
|
||||
return Some((
|
||||
pos,
|
||||
BlockEntity::Skull(crate::containers::Skull::new(
|
||||
tag.get("profile").cloned(),
|
||||
text(tag.get("note_block_sound")),
|
||||
tag.get("custom_name")
|
||||
.and_then(crate::block_entity_nbt::component),
|
||||
)),
|
||||
));
|
||||
}
|
||||
// A spawner's block entity id is not its block's name.
|
||||
let block = match id.as_str() {
|
||||
"minecraft:mob_spawner" => "minecraft:spawner",
|
||||
@@ -700,7 +723,8 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
|
||||
| BlockEntity::Beacon(_)
|
||||
| BlockEntity::Lectern(_)
|
||||
| BlockEntity::Banner(_)
|
||||
| BlockEntity::CreakingHeart(_) => {}
|
||||
| BlockEntity::CreakingHeart(_)
|
||||
| BlockEntity::Skull(_) => {}
|
||||
BlockEntity::Spawner(s) => **s = crate::mobs::spawner::Spawner::load(tag),
|
||||
BlockEntity::TrialSpawner(s) => **s = crate::mobs::trial_spawner::TrialSpawner::load(tag),
|
||||
BlockEntity::Vault(v) => **v = crate::vault::Vault::load(tag),
|
||||
@@ -3893,6 +3917,54 @@ mod tests {
|
||||
}
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn skull_block_entity_keeps_its_profile() {
|
||||
// A placed `player_head` as vanilla saves it: the owning player's
|
||||
// `profile` survives load, encode and `canonical`.
|
||||
let saved = horizon_world::nbt::parse_snbt(
|
||||
r#"{id:"minecraft:skull",x:10,y:64,z:20,profile:{name:"Steve",id:[I;1,2,3,4],properties:[{name:"textures",value:"abc"}]},note_block_sound:"minecraft:entity.player.hurt",custom_name:"Trophy"}"#,
|
||||
)
|
||||
.unwrap()
|
||||
.0;
|
||||
let (pos, back) = decode_block_entity(&saved).expect("skull");
|
||||
assert_eq!(pos, (10, 64, 20));
|
||||
let crate::containers::BlockEntity::Skull(skull) = &back else {
|
||||
panic!("not a skull");
|
||||
};
|
||||
assert_eq!(skull.profile, saved.get("profile").cloned());
|
||||
assert_eq!(
|
||||
skull.note_block_sound.as_deref(),
|
||||
Some("minecraft:entity.player.hurt")
|
||||
);
|
||||
assert_eq!(back.id(), "minecraft:skull");
|
||||
let tag = encode_block_entity(pos, &back, "minecraft:player_head");
|
||||
assert_eq!(
|
||||
crate::block_entity_nbt::id(&tag).as_deref(),
|
||||
Some("minecraft:skull")
|
||||
);
|
||||
assert_eq!(tag.get("profile"), saved.get("profile"));
|
||||
let (_, again) = decode_block_entity(&tag).expect("skull again");
|
||||
let crate::containers::BlockEntity::Skull(again) = again else {
|
||||
panic!("not a skull");
|
||||
};
|
||||
assert_eq!(again.profile, skull.profile);
|
||||
// A profile-less head (skeleton, creeper) round-trips too.
|
||||
let plain = encode_block_entity(
|
||||
pos,
|
||||
&crate::containers::BlockEntity::Skull(crate::containers::Skull::default()),
|
||||
"minecraft:creeper_head",
|
||||
);
|
||||
let (_, decoded) = decode_block_entity(&plain).expect("plain skull");
|
||||
assert!(matches!(decoded, crate::containers::BlockEntity::Skull(_)));
|
||||
// The heads and skulls have one; the piston's head is no skull.
|
||||
use crate::containers::BlockEntity;
|
||||
assert!(matches!(
|
||||
BlockEntity::for_block("minecraft:player_wall_head"),
|
||||
Some(BlockEntity::Skull(_))
|
||||
));
|
||||
assert!(BlockEntity::for_block("minecraft:piston_head").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uuid_text_round_trips() {
|
||||
let u = [-158327916, 1572358581, -2125825222, -487948715];
|
||||
|
||||
@@ -130,6 +130,47 @@ pub fn profile_skin_url(profile: &serde_json::Value) -> Option<String> {
|
||||
unpack(value, Signature::Unsigned, &TEXTURE_URIS).skin
|
||||
}
|
||||
|
||||
/// `profile_skin_url` on a block entity's `profile` NBT (`SkullBlockEntity`):
|
||||
/// the same `properties` list, held as a compound. A name-only profile (a
|
||||
/// string tag) has no properties and no skin URL.
|
||||
pub fn profile_skin_url_nbt(profile: &horizon_world::nbt::Tag) -> Option<String> {
|
||||
let properties = match profile.get("properties")? {
|
||||
horizon_world::nbt::Tag::List(list) => list,
|
||||
_ => return None,
|
||||
};
|
||||
let value = properties.iter().find_map(|p| {
|
||||
let name = nbt_text(p.get("name"))?;
|
||||
(name == "textures")
|
||||
.then(|| nbt_text(p.get("value")))
|
||||
.flatten()
|
||||
})?;
|
||||
unpack(&value, Signature::Unsigned, &TEXTURE_URIS).skin
|
||||
}
|
||||
|
||||
/// The profile's uuid as u128: `id` as an int array (block entity NBT) or a
|
||||
/// uuid string (older and component forms). Used for `DefaultPlayerSkin.get`
|
||||
/// when a head's skin is not downloaded yet.
|
||||
pub fn profile_uuid_nbt(profile: &horizon_world::nbt::Tag) -> Option<u128> {
|
||||
match profile.get("id")? {
|
||||
horizon_world::nbt::Tag::IntArray(v) if v.len() == 4 => Some(
|
||||
v.iter()
|
||||
.fold(0u128, |a, &w| (a << 32) | u128::from(w as u32)),
|
||||
),
|
||||
horizon_world::nbt::Tag::String(s) => {
|
||||
let digits: String = s.to_string_lossy().chars().filter(|c| *c != '-').collect();
|
||||
u128::from_str_radix(&digits, 16).ok()
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn nbt_text(tag: Option<&horizon_world::nbt::Tag>) -> Option<String> {
|
||||
match tag {
|
||||
Some(horizon_world::nbt::Tag::String(s)) => Some(s.to_string_lossy()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `MinecraftProfileTexture.getHash`: the base name of the URL's path.
|
||||
pub fn texture_hash(url: &str) -> Option<String> {
|
||||
let rest = url.split_once("://")?.1;
|
||||
@@ -457,6 +498,42 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn skull_block_entity_profiles_supply_head_skins() {
|
||||
let value = "eyJ0ZXh0dXJlcyI6IHsiU0tJTiI6IHsidXJsIjogImh0dHBzOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2FiYzEyMyJ9fX0=";
|
||||
let profile = horizon_world::nbt::parse_snbt(&format!(
|
||||
r#"{{name:"T",id:[I;-158327916,1572358581,-2125825222,-487948715],properties:[{{name:"textures",value:"{value}"}}]}}"#
|
||||
))
|
||||
.unwrap()
|
||||
.0;
|
||||
assert_eq!(
|
||||
super::profile_skin_url_nbt(&profile).as_deref(),
|
||||
Some("https://textures.minecraft.net/texture/abc123")
|
||||
);
|
||||
assert_eq!(
|
||||
super::profile_uuid_nbt(&profile),
|
||||
Some(
|
||||
[-158327916, 1572358581, -2125825222, -487948715]
|
||||
.iter()
|
||||
.fold(0u128, |a, &w: &i32| (a << 32) | u128::from(w as u32))
|
||||
)
|
||||
);
|
||||
// A name-only profile (a string tag) has no properties: no url, no id.
|
||||
let named = horizon_world::nbt::Tag::String("MHF_Blaze".into());
|
||||
assert_eq!(super::profile_skin_url_nbt(&named), None);
|
||||
assert_eq!(super::profile_uuid_nbt(&named), None);
|
||||
// A uuid in string form decodes too.
|
||||
let text_id = horizon_world::nbt::parse_snbt(
|
||||
r#"{name:"T",id:"e843a1f4-0000-0001-0000-000200000003"}"#,
|
||||
)
|
||||
.unwrap()
|
||||
.0;
|
||||
assert_eq!(
|
||||
super::profile_uuid_nbt(&text_id),
|
||||
Some(u128::from_str_radix("e843a1f4000000010000000200000003", 16).unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sha1_and_base64() {
|
||||
let hex: String = sha1(b"abc").iter().map(|b| format!("{b:02x}")).collect();
|
||||
|
||||
Reference in New Issue
Block a user