draft: showing custom motd works

This commit is contained in:
2026-02-02 15:43:20 +01:00
parent f8a6534321
commit 67220e8d11
4 changed files with 2850 additions and 16 deletions

2809
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,3 +8,4 @@ rcon = { version = "0.6.0", features = ["rt-tokio"] }
tokio = { version = "1.49.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
toml = "0.8"
valence = "0.2.0-alpha.1+mc.1.20.1"

BIN
assets/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

@@ -1,5 +1,6 @@
use std::error::Error;
use std::fs;
use std::net::SocketAddr;
use std::time::Duration;
use rcon::Connection;
@@ -7,11 +8,18 @@ use serde::Deserialize;
use tokio::time::Instant;
use tokio::{io::copy_bidirectional, main};
use tokio::net::{TcpListener, TcpStream};
use valence::network::{
async_trait, BroadcastToLan, CleanupFn, ConnectionMode, HandshakeData, ServerListPing,
};
use valence::prelude::*;
use valence::MINECRAFT_VERSION;
#[derive(Debug, Deserialize)]
struct Config {
listen_addr: String,
server_addr: String,
motd_server_addr: String,
rcon_addr: String,
rcon_password: String,
idle_timeout_secs: u64,
@@ -26,6 +34,43 @@ impl Config {
}
}
struct MotdCallbacks;
#[async_trait]
impl NetworkCallbacks for MotdCallbacks {
async fn server_list_ping(
&self,
_shared: &SharedNetworkState,
_remote_addr: SocketAddr,
handshake_data: &HandshakeData,
) -> ServerListPing {
ServerListPing::Respond {
online_players: 0,
max_players: 0,
player_sample: vec![],
description: "Serwer jest ".into_text() +
"wyłączony".into_text().color(Color::rgb(250, 50, 50)) + "!\n" +
"Dołącz aby uruchomić serwer! ".into_text().color(Color::rgb(255, 150, 230)) +
format!("{}s", 20).into_text().color(Color::rgb(80, 80, 80)),
favicon_png: include_bytes!("../assets/icon.png"),
version_name: MINECRAFT_VERSION.to_string(),
protocol: handshake_data.protocol_version,
}
}
async fn broadcast_to_lan(&self, _shared: &SharedNetworkState) -> BroadcastToLan {
BroadcastToLan::Enabled("Hello Valence!".into())
}
async fn login(
&self,
_shared: &SharedNetworkState,
_info: &NewClientInfo,
) -> Result<CleanupFn, Text> {
Err("You are not meant to join this example".color(Color::rgb(250, 30, 21)))
}
}
fn parse_players_online(s: &str) -> Option<u32> {
s.split_whitespace()
.find_map(|tok| tok.parse::<u32>().ok())
@@ -35,8 +80,19 @@ fn parse_players_online(s: &str) -> Option<u32> {
async fn main() -> Result<(), Box<dyn Error>> {
let config = Config::load("config.toml")?;
App::new()
.insert_resource(NetworkSettings {
connection_mode: ConnectionMode::Offline,
callbacks: MotdCallbacks.into(),
address: config.motd_server_addr.parse().unwrap(),
..Default::default()
})
.add_plugins(DefaultPlugins)
.run();
println!("Listening on {}", config.listen_addr);
println!("Proxying to {}", config.server_addr);
println!("Motd server running on {}", config.motd_server_addr);
let listener = TcpListener::bind(&config.listen_addr).await?;
let server_addr = config.server_addr.clone();