From 4d51113bd92364e89bf67ce40a1ca072511ea05e Mon Sep 17 00:00:00 2001 From: timvisee Date: Thu, 11 Nov 2021 11:37:08 +0100 Subject: [PATCH] Add option to use Minecraft server MOTD --- res/lazymc.toml | 3 +++ src/config.rs | 4 +++ src/status.rs | 71 +++++++++++++++++++++++++++++-------------------- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/res/lazymc.toml b/res/lazymc.toml index b889157..b24d0a9 100644 --- a/res/lazymc.toml +++ b/res/lazymc.toml @@ -60,6 +60,9 @@ command = "java -Xmx1G -Xms1G -jar server.jar --nogui" #motd_starting = "§2☻ Server is starting...\n§7⌛ Please wait..." #motd_stopping = "☠ Server going to sleep...\n⌛ Please wait..." +# Use MOTD from Minecraft server once known. +#use_server_motd = false + # Login messages, when user tries to connect. #login_starting = "Server is starting... §c♥§r\n\nThis may take some time.\n\nPlease try to reconnect in a minute." #login_stopping = "Server is going to sleep... §7☠§r\n\nPlease try to reconnect in a minute to wake it again." diff --git a/src/config.rs b/src/config.rs index c1bbbdc..78d2d6c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -193,6 +193,9 @@ pub struct Messages { /// MOTD when server is stopping. pub motd_stopping: String, + /// Use MOTD from Minecraft server once known. + pub use_server_motd: bool, + /// Login message when server is starting. pub login_starting: String, @@ -206,6 +209,7 @@ impl Default for Messages { motd_sleeping: "☠ Server is sleeping\n§2☻ Join to start it up".into(), motd_starting: "§2☻ Server is starting...\n§7⌛ Please wait...".into(), motd_stopping: "☠ Server going to sleep...\n⌛ Please wait...".into(), + use_server_motd: false, login_starting: "Server is starting... §c♥§r\n\nThis may take some time.\n\nPlease try to reconnect in a minute.".into(), login_stopping: "Server is going to sleep... §7☠§r\n\nPlease try to reconnect in a minute to wake it again.".into(), } diff --git a/src/status.rs b/src/status.rs index 9a687c1..8bdf02e 100644 --- a/src/status.rs +++ b/src/status.rs @@ -76,35 +76,7 @@ pub async fn serve( // Hijack server status packet if client_state == ClientState::Status && packet.id == proto::STATUS_PACKET_ID_STATUS { - // Select version and player max from last known server status - let (version, max) = match server.status().as_ref() { - Some(status) => (status.version.clone(), status.players.max), - None => ( - ServerVersion { - name: config.public.version.clone(), - protocol: config.public.protocol, - }, - 0, - ), - }; - - // Select description - let description = match server.state() { - server::State::Stopped | server::State::Started => &config.messages.motd_sleeping, - server::State::Starting => &config.messages.motd_starting, - server::State::Stopping => &config.messages.motd_stopping, - }; - - // Build status resposne - let server_status = ServerStatus { - version, - description: Message::new(Payload::text(description)), - players: OnlinePlayers { - online: 0, - max, - sample: vec![], - }, - }; + let server_status = server_status(&config, &server); let packet = StatusResponse { server_status }; let mut data = Vec::new(); @@ -259,3 +231,44 @@ async fn kick<'a>(msg: &str, writer: &mut WriteHalf<'a>) -> Result<(), ()> { let response = RawPacket::new(0, data).encode()?; writer.write_all(&response).await.map_err(|_| ()) } + +/// Build server status object to respond to client with. +fn server_status(config: &Config, server: &Server) -> ServerStatus { + let status = server.status(); + + // Select version and player max from last known server status + let (version, max) = match status.as_ref() { + Some(status) => (status.version.clone(), status.players.max), + None => ( + ServerVersion { + name: config.public.version.clone(), + protocol: config.public.protocol, + }, + 0, + ), + }; + + // Select description, use server MOTD if enabled, or use configured + let description = { + if config.messages.use_server_motd && status.is_some() { + status.as_ref().unwrap().description.clone() + } else { + Message::new(Payload::text(match server.state() { + server::State::Stopped | server::State::Started => &config.messages.motd_sleeping, + server::State::Starting => &config.messages.motd_starting, + server::State::Stopping => &config.messages.motd_stopping, + })) + } + }; + + // Build status resposne + ServerStatus { + version, + description, + players: OnlinePlayers { + online: 0, + max, + sample: vec![], + }, + } +}