Add option to use Minecraft server MOTD

This commit is contained in:
timvisee 2021-11-11 11:37:08 +01:00
parent 5df8c65b44
commit 4d51113bd9
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
3 changed files with 49 additions and 29 deletions

View File

@ -60,6 +60,9 @@ command = "java -Xmx1G -Xms1G -jar server.jar --nogui"
#motd_starting = "§2☻ Server is starting...\n§7⌛ Please wait..." #motd_starting = "§2☻ Server is starting...\n§7⌛ Please wait..."
#motd_stopping = "☠ Server going to sleep...\n⌛ 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 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_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." #login_stopping = "Server is going to sleep... §7☠§r\n\nPlease try to reconnect in a minute to wake it again."

View File

@ -193,6 +193,9 @@ pub struct Messages {
/// MOTD when server is stopping. /// MOTD when server is stopping.
pub motd_stopping: String, pub motd_stopping: String,
/// Use MOTD from Minecraft server once known.
pub use_server_motd: bool,
/// Login message when server is starting. /// Login message when server is starting.
pub login_starting: String, 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_sleeping: "☠ Server is sleeping\n§2☻ Join to start it up".into(),
motd_starting: "§2☻ Server is starting...\n§7⌛ Please wait...".into(), motd_starting: "§2☻ Server is starting...\n§7⌛ Please wait...".into(),
motd_stopping: "☠ Server going to sleep...\n⌛ 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_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(), login_stopping: "Server is going to sleep... §7☠§r\n\nPlease try to reconnect in a minute to wake it again.".into(),
} }

View File

@ -76,35 +76,7 @@ pub async fn serve(
// Hijack server status packet // Hijack server status packet
if client_state == ClientState::Status && packet.id == proto::STATUS_PACKET_ID_STATUS { if client_state == ClientState::Status && packet.id == proto::STATUS_PACKET_ID_STATUS {
// Select version and player max from last known server status let server_status = server_status(&config, &server);
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 packet = StatusResponse { server_status }; let packet = StatusResponse { server_status };
let mut data = Vec::new(); 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()?; let response = RawPacket::new(0, data).encode()?;
writer.write_all(&response).await.map_err(|_| ()) 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![],
},
}
}