add command to save the level without stopping

This commit is contained in:
Zoey 2024-04-24 23:27:00 -07:00
parent be81b8d581
commit c3313626be
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
3 changed files with 19 additions and 2 deletions

View file

@ -19,6 +19,7 @@ const CMD_ALLOWENTRY: &str = "allowentry";
const CMD_SETPASS: &str = "setpass"; const CMD_SETPASS: &str = "setpass";
const CMD_SETLEVELSPAWN: &str = "setlevelspawn"; const CMD_SETLEVELSPAWN: &str = "setlevelspawn";
const CMD_WEATHER: &str = "weather"; const CMD_WEATHER: &str = "weather";
const CMD_SAVE: &str = "save";
/// list of commands available on the server /// list of commands available on the server
pub const COMMANDS_LIST: &[&str] = &[ pub const COMMANDS_LIST: &[&str] = &[
@ -33,6 +34,7 @@ pub const COMMANDS_LIST: &[&str] = &[
CMD_SETPASS, CMD_SETPASS,
CMD_SETLEVELSPAWN, CMD_SETLEVELSPAWN,
CMD_WEATHER, CMD_WEATHER,
CMD_SAVE,
]; ];
/// enum for possible commands /// enum for possible commands
@ -74,6 +76,8 @@ pub enum Command<'m> {
SetLevelSpawn, SetLevelSpawn,
/// changes the levels weather /// changes the levels weather
Weather { weather_type: &'m str }, Weather { weather_type: &'m str },
/// saves the current level
Save,
} }
impl<'m> Command<'m> { impl<'m> Command<'m> {
@ -128,6 +132,7 @@ impl<'m> Command<'m> {
CMD_WEATHER => Self::Weather { CMD_WEATHER => Self::Weather {
weather_type: arguments, weather_type: arguments,
}, },
CMD_SAVE => Self::Save,
_ => return Err(format!("Unknown command: {command_name}")), _ => return Err(format!("Unknown command: {command_name}")),
}) })
} }
@ -146,6 +151,7 @@ impl<'m> Command<'m> {
Self::SetPass { .. } => CMD_SETPASS, Self::SetPass { .. } => CMD_SETPASS,
Self::SetLevelSpawn => CMD_SETLEVELSPAWN, Self::SetLevelSpawn => CMD_SETLEVELSPAWN,
Self::Weather { .. } => CMD_WEATHER, Self::Weather { .. } => CMD_WEATHER,
Self::Save => CMD_SAVE,
} }
} }
@ -211,6 +217,7 @@ impl<'m> Command<'m> {
c("<weather type>"), c("<weather type>"),
"&fSets the level's weather.".to_string(), "&fSets the level's weather.".to_string(),
], ],
CMD_SAVE => vec![c(""), "&fSaves the current level.".to_string()],
_ => vec!["&eUnknown command!".to_string()], _ => vec!["&eUnknown command!".to_string()],
} }
} }
@ -497,6 +504,11 @@ impl<'m> Command<'m> {
messages.push(format!("&cUnknown weather type {weather_type}!")); messages.push(format!("&cUnknown weather type {weather_type}!"));
} }
} }
Command::Save => {
data.level.save_now = true;
messages.push("Saving level...".to_string());
}
} }
messages messages

View file

@ -37,6 +37,8 @@ pub struct Level {
/// list of updates to apply to the world on the next tick /// list of updates to apply to the world on the next tick
#[serde(skip)] #[serde(skip)]
pub updates: Vec<BlockUpdate>, pub updates: Vec<BlockUpdate>,
#[serde(skip)]
pub save_now: bool,
} }
impl Level { impl Level {
@ -50,6 +52,7 @@ impl Level {
weather: WeatherType::Sunny, weather: WeatherType::Sunny,
awaiting_update: Default::default(), awaiting_update: Default::default(),
updates: Default::default(), updates: Default::default(),
save_now: false,
} }
} }

View file

@ -157,9 +157,11 @@ async fn handle_ticks(data: Arc<RwLock<ServerData>>) {
break; break;
} }
if data.config.auto_save_minutes != 0 if data.level.save_now
&& last_auto_save.elapsed().as_secs() / 60 >= data.config.auto_save_minutes || (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 data.level
.save(PathBuf::from(LEVELS_PATH).join(&data.config.level_name)) .save(PathBuf::from(LEVELS_PATH).join(&data.config.level_name))
.await .await