Skip to main content

steel_registry/advancement/
display.rs

1use crate::ItemStackTemplate;
2use crate::advancement::registry::AdvancementRef;
3use serde::{Deserialize, Serialize};
4use std::io::Write;
5use steel_utils::Identifier;
6use steel_utils::codec::VarInt;
7use steel_utils::serial::WriteTo;
8use steel_utils::translations::{
9    CHAT_TYPE_ADVANCEMENT_CHALLENGE, CHAT_TYPE_ADVANCEMENT_GOAL, CHAT_TYPE_ADVANCEMENT_TASK,
10};
11use text_components::TextComponent;
12use text_components::format::Color;
13use text_components::translation::Translation;
14
15#[derive(Debug, Default)]
16pub struct DisplayInfo {
17    pub title: TextComponent,
18    pub description: TextComponent,
19    pub icon: ItemStackTemplate,
20    pub background: Option<Identifier>,
21    pub frame_type: AdvancementType,
22    pub show_toast: bool,
23    pub announce_chat: bool,
24    pub hidden: bool,
25}
26
27impl DisplayInfo {
28    #[must_use]
29    pub const fn has_background(&self) -> bool {
30        self.background.is_some()
31    }
32}
33
34impl WriteTo for DisplayInfo {
35    fn write(&self, writer: &mut impl Write) -> std::io::Result<()> {
36        self.title.write(writer)?;
37        self.description.write(writer)?;
38        self.icon.write(writer)?;
39        VarInt(self.frame_type as i32).write(writer)?;
40        let flags = i32::from(self.has_background())
41            | i32::from(self.show_toast) << 1
42            | i32::from(self.hidden) << 2;
43        flags.write(writer)?;
44        if let Some(background) = &self.background {
45            background.write(writer)?;
46        }
47        Ok(())
48    }
49}
50
51#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize)]
52pub enum AdvancementType {
53    #[default]
54    Task,
55    Challenge,
56    Goal,
57}
58
59impl AdvancementType {
60    #[must_use]
61    fn get_translated_text(&self) -> &Translation<2usize> {
62        match self {
63            Self::Task => &CHAT_TYPE_ADVANCEMENT_TASK,
64            Self::Challenge => &CHAT_TYPE_ADVANCEMENT_CHALLENGE,
65            Self::Goal => &CHAT_TYPE_ADVANCEMENT_GOAL,
66        }
67    }
68
69    #[must_use]
70    pub const fn color(&self) -> Color {
71        match self {
72            Self::Task => Color::Green,
73            Self::Challenge => Color::DarkPurple,
74            Self::Goal => Color::Green,
75        }
76    }
77
78    #[must_use]
79    pub fn create_announcement(
80        &self,
81        advancement: AdvancementRef,
82        player_name: TextComponent,
83    ) -> TextComponent {
84        TextComponent::translated(
85            self.get_translated_text()
86                .message([player_name, advancement.name()]),
87        )
88    }
89}
90
91impl WriteTo for AdvancementType {
92    fn write(&self, writer: &mut impl Write) -> std::io::Result<()> {
93        VarInt(*self as i32).write(writer)
94    }
95}