diff --git a/crates/horizon-client/src/front.rs b/crates/horizon-client/src/front.rs index 828402944..e6224683f 100644 --- a/crates/horizon-client/src/front.rs +++ b/crates/horizon-client/src/front.rs @@ -154,6 +154,7 @@ impl Title { seed, creative, focus, + .. } => { let caret = (self.started.elapsed().as_millis() / 500).is_multiple_of(2); create_page(ui, x, w, name, seed, *creative, *focus, caret); @@ -379,14 +380,7 @@ impl Title { }; } } - CREATE => { - self.mode = Mode::Create { - name: "New World".into(), - seed: String::new(), - creative: false, - focus: 0, - } - } + CREATE => self.mode = Title::new_world(), DELETE_WORLD => { if let Some(world) = selected.and_then(|i| worlds.get(i)).cloned() { self.mode = Mode::Delete { world }; diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 6c456a71f..d4988a6ee 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5805,6 +5805,7 @@ impl Game<'_> { name: Option<&str>, seed: Option, creative: bool, + create: Option<&horizon_client::title::WorldOptions>, ) -> Result<()> { self.perf.world_opening(); let save_root = std::env::current_dir()?.join("saves").join(folder); @@ -5825,7 +5826,9 @@ impl Game<'_> { .map(|d| horizon_client::random_sequences::RandomSequences::from_nbt(&d)) .unwrap_or_default(); self.border = Default::default(); - let level = Level::read(&save_root).unwrap_or_else(|| Level { + let existing = Level::read(&save_root); + let created = existing.is_none(); + let level = existing.unwrap_or_else(|| Level { name: name.unwrap_or(folder).to_owned(), // WorldOptions.randomSeed when no seed was given. seed: seed.unwrap_or_else(|| { @@ -5842,6 +5845,13 @@ impl Game<'_> { ..Level::default() }); let mut level = level; + // CreateWorldScreen's choices, before streaming reads the generator. + if created && let Some(o) = create { + level.hardcore = o.hardcore; + level.difficulty = horizon_client::title::DIFFICULTIES[o.difficulty()].into(); + level.allow_commands = Some(o.commands(creative)); + horizon_client::save::write_world_gen_settings(&save_root, level.seed, o.world_gen())?; + } horizon_client::crash::set_detail("World", format!("{} (saves/{folder})", level.name)); if let Some(d) = &self.options.dimension { level.dimension = d.clone(); @@ -8795,13 +8805,18 @@ impl ApplicationHandler for Game<'_> { Action::Language => { self.open_options_at(true, horizon_client::options_ui::Page::Language) } - Action::Play { folder } => self.open_world(&folder, None, None, false)?, + Action::Play { folder } => { + self.open_world(&folder, None, None, false, None)? + } Action::Create { folder, name, seed, creative, - } => self.open_world(&folder, Some(&name), seed, creative)?, + options, + } => { + self.open_world(&folder, Some(&name), seed, creative, Some(&options))? + } Action::Connect { address } => { self.connect_server(&address, &self.options.player_name())? } @@ -9726,7 +9741,7 @@ pub fn run() -> Result<()> { } Some(folder) => { let (seed, creative) = (game.options.seed, game.options.creative); - game.open_world(&folder, None, Some(seed), creative)?; + game.open_world(&folder, None, Some(seed), creative, None)?; } None => { let title = Title::new( diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index af70b738a..243351af6 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -485,11 +485,19 @@ impl Game<'_> { seed, creative, focus, + options, } => json!({ "mode": "create-world", + "tab": (["game", "world", "more"][options.tab.min(2)]), "fields": { "name": name, "seed": seed }, "focus": match focus { 0 => "name", 1 => "seed", _ => "none" }, "creative": creative, + "hardcore": options.hardcore, + "difficulty": horizon_client::title::DIFFICULTIES[options.difficulty()], + "allow_commands": options.commands(*creative), + "superflat": options.flat, + "generate_structures": options.structures, + "bonus_chest": options.bonus_chest(), }), Mode::Edit { world, @@ -1322,10 +1330,17 @@ impl Game<'_> { let title = self.title.as_mut().ok_or("no title screen")?; if let Some(field) = &field { match &mut title.mode { - horizon_client::title::Mode::Create { focus, .. } => { + horizon_client::title::Mode::Create { focus, options, .. } => { + // Each box is on its own tab. *focus = match field.as_str() { - "name" => 0, - "seed" => 1, + "name" => { + options.tab = 0; + 0 + } + "seed" => { + options.tab = 1; + 1 + } other => { return Err(format!( "the create-world fields are name and seed, not {other}" diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index affa8d464..adaefacc5 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -488,7 +488,7 @@ impl Game<'_> { let folder = arg(1)?; let seed = words.get(2).map(|s| s.parse::()).transpose()?; let creative = words.get(3) == Some(&"creative"); - self.open_world(folder, Some(folder), seed, creative)?; + self.open_world(folder, Some(folder), seed, creative, None)?; ok() } "connect" => { diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index f4e410e2f..e73de6988 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -372,7 +372,13 @@ impl Game<'_> { let world_dir = std::env::current_dir()?.join(&universe).join(&level_name); let folder = world_dir.to_string_lossy().into_owned(); let new_world = !world_dir.join("level.dat").exists(); - self.open_world(&folder, Some(&level_name), Some(props.seed), game_mode == 1)?; + self.open_world( + &folder, + Some(&level_name), + Some(props.seed), + game_mode == 1, + None, + )?; if new_world { self.level.hardcore = props.hardcore; } diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 294b788b6..8bd8a6aa9 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -1922,6 +1922,145 @@ const INT_RULES: [&str; 11] = [ /// World and player state stored in `level.dat`, the saved-data files and /// the player file. +/// The world options CreateWorldScreen sets (`WorldOptions` and the world +/// preset) that `world_gen_settings` keeps. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct WorldGen { + /// The `minecraft:flat` preset (Superflat) instead of `normal`. + pub flat: bool, + pub structures: bool, + pub bonus_chest: bool, +} +impl WorldGen { + /// A saved world's choices, from its `world_gen_settings`. + pub fn read(world: &Path) -> Option { + let settings = world_gen_settings(world)?; + let flag = |key: &str, default: bool| number(settings.get(key)).map_or(default, |v| v != 0); + let generator = settings + .get("dimensions") + .and_then(|d| d.get("minecraft:overworld")) + .and_then(|o| o.get("generator")); + Some(Self { + flat: text(generator.and_then(|g| g.get("type"))) + .is_some_and(|t| t.trim_start_matches("minecraft:") == "flat"), + structures: flag("generate_structures", true), + bonus_chest: flag("bonus_chest", false), + }) + } +} +impl Default for WorldGen { + fn default() -> Self { + Self { + flat: false, + structures: true, + bonus_chest: false, + } + } +} +/// Writes a new world's `world_gen_settings` (seed, options and dimensions). +pub fn write_world_gen_settings(dir: &Path, seed: i64, options: WorldGen) -> Result<()> { + fs::create_dir_all(dir)?; + // worldgen/world_preset/flat.json's overworld: the classic flat layers. + let layer = |block: &str, height: i32| { + Tag::compound([("block", string(block)), ("height", Tag::Int(height))]) + }; + let flat = Tag::compound([ + ("type", string("minecraft:overworld")), + ( + "generator", + Tag::compound([ + ("type", string("minecraft:flat")), + ( + "settings", + Tag::compound([ + ("biome", string("minecraft:plains")), + ("features", Tag::Byte(0)), + ("lakes", Tag::Byte(0)), + ( + "layers", + Tag::List(vec![ + layer("minecraft:bedrock", 1), + layer("minecraft:dirt", 2), + layer("minecraft:grass_block", 1), + ]), + ), + ( + "structure_overrides", + Tag::List(vec![ + string("minecraft:strongholds"), + string("minecraft:villages"), + ]), + ), + ]), + ), + ]), + ), + ]); + let dimension = |kind: &str, settings: &str, source: Tag| { + Tag::compound([ + ("type", string(kind)), + ( + "generator", + Tag::compound([ + ("type", string("minecraft:noise")), + ("settings", string(settings)), + ("biome_source", source), + ]), + ), + ]) + }; + let preset = |p: &str| { + Tag::compound([ + ("type", string("minecraft:multi_noise")), + ("preset", string(p)), + ]) + }; + write_saved_data( + dir, + "world_gen_settings", + Tag::compound([ + ("seed", Tag::Long(seed)), + ( + "generate_structures", + Tag::Byte(i8::from(options.structures)), + ), + ("bonus_chest", Tag::Byte(i8::from(options.bonus_chest))), + ( + "dimensions", + Tag::compound([ + ( + "minecraft:overworld", + if options.flat { + flat + } else { + dimension( + "minecraft:overworld", + "minecraft:overworld", + preset("minecraft:overworld"), + ) + }, + ), + ( + "minecraft:the_nether", + dimension( + "minecraft:the_nether", + "minecraft:nether", + preset("minecraft:nether"), + ), + ), + ( + "minecraft:the_end", + dimension( + "minecraft:the_end", + "minecraft:end", + Tag::compound([("type", string("minecraft:the_end"))]), + ), + ), + ]), + ), + ]), + ) +} #[derive(Clone, Debug, Default)] pub struct Level { pub name: String, @@ -1964,6 +2103,9 @@ pub struct Level { pub world_spawn: Option<[i32; 3]>, pub difficulty: String, pub hardcore: bool, + /// `allowCommands` chosen on the Create New World screen; unset keeps + /// the saved value (off for a new world). + pub allow_commands: Option, /// Game rule values by name without namespace (`keep_inventory`, ...). pub game_rules: BTreeMap, pub player_uuid: Option<[i32; 4]>, @@ -2524,6 +2666,9 @@ impl Level { } if let Tag::Compound(map) = &mut data { map.entry("allowCommands".into()).or_insert(Tag::Byte(0)); + if let Some(allow) = self.allow_commands { + map.insert("allowCommands".into(), Tag::Byte(i8::from(allow))); + } map.entry("WasModded".into()).or_insert(Tag::Byte(0)); map.entry("DataPacks".into()).or_insert_with(|| { Tag::compound([ @@ -2567,63 +2712,7 @@ impl Level { } write_saved_data(dir, "world_clocks", clocks)?; if legacy || !saved_data_path(dir, "world_gen_settings").exists() { - let dimension = |kind: &str, settings: &str, source: Tag| { - Tag::compound([ - ("type", string(kind)), - ( - "generator", - Tag::compound([ - ("type", string("minecraft:noise")), - ("settings", string(settings)), - ("biome_source", source), - ]), - ), - ]) - }; - let preset = |p: &str| { - Tag::compound([ - ("type", string("minecraft:multi_noise")), - ("preset", string(p)), - ]) - }; - write_saved_data( - dir, - "world_gen_settings", - Tag::compound([ - ("seed", Tag::Long(self.seed)), - ("generate_structures", Tag::Byte(1)), - ("bonus_chest", Tag::Byte(0)), - ( - "dimensions", - Tag::compound([ - ( - "minecraft:overworld", - dimension( - "minecraft:overworld", - "minecraft:overworld", - preset("minecraft:overworld"), - ), - ), - ( - "minecraft:the_nether", - dimension( - "minecraft:the_nether", - "minecraft:nether", - preset("minecraft:nether"), - ), - ), - ( - "minecraft:the_end", - dimension( - "minecraft:the_end", - "minecraft:end", - Tag::compound([("type", string("minecraft:the_end"))]), - ), - ), - ]), - ), - ]), - )?; + write_world_gen_settings(dir, self.seed, WorldGen::default())?; } if !saved_data_path(dir, "game_rules").exists() || !self.game_rules.is_empty() { // Only rules vanilla knows: an unknown key makes it reject the file. diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index 52dfba28a..c4e568df5 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -12,9 +12,11 @@ use std::{ sync::mpsc, }; +mod create_world; #[cfg(test)] mod tests; mod world_management; +pub use create_world::{DIFFICULTIES, WorldOptions}; /// SelectWorldScreen's `HeaderAndFooterLayout(8 + 9 + 8 + 20 + 4, 60)`. const LIST_TOP: f32 = 49.0; @@ -81,6 +83,7 @@ pub enum Mode { seed: String, creative: bool, focus: u8, + options: WorldOptions, }, Edit { world: WorldEntry, @@ -114,6 +117,7 @@ pub enum Action { name: String, seed: Option, creative: bool, + options: WorldOptions, }, Connect { address: String, @@ -140,7 +144,7 @@ pub struct Title { /// The saves listed for Home, re-read every couple of seconds. recent: std::cell::RefCell)>>, } -struct Button { +pub(crate) struct Button { x: f32, y: f32, w: f32, @@ -265,12 +269,13 @@ impl Title { } }; } - fn new_world() -> Mode { + pub(crate) fn new_world() -> Mode { Mode::Create { name: "New World".into(), seed: String::new(), creative: false, focus: 0, + options: WorldOptions::default(), } } fn return_to_list(&mut self, folder: Option<&str>) { @@ -483,11 +488,22 @@ impl Title { name, seed, creative, + options, .. } = &self.mode else { return Action::None; }; + Action::Create { + folder: self.target_folder(name), + name: name.clone(), + seed: parse_seed(seed), + creative: *creative, + options: options.clone(), + } + } + /// The folder a new world named `name` gets (`findResultFolder`). + fn target_folder(&self, name: &str) -> String { let base: String = name .chars() .map(|c| { @@ -510,12 +526,7 @@ impl Title { folder = format!("{base} ({n})"); n += 1; } - Action::Create { - folder, - name: name.clone(), - seed: parse_seed(seed), - creative: *creative, - } + folder } fn buttons(&self, w: f32, h: f32) -> Vec