Pumpkin/pumpkin-protocol/src/client/play/c_command_suggestions.rs
Alexander Medvedev 03e81a1436 transfer packet id parsing to build time
This also allows now to create custom packets with a custom packet id which is not in the packets.json
2025-01-10 13:42:39 +01:00

57 lines
1.5 KiB
Rust

use bytes::BufMut;
use pumpkin_data::packet::clientbound::PLAY_COMMAND_SUGGESTIONS;
use pumpkin_macros::client_packet;
use pumpkin_util::text::TextComponent;
use crate::{bytebuf::ByteBufMut, ClientPacket, VarInt};
#[client_packet(PLAY_COMMAND_SUGGESTIONS)]
pub struct CCommandSuggestions {
id: VarInt,
start: VarInt,
length: VarInt,
matches: Vec<CommandSuggestion>,
}
impl CCommandSuggestions {
pub fn new(id: VarInt, start: VarInt, length: VarInt, matches: Vec<CommandSuggestion>) -> Self {
Self {
id,
start,
length,
matches,
}
}
}
impl ClientPacket for CCommandSuggestions {
fn write(&self, bytebuf: &mut impl BufMut) {
bytebuf.put_var_int(&self.id);
bytebuf.put_var_int(&self.start);
bytebuf.put_var_int(&self.length);
bytebuf.put_list(&self.matches, |bytebuf, suggestion| {
bytebuf.put_string(&suggestion.suggestion);
bytebuf.put_bool(suggestion.tooltip.is_some());
if let Some(tooltip) = &suggestion.tooltip {
bytebuf.put_slice(&tooltip.encode());
}
})
}
}
#[derive(PartialEq, Eq, Hash, Debug)]
pub struct CommandSuggestion {
pub suggestion: String,
pub tooltip: Option<TextComponent>,
}
impl CommandSuggestion {
pub fn new(suggestion: String, tooltip: Option<TextComponent>) -> Self {
Self {
suggestion,
tooltip,
}
}
}