feat(config): add Configuration for advancement saving (#2259)

* implementing the advancement configuration for saving or not the advancements

* adding a test for not loading files when save is disabled

* cargo fmt

* fix merge

* Fix variable name for players directory
This commit is contained in:
kedor
2026-07-05 16:36:36 +02:00
committed by GitHub
parent da2401d07d
commit 560a0ed77d
4 changed files with 49 additions and 2 deletions

View File

@@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};
/// Configuration for advancements
///
/// Controls whether the advancements should be saved and loaded
#[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct AdvancementConfig {
/// Whether saving advancements is enabled.
pub save_advancements: bool,
}
impl Default for AdvancementConfig {
fn default() -> Self {
Self {
save_advancements: true,
}
}
}

View File

@@ -34,12 +34,14 @@ pub mod chunk;
pub mod lighting;
pub mod op;
mod advancement;
mod player_data;
mod pvp;
mod server_links;
pub mod whitelist;
pub mod world;
use advancement::AdvancementConfig;
use networking::NetworkingConfig;
use player_data::PlayerDataConfig;
use resource_pack::ResourcePackConfig;
@@ -98,6 +100,8 @@ pub struct AdvancedConfiguration {
pub recipe: RecipeConfig,
/// Plugin-related configuration.
pub plugins: PluginsConfig,
/// Advancement configuration
pub advancement: AdvancementConfig,
}
/// Basic configuration for core server settings.

View File

@@ -282,7 +282,7 @@ impl PlayerAdvancement {
/// Loads the player's advancement progress from disk.
pub async fn load(&mut self) -> Result<(), AdvancementDataError> {
if !self.path.exists() {
if !self.path.exists() || !self.is_save_enabled() {
return Ok(());
}
@@ -777,4 +777,26 @@ mod tests {
serde_json::from_str(&content).unwrap();
assert_eq!(saved_data.len(), 2, "Should have saved both advancements");
}
#[tokio::test]
async fn ignore_loading() {
let temp_dir = tempdir().unwrap();
let manager = Arc::new(AdvancementManager::new(temp_dir.path(), false));
let id = Uuid::new_v4();
let mut pa = PlayerAdvancement::new(manager, id);
// Create a JSON file with advancement data
let adv = Advancement::STORY_ROOT;
let data = serde_json::json!({ adv.id.to_string(): { "complete": true } });
std::fs::write(&pa.path, data.to_string()).unwrap();
//try load the file
assert!(pa.load().await.is_ok(), "Load should succeed");
// Verify that the advancement was not loaded
assert!(
pa.progress.is_empty(),
"The advancement shouldn't have been loaded"
);
}
}

View File

@@ -204,7 +204,10 @@ impl Server {
Duration::from_secs(advanced_config.player_data.save_player_cron_interval),
advanced_config.player_data.save_player_data,
);
let advancement_manager = Arc::new(AdvancementManager::new(players_dir.clone(), true));
let advancement_manager = Arc::new(AdvancementManager::new(
players_dir.clone(),
advanced_config.advancement.save_advancements,
));
let white_list = AtomicBool::new(basic_config.white_list);
let tick_rate_manager = Arc::new(ServerTickRateManager::new(basic_config.tps));