Skip to content

Commit

Permalink
Add new player to server list
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Oct 22, 2024
1 parent d9b3d04 commit 91ae85e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ impl Client {

pub async fn handle_status_request(&self, server: &Server, _status_request: SStatusRequest) {
log::debug!("Handling status request for id {}", self.id);
self.send_packet(&server.get_status()).await;
let status = server.get_status();
self.send_packet(&status.lock().await.get_status()).await;
}

pub async fn handle_ping_request(&self, ping_request: SStatusPingRequest) {
Expand Down
8 changes: 5 additions & 3 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async fn main() -> io::Result<()> {
ticker.run(&server).await;
});
}
let mut unique_id = 0;
let mut player_count = 0;
loop {
// Asynchronously wait for an inbound socket.
let (connection, address) = listener.accept().await?;
Expand All @@ -180,8 +180,8 @@ async fn main() -> io::Result<()> {
log::warn!("failed to set TCP_NODELAY {e}");
}

unique_id += 1;
let id = unique_id;
player_count += 1;
let id = player_count;

log::info!(
"Accepted connection from: {} (id: {})",
Expand Down Expand Up @@ -223,7 +223,9 @@ async fn main() -> io::Result<()> {
};
}
player.remove().await;
server.remove_player().await;
}
});
player_count -= 1;
}
}
38 changes: 31 additions & 7 deletions pumpkin/src/server/connection_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use base64::{engine::general_purpose, Engine as _};
use pumpkin_config::{BasicConfiguration, BASIC_CONFIG};
use pumpkin_protocol::{
client::{config::CPluginMessage, status::CStatusResponse},
Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL,
Players, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL,
};

use super::CURRENT_MC_VERSION;
Expand Down Expand Up @@ -39,7 +39,7 @@ fn load_icon_from_bytes(png_data: &[u8]) -> Result<String, Box<dyn error::Error>
}

pub struct CachedStatus {
_status_response: StatusResponse,
status_response: StatusResponse,
// We cache the json response here so we don't parse it every time someone makes a Status request.
// Keep in mind that we must parse this again, when the StatusResponse changes which usually happen when a player joins or leaves
status_response_json: String,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl CachedStatus {
.expect("Failed to parse Status response into JSON");

Self {
_status_response: status_response,
status_response,
status_response_json,
}
}
Expand All @@ -85,6 +85,27 @@ impl CachedStatus {
CStatusResponse::new(&self.status_response_json)
}

// TODO: Player samples
pub fn add_player(&mut self) {
let status_response = &mut self.status_response;
if let Some(players) = &mut status_response.players {
players.online += 1;
}

self.status_response_json = serde_json::to_string(&status_response)
.expect("Failed to parse Status response into JSON");
}

pub fn remove_player(&mut self) {
let status_response = &mut self.status_response;
if let Some(players) = &mut status_response.players {
players.online -= 1;
}

self.status_response_json = serde_json::to_string(&status_response)
.expect("Failed to parse Status response into JSON");
}

pub fn build_response(config: &BasicConfiguration) -> StatusResponse {
let icon = if config.use_favicon {
let icon_path = &config.favicon_path;
Expand Down Expand Up @@ -112,14 +133,17 @@ impl CachedStatus {
players: Some(Players {
max: config.max_players,
online: 0,
sample: vec![Sample {
name: String::new(),
id: String::new(),
}],
sample: vec![],
}),
description: config.motd.clone(),
favicon: icon,
enforce_secure_chat: false,
}
}
}

impl Default for CachedStatus {
fn default() -> Self {
Self::new()
}
}
21 changes: 16 additions & 5 deletions pumpkin/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use pumpkin_entity::EntityId;
use pumpkin_inventory::drag_handler::DragHandler;
use pumpkin_inventory::{Container, OpenContainer};
use pumpkin_protocol::client::login::CEncryptionRequest;
use pumpkin_protocol::client::status::CStatusResponse;
use pumpkin_protocol::{client::config::CPluginMessage, ClientPacket};
use pumpkin_registry::Registry;
use pumpkin_world::dimension::Dimension;
Expand Down Expand Up @@ -37,7 +36,7 @@ pub const CURRENT_MC_VERSION: &str = "1.21.1";

pub struct Server {
key_store: KeyStore,
server_listing: CachedStatus,
server_listing: Mutex<CachedStatus>,
server_branding: CachedBranding,

pub command_dispatcher: Arc<CommandDispatcher<'static>>,
Expand Down Expand Up @@ -89,7 +88,7 @@ impl Server {
command_dispatcher: Arc::new(command_dispatcher),
auth_client,
key_store: KeyStore::new(),
server_listing: CachedStatus::new(),
server_listing: Mutex::new(CachedStatus::new()),
server_branding: CachedBranding::new(),
}
}
Expand All @@ -106,9 +105,21 @@ impl Server {

let player = Arc::new(Player::new(client, world.clone(), entity_id, gamemode).await);
world.add_player(id, player.clone()).await;
// TODO: Config if we want increase online
if let Some(config) = player.client.config.lock().await.as_ref() {
// TODO: Config so we can also just ignore this hehe
if config.server_listing {
self.server_listing.lock().await.add_player();
}
}
(player, world.clone())
}

pub async fn remove_player(&self) {
// TODO: Config if we want increase online
self.server_listing.lock().await.remove_player();
}

pub async fn try_get_container(
&self,
player_id: EntityId,
Expand Down Expand Up @@ -151,8 +162,8 @@ impl Server {
self.server_branding.get_branding()
}

pub fn get_status(&self) -> CStatusResponse<'_> {
self.server_listing.get_status()
pub fn get_status(&self) -> &Mutex<CachedStatus> {
&self.server_listing
}

pub fn encryption_request<'a>(
Expand Down

0 comments on commit 91ae85e

Please sign in to comment.