diff --git a/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs b/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs new file mode 100644 index 000000000..350d9be81 --- /dev/null +++ b/crates/horizon-client/examples/probe/lore_tooltip_fixture.rs @@ -0,0 +1,53 @@ +//! Lore tooltip companion for tools/test_lore_tooltip_parity.py: the spans +//! `item_tooltip::lore_texts` gives each stack (SNBT, one per line). +use horizon_client::{ + item_data::parse_stack, + item_tooltip::{TooltipDisplay, lore_texts}, +}; +use std::{error::Error, fmt::Write}; + +pub fn main() -> Result<(), Box> { + let args: Vec = crate::args().collect(); + let input = std::fs::read_to_string(&args[1])?; + let mut output = String::new(); + for line in input + .lines() + .filter(|l| !l.is_empty() && !l.starts_with('#')) + { + let stack = parse_stack(line)?.ok_or("empty stack")?; + let display = TooltipDisplay::for_stack(&stack); + if !display.shows("minecraft:lore") { + writeln!(output, "-")?; + continue; + } + let lines = lore_texts(&stack); + if lines.is_empty() { + writeln!(output, "-")?; + continue; + } + let mut rendered = Vec::new(); + for text in lines { + let mut spans = Vec::new(); + for span in &text.spans { + if span.text.is_empty() { + continue; + } + let style = &span.style; + spans.push(format!( + "{}\t{}\t{}\t{}\t{}\t{}\t{}", + span.text, + style.color.map(|c| c as i32).unwrap_or(-1), + style.bold == Some(true), + style.italic == Some(true), + style.underlined == Some(true), + style.strikethrough == Some(true), + style.obfuscated == Some(true), + )); + } + rendered.push(spans.join(" | ")); + } + writeln!(output, "{}", rendered.join(" || "))?; + } + std::fs::write(&args[2], output)?; + Ok(()) +} diff --git a/crates/horizon-client/examples/probe/main.rs b/crates/horizon-client/examples/probe/main.rs index f05c2a662..e80088fd0 100644 --- a/crates/horizon-client/examples/probe/main.rs +++ b/crates/horizon-client/examples/probe/main.rs @@ -49,6 +49,7 @@ mod input_probe; mod interaction_input_fixture; mod lan_probe; mod light_parity; +mod lore_tooltip_fixture; mod marker_particles_probe; mod menu_fixture; mod mipmap_probe; @@ -133,6 +134,7 @@ probes! { interaction_input_fixture, lan_probe, light_parity, + lore_tooltip_fixture, marker_particles_probe, menu_fixture, mipmap_probe, diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java new file mode 100644 index 000000000..462a8016a --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLoreTooltipOracle.java @@ -0,0 +1,58 @@ +package dev.horizonlab.parity; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Optional; +import net.minecraft.core.component.DataComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.Style; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.component.ItemLore; +import net.minecraft.world.item.component.TooltipDisplay; + +/** + * The styled lines {@code ItemLore.addToTooltip} adds, one stack (SNBT) per input + * line. Each line is its visited spans ({@code text, colour, bold, italic, + * underlined, strikethrough, obfuscated}); lines are separated by {@code ||}. + */ +public final class VanillaLoreTooltipOracle { + public static void main(String[] args) throws Exception { + ParityData.init(); + var context = Item.TooltipContext.of(ParityData.lookup); + var output = new ArrayList(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + if (line.isBlank() || line.startsWith("#")) continue; + var stack = ParityData.stack(line); + var display = stack.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT); + ItemLore lore = stack.get(DataComponents.LORE); + if (lore == null || !display.shows(DataComponents.LORE)) { + output.add("-"); + continue; + } + var lines = new ArrayList(); + lore.addToTooltip(context, component -> lines.add(spans(component)), TooltipFlag.NORMAL, stack); + output.add(lines.isEmpty() ? "-" : String.join(" || ", lines)); + } + Files.write(Path.of(args[1]), output); + } + + /** {@code Component.visit} from {@code Style.EMPTY}: the style a tooltip draws. */ + static String spans(Component component) { + var out = new StringBuilder(); + component.visit((style, text) -> { + if (text.isEmpty()) return Optional.empty(); + if (out.length() > 0) out.append(" | "); + int color = style.getColor() == null ? -1 : style.getColor().getValue(); + out.append(text).append('\t').append(color).append('\t') + .append(style.isBold()).append('\t') + .append(style.isItalic()).append('\t') + .append(style.isUnderlined()).append('\t') + .append(style.isStrikethrough()).append('\t') + .append(style.isObfuscated()); + return Optional.empty(); + }, Style.EMPTY); + return out.toString(); + } +} diff --git a/tools/test_lore_tooltip_parity.py b/tools/test_lore_tooltip_parity.py new file mode 100644 index 000000000..d262a3542 --- /dev/null +++ b/tools/test_lore_tooltip_parity.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Compare item lore tooltip styles with the pinned vanilla client. + +Vanilla prints what ``ItemLore.addToTooltip`` adds after ``ComponentUtils.mergeStyles`` +with dark purple italic (VanillaLoreTooltipOracle). Horizon prints what +``item_tooltip::lore_texts`` gives (examples/probe/lore_tooltip_fixture). +""" +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + +# SNBT stacks. A lore line is a text component; italic:0b is an explicit false. +CASES = [ + '{id:"minecraft:stick",count:1}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Plain"\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Gold","color":"gold","italic":0b}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"A","extra":[{"text":"B","color":"aqua","italic":0b}]}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"One"\',\'{"text":"Two","bold":1b}\']}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'"Hidden"\'],"minecraft:tooltip_display":{hidden_components:["minecraft:lore"]}}}', + '{id:"minecraft:stick",count:1,components:{"minecraft:lore":[\'{"text":"Line","underlined":1b,"strikethrough":1b}\']}}', +] + + +def cases(build): + path = build / 'cases.txt' + path.write_text(''.join(line + '\n' for line in CASES)) + return path, len(CASES) + + +if __name__ == '__main__': + harness.differential( + 'lore-tooltip-parity', 'VanillaLoreTooltipOracle', 'lore_tooltip_fixture', cases, + 'Item lore tooltip lines: dark purple italic, child colours, explicit non-italic, and tooltip_display', + sources=[ + lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs', + lab.ROOT / 'crates/horizon-client/src/item_data.rs', + lab.ROOT / 'crates/horizon-client/src/text.rs', + lab.ROOT / 'crates/horizon-client/src/component.rs', + ], + expected_lines=len(CASES))