Show ominous banner names and rarity in HUD and Creative tooltips

This commit is contained in:
Cosmic
2026-10-16 15:07:19 -04:00
parent 4218a496b2
commit 8f86098a02
5 changed files with 66 additions and 31 deletions
+5 -5
View File
@@ -7999,7 +7999,7 @@ impl Game<'_> {
self.creative_screen.search_focused.elapsed().as_millis(),
);
}
let mut hovered: Option<String> = None;
let mut hovered: Option<ItemStack> = None;
if tab.inventory {
self.draw_player_preview(
batch,
@@ -8049,7 +8049,7 @@ impl Game<'_> {
&& self.cursor.1 < y + 16.0
{
batch.fill(&self.res.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128));
hovered = Some(stack.item.to_string());
hovered = Some(stack.clone());
}
}
}
@@ -8064,7 +8064,7 @@ impl Game<'_> {
&& self.cursor.1 >= y
&& self.cursor.1 < y + 16.0
{
hovered = Some(stack.item.to_string());
hovered = Some(stack);
}
}
let scrollable = self.creative_screen.rows(&self.res.names) > 5;
@@ -8100,8 +8100,8 @@ impl Game<'_> {
} else if self.carried.is_none()
&& let Some(item) = hovered
{
let name = self.res.names.item(&item);
self.tooltip(batch, &name, self.cursor.0, self.cursor.1);
let lines = self.stack_tooltip(&item);
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
}
self.draw_inventory_effects(batch, (ox, oy), Creative::WIDTH as i32);
}
+8
View File
@@ -608,6 +608,14 @@ mod tests {
let mut dropped = ItemStack::new("minecraft:white_banner", 1);
pending.attach([pos.0, pos.1, pos.2], &mut dropped);
assert!(dropped.data().unwrap().ominous_banner);
assert_eq!(
horizon_client::item_tooltip::name_line(&dropped, &|_| None),
"Ominous Banner"
);
assert_eq!(
horizon_client::item_tooltip::rarity(&dropped, "common"),
"uncommon"
);
assert_eq!(
item_data::encode_components(&dropped),
Some(Tag::Compound(components.clone()))
+4 -12
View File
@@ -1860,18 +1860,10 @@ impl Game<'_> {
pub(super) fn stack_tooltip(&self, stack: &ItemStack) -> Vec<(String, u32)> {
let e: &Enchantments = &self.res.stations.enchantments;
let props = e.props(&stack.item);
let mut rarity = props.map_or("common", |p| p.rarity.as_str());
// The ominous banner's rarity component.
if stack.data().is_some_and(|d| d.ominous_banner) {
rarity = "uncommon";
}
if !stack.enchantments().is_empty() {
rarity = match rarity {
"common" | "uncommon" => "rare",
"rare" => "epic",
r => r,
};
}
let rarity = horizon_client::item_tooltip::rarity(
stack,
props.map_or("common", |p| p.rarity.as_str()),
);
let color = match rarity {
"uncommon" => rgba(255, 255, 85, 255),
"rare" => rgba(85, 255, 255, 255),
+7 -14
View File
@@ -6,6 +6,7 @@ use horizon_client::{
hud::HudBatch,
hud_layout,
hud_overlays::HeldName,
item_tooltip,
text::{self, Text},
};
@@ -24,22 +25,14 @@ impl Game<'_> {
fn held_name(&self) -> Option<HeldName> {
let stack = self.inventory.get(self.selected)?.as_ref()?;
let custom = stack.custom_name();
// ItemStack.getRarity: enchanting raises common and uncommon to rare
// and rare to epic.
let mut rarity = self.item_rarity(&stack.item);
if !stack.enchantments().is_empty() {
rarity = match rarity.as_str() {
"common" | "uncommon" => "rare".into(),
"rare" => "epic".into(),
_ => rarity,
};
}
let default_rarity = self.item_rarity(&stack.item);
let rarity = item_tooltip::rarity(stack, &default_rarity);
Some(HeldName {
item: stack.item.to_string(),
hover_name: Text::literal(
custom.map_or_else(|| self.res.names.item(&stack.item), str::to_owned),
),
rarity_color: match rarity.as_str() {
hover_name: Text::literal(item_tooltip::name_line(stack, &|key| {
self.res.names.get(key).map(str::to_owned)
})),
rarity_color: match rarity {
"uncommon" => text::YELLOW,
"rare" => text::AQUA,
"epic" => text::LIGHT_PURPLE,
+42
View File
@@ -102,6 +102,10 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option<String>)
if let Some(name) = stack.custom_name() {
return name.to_owned();
}
if stack.data().is_some_and(|d| d.ominous_banner) {
return translate("block.minecraft.ominous_banner")
.unwrap_or_else(|| "Ominous Banner".to_owned());
}
let key = if is_potion_item(&stack.item) {
Some(crate::brewing::potion_name_key(&stack.item, stack.potion()))
} else if let Some(pattern) = path.strip_suffix("_armor_trim_smithing_template") {
@@ -120,6 +124,24 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option<String>)
.unwrap_or_else(|| path.replace('_', " "))
}
/// ItemStack.getRarity: the ominous banner override, then the enchantment upgrade.
pub fn rarity<'a>(stack: &ItemStack, default: &'a str) -> &'a str {
let rarity = if stack.data().is_some_and(|d| d.ominous_banner) {
"uncommon"
} else {
default
};
if stack.enchantments().is_empty() {
rarity
} else {
match rarity {
"common" | "uncommon" => "rare",
"rare" => "epic",
r => r,
}
}
}
/// The lines of `stack` below its name that its components give, in
/// `ItemStack.addDetailsToTooltip` order.
pub fn detail_lines(
@@ -292,6 +314,26 @@ pub fn lines(
mod tests {
use super::*;
#[test]
fn ominous_banner_name_and_rarity_use_the_stack() {
let translate = |key: &str| match key {
"block.minecraft.ominous_banner" => Some("Ominous Banner".to_owned()),
"block.minecraft.white_banner" => Some("White Banner".to_owned()),
_ => None,
};
let mut banner = crate::mobs::raider::ominous_banner();
assert_eq!(name_line(&banner, &translate), "Ominous Banner");
assert_eq!(name_line(&banner, &|_| None), "Ominous Banner");
assert_eq!(rarity(&banner, "common"), "uncommon");
banner.data_mut().custom_name = Some("Captain's Standard".into());
assert_eq!(name_line(&banner, &translate), "Captain's Standard");
banner.enchant("minecraft:unbreaking", 1);
assert_eq!(rarity(&banner, "common"), "rare");
let plain = ItemStack::new("minecraft:white_banner", 1);
assert_eq!(name_line(&plain, &translate), "White Banner");
assert_eq!(rarity(&plain, "common"), "common");
}
#[test]
fn container_lines_list_five_stacks_then_how_many_more() {
let mut chest = ItemStack::new("minecraft:chest", 1);