add command for setting the level's spawnpoint for #7

This commit is contained in:
Zoey 2024-04-23 22:12:00 -07:00
parent 87a2111751
commit 4860ac6abb
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
3 changed files with 47 additions and 16 deletions

View file

@ -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("<new password>"), "&fUpdates your password.".to_string()],
CMD_SETLEVELSPAWN => vec![
c(""),
"&fSets the level's spawn to your location.".to_string(),
],
_ => vec!["&eUnknown command!".to_string()],
}
}

View file

@ -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

View file

@ -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) => {