From 694c2f07d8c431a4a346439f10be625d2087e9f0 Mon Sep 17 00:00:00 2001 From: ari melody Date: Wed, 5 Nov 2025 01:58:05 +0000 Subject: [PATCH] improve query support simple string MOTDs; optional player sample --- src/status.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/status.rs b/src/status.rs index 3af55f6..65c6eb3 100644 --- a/src/status.rs +++ b/src/status.rs @@ -19,7 +19,7 @@ pub struct MinecraftPlayer { pub struct MinecraftPlayers { pub online: u32, pub max: u32, - pub sample: Vec, + pub sample: Option>, } #[derive(serde::Serialize, serde::Deserialize)] @@ -30,7 +30,14 @@ enum MinecraftDescriptionExtra { } #[derive(serde::Serialize, serde::Deserialize)] -pub struct MinecraftDescription { +#[serde(untagged)] +pub enum MinecraftDescription { + Rich(MinecraftRichDescription), + Plain(String) +} + +#[derive(serde::Serialize, serde::Deserialize)] +pub struct MinecraftRichDescription { text: String, extra: Option>, bold: Option, @@ -103,7 +110,6 @@ impl MinecraftStatus { } let msg = std::str::from_utf8(&data[offset..]).unwrap().trim(); let sanitised: String = msg.chars().filter(|&c| c >= '\u{20}' || c == '\n' || c == '\r' || c == '\t').collect(); - // println!("{sanitised}"); let status: MinecraftStatus = serde_json::from_slice(sanitised.as_bytes()).unwrap(); Ok(status) @@ -119,21 +125,28 @@ impl MinecraftStatus { } fn _description(description: &MinecraftDescription) -> String { - if description.extra.is_some() { - let mut extras = String::new(); - for extra in description.extra.as_ref().unwrap() { - match extra { - MinecraftDescriptionExtra::Extra(description) => { - extras += &_description(&description); - } - MinecraftDescriptionExtra::String(string) => { - extras += &string; + match description { + MinecraftDescription::Rich(description) => { + if description.extra.is_some() { + let mut extras = String::new(); + for extra in description.extra.as_ref().unwrap() { + match extra { + MinecraftDescriptionExtra::Extra(description) => { + extras += &_description(&description); + } + MinecraftDescriptionExtra::String(string) => { + extras += &string; + } + } } + return description.text.clone() + &extras; } + description.text.clone() + } + MinecraftDescription::Plain(description) => { + description.clone() } - return description.text.clone() + &extras; } - description.text.clone() } fn send_packet(stream: &mut TcpStream, data: &[u8]) -> Result<()> {