Fix item tooltip display parity
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
//! Tooltip display visibility oracle companion for tools/test_tooltip_display_parity.py.
|
||||
use horizon_client::{item_tooltip::TooltipDisplay, items::ItemStack};
|
||||
use horizon_world::nbt::Tag;
|
||||
use std::{error::Error, fmt::Write};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let mut output = String::new();
|
||||
for line in std::fs::read_to_string(&args[1])?.lines() {
|
||||
let fields: Vec<&str> = line.split('\t').collect();
|
||||
let hide_tooltip: bool = fields[0].parse()?;
|
||||
let creative: bool = fields[1].parse()?;
|
||||
let hide_attributes: bool = fields[2].parse()?;
|
||||
let mut component = Vec::new();
|
||||
if hide_tooltip {
|
||||
component.push(("hide_tooltip", Tag::Byte(1)));
|
||||
}
|
||||
if hide_attributes {
|
||||
component.push((
|
||||
"hidden_components",
|
||||
Tag::List(vec![Tag::String("minecraft:attribute_modifiers".into())]),
|
||||
));
|
||||
}
|
||||
let mut stack = ItemStack::new("minecraft:diamond_sword", 1);
|
||||
if !component.is_empty() {
|
||||
stack
|
||||
.data_mut()
|
||||
.set_other("minecraft:tooltip_display", Some(Tag::compound(component)));
|
||||
}
|
||||
let display = TooltipDisplay::for_stack(&stack);
|
||||
writeln!(
|
||||
output,
|
||||
"{}\t{}",
|
||||
display.name_visible(creative),
|
||||
display.shows("minecraft:attribute_modifiers")
|
||||
)?;
|
||||
}
|
||||
std::fs::write(&args[2], output)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1885,17 +1885,27 @@ impl Game<'_> {
|
||||
} else {
|
||||
self.pack.names.item(&stack.item)
|
||||
};
|
||||
let mut lines = vec![(name, color)];
|
||||
let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack);
|
||||
let mut lines = Vec::new();
|
||||
if display.name_visible(self.screen == Screen::Creative) {
|
||||
lines.push((name, color));
|
||||
}
|
||||
let translate = |k: &str| e.translate(k).map(str::to_owned);
|
||||
// ItemContainerContents.addToTooltip.
|
||||
let hover_name = |s: &ItemStack| {
|
||||
s.custom_name()
|
||||
.map_or_else(|| self.pack.names.item(&s.item), str::to_owned)
|
||||
};
|
||||
for line in horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) {
|
||||
lines.push((line, u32::MAX));
|
||||
if display.shows("minecraft:container") {
|
||||
for line in
|
||||
horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate)
|
||||
{
|
||||
lines.push((line, u32::MAX));
|
||||
}
|
||||
}
|
||||
if stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow" {
|
||||
if display.shows("minecraft:potion_contents")
|
||||
&& (stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow")
|
||||
{
|
||||
let scale = match &*stack.item {
|
||||
"minecraft:lingering_potion" => 0.25,
|
||||
"minecraft:tipped_arrow" => 0.125,
|
||||
@@ -1912,7 +1922,9 @@ impl Game<'_> {
|
||||
lines.push((line, c));
|
||||
}
|
||||
}
|
||||
if let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) {
|
||||
if display.shows("minecraft:trim")
|
||||
&& let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone())
|
||||
{
|
||||
lines.push((
|
||||
e.translate("item.minecraft.smithing_template.upgrade")
|
||||
.unwrap_or("Upgrade:")
|
||||
@@ -1929,30 +1941,44 @@ impl Game<'_> {
|
||||
lines.push((format!(" {p}"), rgba(170, 170, 170, 255)));
|
||||
lines.push((format!(" {m}"), rgba(170, 170, 170, 255)));
|
||||
}
|
||||
for (text, curse) in e
|
||||
.tooltip(stack.stored_enchantments())
|
||||
.into_iter()
|
||||
.chain(e.tooltip(stack.enchantments()))
|
||||
{
|
||||
lines.push((
|
||||
text,
|
||||
if curse {
|
||||
rgba(255, 85, 85, 255)
|
||||
} else {
|
||||
rgba(170, 170, 170, 255)
|
||||
},
|
||||
));
|
||||
if display.shows("minecraft:stored_enchantments") {
|
||||
for (text, curse) in e.tooltip(stack.stored_enchantments()) {
|
||||
lines.push((
|
||||
text,
|
||||
if curse {
|
||||
rgba(255, 85, 85, 255)
|
||||
} else {
|
||||
rgba(170, 170, 170, 255)
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
if display.shows("minecraft:enchantments") {
|
||||
for (text, curse) in e.tooltip(stack.enchantments()) {
|
||||
lines.push((
|
||||
text,
|
||||
if curse {
|
||||
rgba(255, 85, 85, 255)
|
||||
} else {
|
||||
rgba(170, 170, 170, 255)
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
// BannerPatternLayers.addToTooltip: up to six layer names.
|
||||
for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) {
|
||||
let key = self.res.stations.banners.layer_key(layer);
|
||||
lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255)));
|
||||
if display.shows("minecraft:banner_patterns") {
|
||||
for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) {
|
||||
let key = self.res.stations.banners.layer_key(layer);
|
||||
lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255)));
|
||||
}
|
||||
}
|
||||
// Spawner.appendHoverText: what a spawner or trial spawner item spawns.
|
||||
if matches!(
|
||||
&*stack.item,
|
||||
"minecraft:spawner" | "minecraft:trial_spawner"
|
||||
) {
|
||||
if display.shows("minecraft:block_entity_data")
|
||||
&& matches!(
|
||||
&*stack.item,
|
||||
"minecraft:spawner" | "minecraft:trial_spawner"
|
||||
)
|
||||
{
|
||||
let gray = rgba(170, 170, 170, 255);
|
||||
match block_entity_item::spawner_entity(stack) {
|
||||
Some(id) => {
|
||||
@@ -1969,7 +1995,9 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(info) = self.res.items.get(&stack.item) {
|
||||
if display.shows("minecraft:attribute_modifiers")
|
||||
&& let Some(info) = self.res.items.get(&stack.item)
|
||||
{
|
||||
let base = |name: &str, default| {
|
||||
self.player
|
||||
.as_ref()
|
||||
@@ -1997,7 +2025,8 @@ impl Game<'_> {
|
||||
&translate,
|
||||
));
|
||||
}
|
||||
if let Some(info) = self.res.items.get(&stack.item)
|
||||
if display.shows("minecraft:damage")
|
||||
&& let Some(info) = self.res.items.get(&stack.item)
|
||||
&& info.max_damage > 0
|
||||
&& stack.damage > 0
|
||||
&& self.options_advanced_tooltips()
|
||||
|
||||
@@ -110,6 +110,16 @@ pub const OMINOUS_LAYERS: [(&str, &str); 8] = [
|
||||
("circle", "light_gray"),
|
||||
("border", "black"),
|
||||
];
|
||||
|
||||
/// The raid captain banner's default tooltip setting, stored by vanilla as a
|
||||
/// component but represented by `StackData::ominous_banner` in Horizon.
|
||||
fn ominous_tooltip_display() -> Tag {
|
||||
Tag::compound([(
|
||||
"hidden_components",
|
||||
Tag::List(vec![Tag::String("minecraft:banner_patterns".into())]),
|
||||
)])
|
||||
}
|
||||
|
||||
/// `WrittenBookContent` with the pages as plain text.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct WrittenBook {
|
||||
@@ -610,13 +620,9 @@ pub fn components_tag(damage: u32, data: Option<&StackData>) -> Option<Tag> {
|
||||
"minecraft:banner_patterns",
|
||||
Tag::List(OMINOUS_LAYERS.iter().map(|(p, c)| layer(p, c)).collect()),
|
||||
));
|
||||
entries.push((
|
||||
"minecraft:tooltip_display",
|
||||
Tag::compound([(
|
||||
"hidden_components",
|
||||
Tag::List(vec![Tag::String("minecraft:banner_patterns".into())]),
|
||||
)]),
|
||||
));
|
||||
if d.other("minecraft:tooltip_display").is_none() {
|
||||
entries.push(("minecraft:tooltip_display", ominous_tooltip_display()));
|
||||
}
|
||||
entries.push((
|
||||
"minecraft:item_name",
|
||||
Tag::compound([(
|
||||
@@ -793,10 +799,8 @@ pub fn decode_components(components: Option<&Tag>) -> (u32, Option<Box<StackData
|
||||
for (k, v) in map {
|
||||
let key = k.to_string_lossy();
|
||||
let ominous_part = data.ominous_banner
|
||||
&& matches!(
|
||||
key.as_str(),
|
||||
"minecraft:item_name" | "minecraft:rarity" | "minecraft:tooltip_display"
|
||||
);
|
||||
&& (matches!(key.as_str(), "minecraft:item_name" | "minecraft:rarity")
|
||||
|| (key == "minecraft:tooltip_display" && v == &ominous_tooltip_display()));
|
||||
if !MODELLED.contains(&key.as_str()) && !ominous_part {
|
||||
data.other.push((key, v.clone()));
|
||||
}
|
||||
@@ -882,7 +886,19 @@ mod tests {
|
||||
let tag = encode_components(&banner).unwrap();
|
||||
assert!(tag.get("minecraft:banner_patterns").is_some());
|
||||
let (_, back) = decode_components(Some(&tag));
|
||||
assert!(back.unwrap().ominous_banner);
|
||||
let back = back.unwrap();
|
||||
assert!(back.ominous_banner);
|
||||
assert!(back.other("minecraft:tooltip_display").is_none());
|
||||
|
||||
banner.data_mut().set_other(
|
||||
"minecraft:tooltip_display",
|
||||
Some(Tag::Compound(Default::default())),
|
||||
);
|
||||
let (_, custom_display) = decode_components(encode_components(&banner).as_ref());
|
||||
assert_eq!(
|
||||
custom_display.unwrap().other("minecraft:tooltip_display"),
|
||||
Some(&Tag::Compound(Default::default()))
|
||||
);
|
||||
let mut bottle = ItemStack::new("minecraft:ominous_bottle", 1);
|
||||
bottle.data_mut().ominous_amplifier = Some(3);
|
||||
let (_, back) = decode_components(encode_components(&bottle).as_ref());
|
||||
|
||||
@@ -4,6 +4,64 @@ use crate::{
|
||||
hud::rgba,
|
||||
items::{ItemInfo, ItemStack},
|
||||
};
|
||||
use horizon_world::nbt::Tag;
|
||||
|
||||
/// The `minecraft:tooltip_display` component applied by `ItemStack` while it
|
||||
/// builds a hover tooltip.
|
||||
pub struct TooltipDisplay<'a> {
|
||||
component: Option<&'a Tag>,
|
||||
ominous_banner: bool,
|
||||
}
|
||||
|
||||
impl<'a> TooltipDisplay<'a> {
|
||||
pub fn for_stack(stack: &'a ItemStack) -> Self {
|
||||
Self {
|
||||
component: stack
|
||||
.data()
|
||||
.and_then(|data| data.other("minecraft:tooltip_display")),
|
||||
ominous_banner: stack.data().is_some_and(|data| data.ominous_banner),
|
||||
}
|
||||
}
|
||||
|
||||
fn hides_tooltip(&self) -> bool {
|
||||
matches!(
|
||||
self.component.and_then(|display| display.get("hide_tooltip")),
|
||||
Some(Tag::Byte(value)) if *value != 0
|
||||
)
|
||||
}
|
||||
|
||||
/// `ItemStack.getTooltipLines`: `hide_tooltip` suppresses the name unless
|
||||
/// the tooltip flag is creative.
|
||||
pub fn name_visible(&self, creative: bool) -> bool {
|
||||
creative || !self.hides_tooltip()
|
||||
}
|
||||
|
||||
/// `TooltipDisplay.shows`: whether details for a data component are shown.
|
||||
pub fn shows(&self, component: &str) -> bool {
|
||||
if self.hides_tooltip() {
|
||||
return false;
|
||||
}
|
||||
// `item_data::decode_components` models the raid captain banner's
|
||||
// default hidden pattern tooltip as `ominous_banner` instead of
|
||||
// retaining its redundant `tooltip_display` component.
|
||||
if self.ominous_banner
|
||||
&& self.component.is_none()
|
||||
&& component == "minecraft:banner_patterns"
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let Some(Tag::List(hidden)) = self
|
||||
.component
|
||||
.and_then(|display| display.get("hidden_components"))
|
||||
else {
|
||||
return true;
|
||||
};
|
||||
let component = horizon_world::nbt::JavaString::from(component);
|
||||
!hidden
|
||||
.iter()
|
||||
.any(|tag| matches!(tag, Tag::String(id) if id == &component))
|
||||
}
|
||||
}
|
||||
|
||||
/// `EquipmentSlotGroup` in declaration order, the order tooltips list them in.
|
||||
const SLOT_GROUPS: [&str; 11] = [
|
||||
@@ -348,6 +406,49 @@ pub fn lines(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn tooltip_display_controls_name_and_component_visibility() {
|
||||
let mut stack = ItemStack::new("minecraft:diamond_sword", 1);
|
||||
let display = TooltipDisplay::for_stack(&stack);
|
||||
assert!(display.name_visible(false));
|
||||
assert!(display.shows("minecraft:attribute_modifiers"));
|
||||
|
||||
stack.data_mut().set_other(
|
||||
"minecraft:tooltip_display",
|
||||
Some(Tag::compound([(
|
||||
"hidden_components",
|
||||
Tag::List(vec![Tag::String("minecraft:attribute_modifiers".into())]),
|
||||
)])),
|
||||
);
|
||||
let display = TooltipDisplay::for_stack(&stack);
|
||||
assert!(display.name_visible(false));
|
||||
assert!(!display.shows("minecraft:attribute_modifiers"));
|
||||
assert!(display.shows("minecraft:enchantments"));
|
||||
|
||||
stack.data_mut().set_other(
|
||||
"minecraft:tooltip_display",
|
||||
Some(Tag::compound([("hide_tooltip", Tag::Byte(1))])),
|
||||
);
|
||||
let display = TooltipDisplay::for_stack(&stack);
|
||||
assert!(!display.name_visible(false));
|
||||
assert!(display.name_visible(true));
|
||||
assert!(!display.shows("minecraft:attribute_modifiers"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ominous_banner_keeps_its_default_hidden_pattern_tooltip() {
|
||||
let mut banner = crate::mobs::raider::ominous_banner();
|
||||
let display = TooltipDisplay::for_stack(&banner);
|
||||
assert!(!display.shows("minecraft:banner_patterns"));
|
||||
|
||||
banner.data_mut().set_other(
|
||||
"minecraft:tooltip_display",
|
||||
Some(Tag::Compound(Default::default())),
|
||||
);
|
||||
let display = TooltipDisplay::for_stack(&banner);
|
||||
assert!(display.shows("minecraft:banner_patterns"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ominous_banner_name_and_rarity_use_the_stack() {
|
||||
let translate = |key: &str| match key {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import net.minecraft.core.component.DataComponentType;
|
||||
import net.minecraft.core.component.DataComponents;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.item.TooltipFlag;
|
||||
import net.minecraft.world.item.component.TooltipDisplay;
|
||||
|
||||
/** The name and attribute lines returned for a diamond sword's tooltip. */
|
||||
public final class VanillaTooltipDisplayOracle {
|
||||
public static void main(String[] args) throws Exception {
|
||||
ParityData.init();
|
||||
var context = Item.TooltipContext.of(ParityData.lookup);
|
||||
var output = new ArrayList<String>();
|
||||
for (String line : Files.readAllLines(Path.of(args[0]))) {
|
||||
String[] fields = line.split("\t");
|
||||
boolean hideTooltip = Boolean.parseBoolean(fields[0]);
|
||||
boolean creative = Boolean.parseBoolean(fields[1]);
|
||||
boolean hideAttributes = Boolean.parseBoolean(fields[2]);
|
||||
var hidden = new LinkedHashSet<DataComponentType<?>>();
|
||||
if (hideAttributes) hidden.add(DataComponents.ATTRIBUTE_MODIFIERS);
|
||||
var stack = new ItemStack(Items.DIAMOND_SWORD, 1);
|
||||
stack.set(DataComponents.TOOLTIP_DISPLAY, new TooltipDisplay(hideTooltip, hidden));
|
||||
TooltipFlag flag = creative ? TooltipFlag.Default.NORMAL.asCreative() : TooltipFlag.Default.NORMAL;
|
||||
var tooltip = stack.getTooltipLines(context, null, flag);
|
||||
output.add((!tooltip.isEmpty()) + "\t" + (tooltip.size() > 1));
|
||||
}
|
||||
Files.write(Path.of(args[1]), output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare item tooltip display visibility with the pinned vanilla client."""
|
||||
try:
|
||||
from . import lab, oracle_harness as harness
|
||||
except ImportError:
|
||||
import lab, oracle_harness as harness
|
||||
|
||||
|
||||
def cases(build):
|
||||
values = [
|
||||
(False, False, False),
|
||||
(False, False, True),
|
||||
(False, True, True),
|
||||
(True, False, False),
|
||||
(True, True, False),
|
||||
(True, True, True),
|
||||
]
|
||||
path = build / 'cases.txt'
|
||||
path.write_text('\n'.join('\t'.join(map(str, row)) for row in values) + '\n')
|
||||
return path, len(values)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
harness.differential(
|
||||
'tooltip-display-parity', 'VanillaTooltipDisplayOracle', 'tooltip_display_fixture', cases,
|
||||
'Diamond sword name and attribute tooltip visibility for hide_tooltip, hidden_components, and creative flags',
|
||||
sources=[
|
||||
lab.ROOT / 'crates/horizon-client/src/game/stations.rs',
|
||||
lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs',
|
||||
],
|
||||
expected_lines=6)
|
||||
Reference in New Issue
Block a user