From c3313626bed737042726eddf77dd3bd91dce583f Mon Sep 17 00:00:00 2001 From: Zoey Date: Wed, 24 Apr 2024 23:27:00 -0700 Subject: [PATCH] add command to save the level without stopping --- src/command.rs | 12 ++++++++++++ src/level.rs | 3 +++ src/server.rs | 6 ++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/command.rs b/src/command.rs index fa6423d..f90c7e5 100644 --- a/src/command.rs +++ b/src/command.rs @@ -19,6 +19,7 @@ const CMD_ALLOWENTRY: &str = "allowentry"; const CMD_SETPASS: &str = "setpass"; const CMD_SETLEVELSPAWN: &str = "setlevelspawn"; const CMD_WEATHER: &str = "weather"; +const CMD_SAVE: &str = "save"; /// list of commands available on the server pub const COMMANDS_LIST: &[&str] = &[ @@ -33,6 +34,7 @@ pub const COMMANDS_LIST: &[&str] = &[ CMD_SETPASS, CMD_SETLEVELSPAWN, CMD_WEATHER, + CMD_SAVE, ]; /// enum for possible commands @@ -74,6 +76,8 @@ pub enum Command<'m> { SetLevelSpawn, /// changes the levels weather Weather { weather_type: &'m str }, + /// saves the current level + Save, } impl<'m> Command<'m> { @@ -128,6 +132,7 @@ impl<'m> Command<'m> { CMD_WEATHER => Self::Weather { weather_type: arguments, }, + CMD_SAVE => Self::Save, _ => return Err(format!("Unknown command: {command_name}")), }) } @@ -146,6 +151,7 @@ impl<'m> Command<'m> { Self::SetPass { .. } => CMD_SETPASS, Self::SetLevelSpawn => CMD_SETLEVELSPAWN, Self::Weather { .. } => CMD_WEATHER, + Self::Save => CMD_SAVE, } } @@ -211,6 +217,7 @@ impl<'m> Command<'m> { c(""), "&fSets the level's weather.".to_string(), ], + CMD_SAVE => vec![c(""), "&fSaves the current level.".to_string()], _ => vec!["&eUnknown command!".to_string()], } } @@ -497,6 +504,11 @@ impl<'m> Command<'m> { messages.push(format!("&cUnknown weather type {weather_type}!")); } } + + Command::Save => { + data.level.save_now = true; + messages.push("Saving level...".to_string()); + } } messages diff --git a/src/level.rs b/src/level.rs index b595a2d..1153391 100644 --- a/src/level.rs +++ b/src/level.rs @@ -37,6 +37,8 @@ pub struct Level { /// list of updates to apply to the world on the next tick #[serde(skip)] pub updates: Vec, + #[serde(skip)] + pub save_now: bool, } impl Level { @@ -50,6 +52,7 @@ impl Level { weather: WeatherType::Sunny, awaiting_update: Default::default(), updates: Default::default(), + save_now: false, } } diff --git a/src/server.rs b/src/server.rs index 0040bf9..70b3193 100644 --- a/src/server.rs +++ b/src/server.rs @@ -157,9 +157,11 @@ async fn handle_ticks(data: Arc>) { break; } - if data.config.auto_save_minutes != 0 - && last_auto_save.elapsed().as_secs() / 60 >= data.config.auto_save_minutes + if data.level.save_now + || (data.config.auto_save_minutes != 0 + && last_auto_save.elapsed().as_secs() / 60 >= data.config.auto_save_minutes) { + data.level.save_now = false; data.level .save(PathBuf::from(LEVELS_PATH).join(&data.config.level_name)) .await