valence_server/
title.rs

1use valence_protocol::encode::WritePacket;
2use valence_protocol::packets::play::{
3    ClearTitleS2c, OverlayMessageS2c, SubtitleS2c, TitleFadeS2c, TitleS2c,
4};
5use valence_protocol::text::IntoText;
6
7pub trait SetTitle {
8    /// Displays a title to a client.
9    ///
10    /// A title is a large piece of text displayed in the center of the screen
11    /// which may also include a subtitle underneath it. The title can be
12    /// configured to fade in and out using
13    /// [`set_title_times`](Self::set_title_times).
14    fn set_title<'a>(&mut self, text: impl IntoText<'a>);
15
16    fn set_subtitle<'a>(&mut self, text: impl IntoText<'a>);
17
18    fn set_action_bar<'a>(&mut self, text: impl IntoText<'a>);
19
20    /// - `fade_in`: Ticks to spend fading in.
21    /// - `stay`: Ticks to keep the title displayed.
22    /// - `fade_out`: Ticks to spend fading out.
23    fn set_title_times(&mut self, fade_in: i32, stay: i32, fade_out: i32);
24
25    fn clear_title(&mut self);
26
27    fn reset_title(&mut self);
28}
29
30impl<T: WritePacket> SetTitle for T {
31    fn set_title<'a>(&mut self, text: impl IntoText<'a>) {
32        self.write_packet(&TitleS2c {
33            title_text: text.into_cow_text(),
34        });
35    }
36
37    fn set_subtitle<'a>(&mut self, text: impl IntoText<'a>) {
38        self.write_packet(&SubtitleS2c {
39            subtitle_text: text.into_cow_text(),
40        });
41    }
42
43    fn set_action_bar<'a>(&mut self, text: impl IntoText<'a>) {
44        self.write_packet(&OverlayMessageS2c {
45            action_bar_text: text.into_cow_text(),
46        });
47    }
48
49    fn set_title_times(&mut self, fade_in: i32, stay: i32, fade_out: i32) {
50        self.write_packet(&TitleFadeS2c {
51            fade_in,
52            stay,
53            fade_out,
54        });
55    }
56
57    fn clear_title(&mut self) {
58        self.write_packet(&ClearTitleS2c { reset: false });
59    }
60
61    fn reset_title(&mut self) {
62        self.write_packet(&ClearTitleS2c { reset: true });
63    }
64}