From 4860ac6abb16dc14b94b861fc6f2dbd7034d6c85 Mon Sep 17 00:00:00 2001 From: Zoey Date: Tue, 23 Apr 2024 22:12:00 -0700 Subject: [PATCH] add command for setting the level's spawnpoint for #7 --- src/command.rs | 10 ++++++++++ src/server/config.rs | 11 +++++++---- src/server/network.rs | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/command.rs b/src/command.rs index 77547d3..65a062c 100644 --- a/src/command.rs +++ b/src/command.rs @@ -9,6 +9,7 @@ const CMD_HELP: &str = "help"; const CMD_BAN: &str = "ban"; const CMD_ALLOWENTRY: &str = "allowentry"; const CMD_SETPASS: &str = "setpass"; +const CMD_SETLEVELSPAWN: &str = "setlevelspawn"; /// list of commands available on the server pub const COMMANDS_LIST: &[&str] = &[ @@ -21,6 +22,7 @@ pub const COMMANDS_LIST: &[&str] = &[ CMD_BAN, CMD_ALLOWENTRY, CMD_SETPASS, + CMD_SETLEVELSPAWN, ]; /// enum for possible commands @@ -58,6 +60,8 @@ pub enum Command<'m> { }, /// sets the current player's password SetPass { password: &'m str }, + /// sets the level spawn to the player's location + SetLevelSpawn, } impl<'m> Command<'m> { @@ -105,6 +109,7 @@ impl<'m> Command<'m> { CMD_SETPASS => Self::SetPass { password: arguments.trim(), }, + CMD_SETLEVELSPAWN => Self::SetLevelSpawn, _ => return Err(format!("Unknown command: {command_name}")), }) } @@ -121,6 +126,7 @@ impl<'m> Command<'m> { Self::Ban { .. } => CMD_BAN, Self::AllowEntry { .. } => CMD_ALLOWENTRY, Self::SetPass { .. } => CMD_SETPASS, + Self::SetLevelSpawn => CMD_SETLEVELSPAWN, } } @@ -178,6 +184,10 @@ impl<'m> Command<'m> { "&fAllows a player into the server.".to_string(), ], CMD_SETPASS => vec![c(""), "&fUpdates your password.".to_string()], + CMD_SETLEVELSPAWN => vec![ + c(""), + "&fSets the level's spawn to your location.".to_string(), + ], _ => vec!["&eUnknown command!".to_string()], } } diff --git a/src/server/config.rs b/src/server/config.rs index d24c68d..60fe4bf 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -66,11 +66,14 @@ pub struct ConfigCoordinates { } /// coordinates stored in config including orientation -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ConfigCoordinatesWithOrientation { - /// the inner coordinates - #[serde(flatten)] - pub coords: ConfigCoordinates, + /// the X coordinate + pub x: f32, + /// the Y coordinate + pub y: f32, + /// the Z coordinate + pub z: f32, /// the orientation's yaw pub yaw: u8, /// the orientation's pitch diff --git a/src/server/network.rs b/src/server/network.rs index 1d63779..1045746 100644 --- a/src/server/network.rs +++ b/src/server/network.rs @@ -16,7 +16,7 @@ use crate::{ client::ClientPacket, server::ServerPacket, PacketWriter, ARRAY_LENGTH, STRING_LENGTH, }, player::{Player, PlayerType}, - server::config::ServerProtectionMode, + server::config::{ConfigCoordinatesWithOrientation, ServerProtectionMode}, }; use super::ServerData; @@ -205,21 +205,15 @@ async fn handle_stream_inner( let (spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch) = if let Some(spawn) = &data.config.spawn { - ( - spawn.coords.x, - spawn.coords.y, - spawn.coords.z, - spawn.yaw, - spawn.pitch, - ) + (spawn.x, spawn.y, spawn.z, spawn.yaw, spawn.pitch) } else { - (16, data.level.y_size / 2 + 2, 16, 0, 0) + (16.5, (data.level.y_size / 2 + 2) as f32, 16.5, 0, 0) }; let (spawn_x, spawn_y, spawn_z) = ( - f16::from_f32(spawn_x as f32 + 0.5), - f16::from_f32(spawn_y as f32), - f16::from_f32(spawn_z as f32 + 0.5), + f16::from_f32(spawn_x), + f16::from_f32(spawn_y), + f16::from_f32(spawn_z), ); player.x = spawn_x; @@ -337,6 +331,18 @@ async fn handle_stream_inner( pitch, } => { let mut data = data.write().await; + + let player = data + .players + .iter_mut() + .find(|p| p.id == *own_id) + .expect("missing player"); + player.x = x; + player.y = y; + player.z = z; + player.yaw = yaw; + player.pitch = pitch; + spread_packet!( data, ServerPacket::SetPositionOrientation { @@ -603,6 +609,18 @@ async fn handle_stream_inner( msg!("&cServer must be set to per-user passwords!".to_string()); } } + + Command::SetLevelSpawn => { + data.config.spawn = Some(ConfigCoordinatesWithOrientation { + x: player.x.to_f32(), + y: player.y.to_f32(), + z: player.z.to_f32(), + yaw: player.yaw, + pitch: player.pitch + }); + data.config_needs_saving = true; + msg!("Level spawn updated!".to_string()); + } } } Err(msg) => {