56a4989560
ClientboundUpdateAdvancementsPacket and ClientboundAwardStatsPacket are new in the protocol crate, and the window applies them as vanilla's client does: - ClientAdvancements.update: a reset replaces the tree, added advancements join it with their place on the tab (DisplayInfo x, y), removed ones go, each changed one takes the progress sent, and one that is done in a non-reset update with show set gets its toast. The advancements screen lays a server's tree out where the server placed it and shows everything it sent. - handleAwardStats: the counters the server sends, by registry name. Opening the statistics screen asks for them (StatsScreen.init's REQUEST_STATS). Joining and leaving a server start from empty advancements and statistics. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
481 lines
18 KiB
Rust
481 lines
18 KiB
Rust
//! The Statistics screen (`StatsScreen`): General, Items and Mobs tabs over
|
|
//! the player's statistics, with the Done button.
|
|
use super::{Game, Screen};
|
|
use horizon_client::{hud::HudBatch, items::ItemStack, text::Text, ui};
|
|
use std::fmt::Write as _;
|
|
|
|
/// `Stats.CUSTOM` in registration order with its formatter.
|
|
const CUSTOM: &[(&str, Format)] = &[
|
|
("leave_game", Format::Default),
|
|
("play_time", Format::Time),
|
|
("total_world_time", Format::Time),
|
|
("time_since_death", Format::Time),
|
|
("time_since_rest", Format::Time),
|
|
("sneak_time", Format::Time),
|
|
("walk_one_cm", Format::Distance),
|
|
("crouch_one_cm", Format::Distance),
|
|
("sprint_one_cm", Format::Distance),
|
|
("walk_on_water_one_cm", Format::Distance),
|
|
("fall_one_cm", Format::Distance),
|
|
("climb_one_cm", Format::Distance),
|
|
("fly_one_cm", Format::Distance),
|
|
("walk_under_water_one_cm", Format::Distance),
|
|
("minecart_one_cm", Format::Distance),
|
|
("boat_one_cm", Format::Distance),
|
|
("pig_one_cm", Format::Distance),
|
|
("happy_ghast_one_cm", Format::Distance),
|
|
("horse_one_cm", Format::Distance),
|
|
("aviate_one_cm", Format::Distance),
|
|
("swim_one_cm", Format::Distance),
|
|
("strider_one_cm", Format::Distance),
|
|
("nautilus_one_cm", Format::Distance),
|
|
("jump", Format::Default),
|
|
("drop", Format::Default),
|
|
("damage_dealt", Format::Tenths),
|
|
("damage_dealt_absorbed", Format::Tenths),
|
|
("damage_dealt_resisted", Format::Tenths),
|
|
("damage_taken", Format::Tenths),
|
|
("damage_blocked_by_shield", Format::Tenths),
|
|
("damage_absorbed", Format::Tenths),
|
|
("damage_resisted", Format::Tenths),
|
|
("deaths", Format::Default),
|
|
("mob_kills", Format::Default),
|
|
("animals_bred", Format::Default),
|
|
("player_kills", Format::Default),
|
|
("fish_caught", Format::Default),
|
|
("talked_to_villager", Format::Default),
|
|
("traded_with_villager", Format::Default),
|
|
("eat_cake_slice", Format::Default),
|
|
("fill_cauldron", Format::Default),
|
|
("use_cauldron", Format::Default),
|
|
("clean_armor", Format::Default),
|
|
("clean_banner", Format::Default),
|
|
("clean_shulker_box", Format::Default),
|
|
("interact_with_brewingstand", Format::Default),
|
|
("interact_with_beacon", Format::Default),
|
|
("inspect_dropper", Format::Default),
|
|
("inspect_hopper", Format::Default),
|
|
("inspect_dispenser", Format::Default),
|
|
("play_noteblock", Format::Default),
|
|
("tune_noteblock", Format::Default),
|
|
("pot_flower", Format::Default),
|
|
("trigger_trapped_chest", Format::Default),
|
|
("open_enderchest", Format::Default),
|
|
("enchant_item", Format::Default),
|
|
("play_record", Format::Default),
|
|
("interact_with_furnace", Format::Default),
|
|
("interact_with_crafting_table", Format::Default),
|
|
("open_chest", Format::Default),
|
|
("sleep_in_bed", Format::Default),
|
|
("sleep_in_straw_bed", Format::Default),
|
|
("open_shulker_box", Format::Default),
|
|
("open_barrel", Format::Default),
|
|
("interact_with_blast_furnace", Format::Default),
|
|
("interact_with_smoker", Format::Default),
|
|
("interact_with_lectern", Format::Default),
|
|
("interact_with_campfire", Format::Default),
|
|
("interact_with_cartography_table", Format::Default),
|
|
("interact_with_loom", Format::Default),
|
|
("interact_with_stonecutter", Format::Default),
|
|
("bell_ring", Format::Default),
|
|
("raid_trigger", Format::Default),
|
|
("raid_win", Format::Default),
|
|
("interact_with_anvil", Format::Default),
|
|
("interact_with_grindstone", Format::Default),
|
|
("target_hit", Format::Default),
|
|
("interact_with_smithing_table", Format::Default),
|
|
];
|
|
#[derive(Clone, Copy)]
|
|
enum Format {
|
|
Default,
|
|
Tenths,
|
|
Distance,
|
|
Time,
|
|
}
|
|
fn two(v: f64) -> String {
|
|
format!("{v:.2}")
|
|
}
|
|
/// `StatFormatter`.
|
|
fn format(f: Format, v: i32) -> String {
|
|
match f {
|
|
Format::Default => {
|
|
// NumberFormat.getIntegerInstance(Locale.US): thousands separators.
|
|
let digits = v.unsigned_abs().to_string();
|
|
let mut out = String::new();
|
|
for (i, c) in digits.chars().enumerate() {
|
|
if i > 0 && (digits.len() - i).is_multiple_of(3) {
|
|
out.push(',');
|
|
}
|
|
out.push(c);
|
|
}
|
|
if v < 0 {
|
|
out.insert(0, '-');
|
|
}
|
|
out
|
|
}
|
|
Format::Tenths => two(f64::from(v) * 0.1),
|
|
Format::Distance => {
|
|
let m = f64::from(v) / 100.0;
|
|
let km = m / 1000.0;
|
|
if km > 0.5 {
|
|
format!("{} km", two(km))
|
|
} else if m > 0.5 {
|
|
format!("{} m", two(m))
|
|
} else {
|
|
format!("{v} cm")
|
|
}
|
|
}
|
|
Format::Time => {
|
|
let s = f64::from(v) / 20.0;
|
|
let (min, h) = (s / 60.0, s / 3600.0);
|
|
let (d, y) = (h / 24.0, h / 24.0 / 365.0);
|
|
if y > 0.5 {
|
|
format!("{} y", two(y))
|
|
} else if d > 0.5 {
|
|
format!("{} d", two(d))
|
|
} else if h > 0.5 {
|
|
format!("{} h", two(h))
|
|
} else if min > 0.5 {
|
|
format!("{} min", two(min))
|
|
} else {
|
|
// Java's double + " s".
|
|
let mut t = String::new();
|
|
let _ = write!(t, "{s:?}");
|
|
format!("{t} s")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// The item columns: (stat type, header sprite).
|
|
const COLUMNS: [(&str, &str); 6] = [
|
|
("mined", "gui/sprites/statistics/block_mined"),
|
|
("broken", "gui/sprites/statistics/item_broken"),
|
|
("crafted", "gui/sprites/statistics/item_crafted"),
|
|
("used", "gui/sprites/statistics/item_used"),
|
|
("picked_up", "gui/sprites/statistics/item_picked_up"),
|
|
("dropped", "gui/sprites/statistics/item_dropped"),
|
|
];
|
|
#[derive(Default)]
|
|
pub(super) struct StatsUi {
|
|
tab: usize,
|
|
scroll: f32,
|
|
sort: Option<(usize, i32)>,
|
|
}
|
|
/// `MenuTabBar` geometry: three tabs over at most 400 pixels.
|
|
fn tabs(w: f32) -> (f32, f32) {
|
|
let total = w.min(400.0) - 28.0;
|
|
let tab = ((total / 3.0) / 2.0).round() * 2.0;
|
|
let x0 = ((w - tab * 3.0) / 2.0).floor();
|
|
(x0, tab)
|
|
}
|
|
const TAB_H: f32 = 24.0;
|
|
const FOOTER: f32 = 33.0;
|
|
impl Game<'_> {
|
|
pub(super) fn open_stats(&mut self) {
|
|
self.load_stats();
|
|
// StatsScreen.init: a server's client asks for its statistics.
|
|
if self.remote.is_some() {
|
|
self.send(horizon_protocol::packets::play::Serverbound::ClientCommand(
|
|
horizon_protocol::packets::play::ClientCommand { action: 1 },
|
|
));
|
|
}
|
|
self.stats_ui = StatsUi::default();
|
|
self.screen = Screen::Stats;
|
|
}
|
|
fn stats_rows(&self) -> (f32, usize) {
|
|
match self.stats_ui.tab {
|
|
0 => (14.0, CUSTOM.len()),
|
|
1 => (22.0, self.stat_items().len() + 1),
|
|
_ => (36.0, self.stat_mobs().len()),
|
|
}
|
|
}
|
|
/// Items (and block items) with any non-zero column.
|
|
fn stat_items(&self) -> Vec<String> {
|
|
let mut items: Vec<String> = Vec::new();
|
|
for (ty, _) in COLUMNS {
|
|
if let Some(m) = self.stats.values.get(&format!("minecraft:{ty}")) {
|
|
for (k, v) in m {
|
|
if *v > 0 && k != "minecraft:air" && !items.contains(k) {
|
|
items.push(k.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if let Some((col, order)) = self.stats_ui.sort {
|
|
let ty = COLUMNS[col].0;
|
|
items.sort_by_key(|i| self.stats.get(ty, i) * -order);
|
|
}
|
|
items
|
|
}
|
|
fn stat_mobs(&self) -> Vec<String> {
|
|
let mut mobs: Vec<String> = Vec::new();
|
|
for ty in ["killed", "killed_by"] {
|
|
if let Some(m) = self.stats.values.get(&format!("minecraft:{ty}")) {
|
|
for (k, v) in m {
|
|
if *v > 0 && !mobs.contains(k) {
|
|
mobs.push(k.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
mobs
|
|
}
|
|
pub(super) fn draw_stats(&mut self, batch: &mut HudBatch) {
|
|
let (w, h) = (batch.width, batch.height);
|
|
let atlas = &self.atlas;
|
|
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true);
|
|
let (top, bottom) = (TAB_H, h - FOOTER);
|
|
ui::separators(batch, atlas, top, bottom, true);
|
|
// The tab bar.
|
|
let (x0, tw) = tabs(w);
|
|
for (i, key) in ["stat.generalButton", "stat.itemsButton", "stat.mobsButton"]
|
|
.into_iter()
|
|
.enumerate()
|
|
{
|
|
let x = x0 + tw * i as f32;
|
|
let selected = self.stats_ui.tab == i;
|
|
let hover = ui::hovered(self.cursor, x, 0.0, tw, TAB_H);
|
|
let sprite = match (selected, hover) {
|
|
(true, true) => "gui/sprites/widget/tab_selected_highlighted",
|
|
(true, false) => "gui/sprites/widget/tab_selected",
|
|
(false, true) => "gui/sprites/widget/tab_highlighted",
|
|
(false, false) => "gui/sprites/widget/tab",
|
|
};
|
|
ui::nine_slice(batch, atlas, sprite, x, 0.0, tw, TAB_H, u32::MAX);
|
|
let label = Text::translate(key, &[]);
|
|
let lw = atlas.font.width_text(&label);
|
|
let color = if selected { u32::MAX } else { 0xFFA0_A0A0 };
|
|
batch.text_styled(
|
|
atlas,
|
|
&label,
|
|
(x + (tw - lw) / 2.0).floor(),
|
|
8.0,
|
|
color,
|
|
true,
|
|
);
|
|
if selected {
|
|
batch.fill(atlas, x + 2.0, TAB_H - 3.0, tw - 4.0, 1.0, u32::MAX);
|
|
}
|
|
}
|
|
// The list.
|
|
let (row_h, rows) = self.stats_rows();
|
|
let content = row_h * rows as f32 + 4.0;
|
|
let max_scroll = (content - (bottom - top)).max(0.0);
|
|
self.stats_ui.scroll = self.stats_ui.scroll.clamp(0.0, max_scroll);
|
|
let left = ((w - 280.0) / 2.0).floor();
|
|
let scroll = self.stats_ui.scroll;
|
|
let row_y = |i: usize| top + 4.0 + i as f32 * row_h - scroll;
|
|
let visible = |y: f32| y >= top && y + row_h <= bottom;
|
|
match self.stats_ui.tab {
|
|
0 => {
|
|
let mut sorted: Vec<(Text, String)> = CUSTOM
|
|
.iter()
|
|
.map(|(k, f)| {
|
|
let label = Text::translate(&format!("stat.minecraft.{k}"), &[]);
|
|
let v = self.stats.get("custom", &format!("minecraft:{k}"));
|
|
(label, format(*f, v))
|
|
})
|
|
.collect();
|
|
sorted.sort_by_key(|(l, _)| l.plain());
|
|
for (i, (label, value)) in sorted.iter().enumerate() {
|
|
let y = row_y(i);
|
|
if !visible(y) {
|
|
continue;
|
|
}
|
|
let color = if i % 2 == 0 { u32::MAX } else { 0xFFBA_BABA };
|
|
let ty = y + (row_h - 8.0) / 2.0;
|
|
batch.text_styled(atlas, label, left + 2.0, ty.floor(), color, true);
|
|
let vw = HudBatch::text_width(atlas, value);
|
|
batch.text(
|
|
atlas,
|
|
value,
|
|
left + 280.0 - vw - 4.0,
|
|
ty.floor(),
|
|
color,
|
|
true,
|
|
);
|
|
}
|
|
}
|
|
1 => {
|
|
let items = self.stat_items();
|
|
if items.is_empty() {
|
|
return self.draw_stats_footer(batch, w, h);
|
|
}
|
|
// The header row of sort buttons.
|
|
let y = row_y(0);
|
|
if visible(y) {
|
|
for (c, (_, sprite)) in COLUMNS.iter().enumerate() {
|
|
let x = left + 75.0 + 40.0 * c as f32 - 18.0;
|
|
let hover = ui::hovered(self.cursor, x, y + 1.0, 18.0, 18.0);
|
|
let bg = if hover {
|
|
"gui/sprites/statistics/header"
|
|
} else {
|
|
"gui/sprites/container/slot"
|
|
};
|
|
batch.sprite(atlas, bg, x, y + 1.0);
|
|
batch.sprite(atlas, sprite, x, y + 1.0);
|
|
if let Some((sc, order)) = self.stats_ui.sort
|
|
&& sc == c
|
|
{
|
|
let s = if order < 0 {
|
|
"gui/sprites/statistics/sort_down"
|
|
} else {
|
|
"gui/sprites/statistics/sort_up"
|
|
};
|
|
batch.sprite(atlas, s, x, y + 1.0);
|
|
}
|
|
}
|
|
}
|
|
for (i, item) in items.iter().enumerate() {
|
|
let y = row_y(i + 1);
|
|
if !visible(y) {
|
|
continue;
|
|
}
|
|
let stack = ItemStack::new(item, 1);
|
|
batch.sprite(
|
|
&self.atlas,
|
|
"gui/sprites/container/slot",
|
|
left + 1.0,
|
|
y + 1.0,
|
|
);
|
|
self.draw_item(batch, &stack, left + 2.0, y + 2.0);
|
|
let atlas = &self.atlas;
|
|
let color = if (i + 1) % 2 == 0 {
|
|
u32::MAX
|
|
} else {
|
|
0xFFBA_BABA
|
|
};
|
|
for (c, (ty, _)) in COLUMNS.iter().enumerate() {
|
|
let x = left + 75.0 + 40.0 * c as f32;
|
|
let block =
|
|
c == 0 && self.res.items.get(item).is_some_and(|i| i.block.is_some());
|
|
let text = if c == 0 && !block {
|
|
Text::translate("stats.none", &[]).plain()
|
|
} else {
|
|
format(Format::Default, self.stats.get(ty, item))
|
|
};
|
|
let tw = HudBatch::text_width(atlas, &text);
|
|
batch.text(atlas, &text, x - tw, (y + 7.0).floor(), color, true);
|
|
}
|
|
}
|
|
}
|
|
_ => {
|
|
for (i, mob) in self.stat_mobs().iter().enumerate() {
|
|
let y = row_y(i);
|
|
if !visible(y) {
|
|
continue;
|
|
}
|
|
let name = Text::translate(&format!("entity.{}", mob.replace(':', ".")), &[]);
|
|
batch.text_styled(atlas, &name, left + 2.0, y + 1.0, u32::MAX, true);
|
|
let kills = self.stats.get("killed", mob);
|
|
let by = self.stats.get("killed_by", mob);
|
|
let k = if kills == 0 {
|
|
Text::translate(
|
|
"stat_type.minecraft.killed.none",
|
|
std::slice::from_ref(&name),
|
|
)
|
|
} else {
|
|
Text::translate(
|
|
"stat_type.minecraft.killed",
|
|
&[Text::literal(kills.to_string()), name.clone()],
|
|
)
|
|
};
|
|
let b = if by == 0 {
|
|
Text::translate(
|
|
"stat_type.minecraft.killed_by.none",
|
|
std::slice::from_ref(&name),
|
|
)
|
|
} else {
|
|
Text::translate(
|
|
"stat_type.minecraft.killed_by",
|
|
&[name.clone(), Text::literal(by.to_string())],
|
|
)
|
|
};
|
|
let (ck, cb) = (
|
|
if kills > 0 { 0xFFBA_BABA } else { 0xFF80_8080 },
|
|
if by > 0 { 0xFFBA_BABA } else { 0xFF80_8080 },
|
|
);
|
|
batch.text_styled(atlas, &k, left + 12.0, y + 10.0, ck, true);
|
|
batch.text_styled(atlas, &b, left + 12.0, y + 19.0, cb, true);
|
|
}
|
|
}
|
|
}
|
|
let atlas = &self.atlas;
|
|
ui::scrollbar(
|
|
batch,
|
|
atlas,
|
|
left + 280.0 + 6.0,
|
|
top,
|
|
bottom,
|
|
content,
|
|
self.stats_ui.scroll,
|
|
);
|
|
self.draw_stats_footer(batch, w, h);
|
|
}
|
|
fn draw_stats_footer(&self, batch: &mut HudBatch, w: f32, h: f32) {
|
|
let atlas = &self.atlas;
|
|
let (bx, by) = ((w / 2.0 - 100.0).floor(), h - FOOTER + 6.0);
|
|
let hover = ui::hovered(self.cursor, bx, by, 200.0, 20.0);
|
|
ui::button(
|
|
batch,
|
|
atlas,
|
|
bx,
|
|
by,
|
|
200.0,
|
|
20.0,
|
|
&Text::translate("gui.done", &[]),
|
|
true,
|
|
hover,
|
|
);
|
|
}
|
|
pub(super) fn stats_click(&mut self, size: [u32; 2]) {
|
|
let b = HudBatch::new(size);
|
|
let (w, h) = (b.width, b.height);
|
|
let (x0, tw) = tabs(w);
|
|
for i in 0..3 {
|
|
if ui::hovered(self.cursor, x0 + tw * i as f32, 0.0, tw, TAB_H)
|
|
&& self.stats_ui.tab != i
|
|
{
|
|
self.stats_ui.tab = i;
|
|
self.stats_ui.scroll = 0.0;
|
|
self.audio
|
|
.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
|
return;
|
|
}
|
|
}
|
|
if ui::hovered(
|
|
self.cursor,
|
|
(w / 2.0 - 100.0).floor(),
|
|
h - FOOTER + 6.0,
|
|
200.0,
|
|
20.0,
|
|
) {
|
|
self.audio
|
|
.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
|
self.screen = Screen::Paused;
|
|
return;
|
|
}
|
|
if self.stats_ui.tab == 1 {
|
|
let left = ((w - 280.0) / 2.0).floor();
|
|
let y = TAB_H + 4.0 - self.stats_ui.scroll;
|
|
for c in 0..COLUMNS.len() {
|
|
let x = left + 75.0 + 40.0 * c as f32 - 18.0;
|
|
if ui::hovered(self.cursor, x, y + 1.0, 18.0, 18.0) {
|
|
// sortByColumn: descending, ascending, off.
|
|
self.stats_ui.sort = match self.stats_ui.sort {
|
|
Some((sc, -1)) if sc == c => Some((c, 1)),
|
|
Some((sc, _)) if sc == c => None,
|
|
_ => Some((c, -1)),
|
|
};
|
|
self.audio
|
|
.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
pub(super) fn stats_scroll(&mut self, amount: f32) {
|
|
let (row_h, _) = self.stats_rows();
|
|
self.stats_ui.scroll -= amount * row_h / 2.0;
|
|
}
|
|
}
|