Skip to main content

steel_protocol/packets/game/
c_command_suggestions.rs

1use steel_macros::{ClientPacket, WriteTo};
2use steel_registry::packets::play::C_COMMAND_SUGGESTIONS;
3use text_components::TextComponent;
4
5/// Sent by the server in response to a command suggestion request.
6#[derive(ClientPacket, WriteTo, Clone, Debug)]
7#[packet_id(Play = C_COMMAND_SUGGESTIONS)]
8pub struct CCommandSuggestions {
9    /// Transaction ID matching the client's request.
10    #[write(as = VarInt)]
11    pub id: i32,
12    /// Start position in the command string where suggestions apply.
13    #[write(as = VarInt)]
14    pub start: i32,
15    /// Length of the text to be replaced by the suggestion.
16    #[write(as = VarInt)]
17    pub length: i32,
18    /// List of suggestion entries.
19    #[write(as = Prefixed(VarInt))]
20    pub suggestions: Vec<SuggestionEntry>,
21}
22
23/// A single command suggestion entry.
24#[derive(WriteTo, Clone, Debug)]
25pub struct SuggestionEntry {
26    /// The suggestion text to insert.
27    #[write(as = Prefixed(VarInt))]
28    pub text: String,
29    /// Optional tooltip shown when hovering over the suggestion.
30    pub tooltip: Option<TextComponent>,
31}
32
33impl SuggestionEntry {
34    /// Creates a new suggestion entry with just text.
35    pub fn new(text: impl Into<String>) -> Self {
36        Self {
37            text: text.into(),
38            tooltip: None,
39        }
40    }
41
42    /// Creates a new suggestion entry with text and tooltip.
43    pub fn with_tooltip(text: impl Into<String>, tooltip: impl Into<TextComponent>) -> Self {
44        Self {
45            text: text.into(),
46            tooltip: Some(tooltip.into()),
47        }
48    }
49}
50
51impl CCommandSuggestions {
52    /// Creates a new command suggestions response.
53    #[must_use]
54    pub const fn new(id: i32, start: i32, length: i32, suggestions: Vec<SuggestionEntry>) -> Self {
55        Self {
56            id,
57            start,
58            length,
59            suggestions,
60        }
61    }
62}