From 58001c972a4d311e7137b13473609823e738d2d9 Mon Sep 17 00:00:00 2001 From: MrlnHi Date: Sun, 23 Apr 2023 11:26:28 +0200 Subject: [PATCH] Fix schem_loading example --- crates/valence/examples/schem_loading.rs | 76 ++++++++++-------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/crates/valence/examples/schem_loading.rs b/crates/valence/examples/schem_loading.rs index 5b0e2723e..bbf09f33e 100644 --- a/crates/valence/examples/schem_loading.rs +++ b/crates/valence/examples/schem_loading.rs @@ -1,9 +1,6 @@ use std::path::PathBuf; use clap::Parser; -use valence::bevy_app::AppExit; -use valence::client::despawn_disconnected_clients; -use valence::client::event::default_event_handler; use valence::prelude::*; use valence_schem::Schematic; @@ -16,68 +13,61 @@ struct Cli { path: PathBuf, } +#[derive(Resource)] +struct SchemRes(Schematic); + pub fn main() { tracing_subscriber::fmt().init(); - App::new() - .add_plugin(ServerPlugin::new(())) - .add_startup_system(setup) - .add_systems(( - default_event_handler.in_schedule(EventLoopSchedule), - init_clients, - despawn_disconnected_clients, - )) - .add_systems(PlayerList::default_systems()) - .run(); -} - -fn setup(mut commands: Commands, server: Res, mut exit: EventWriter) { let Cli { path } = Cli::parse(); - if !path.exists() { eprintln!("File `{}` does not exist. Exiting.", path.display()); - exit.send_default(); + return; } else if !path.is_file() { eprintln!("`{}` is not a file. Exiting.", path.display()); - exit.send_default(); + return; } - - let mut instance = server.new_instance(DimensionId::default()); - - match Schematic::load(path) { - Ok(schem) => { - schem.paste(&mut instance, SPAWN_POS, |_| BiomeId::default()); - } + let schem = match Schematic::load(path) { + Ok(schem) => schem, Err(err) => { eprintln!("Error loading schematic: {err}"); - exit.send_default(); + return; } - } + }; + App::new() + .add_plugins(DefaultPlugins) + .insert_resource(SchemRes(schem)) + .add_startup_system(setup) + .add_systems((init_clients, despawn_disconnected_clients)) + .run(); +} + +fn setup( + mut commands: Commands, + dimensions: Query<&DimensionType>, + biomes: Query<&Biome>, + server: Res, + schem: Res, +) { + let mut instance = Instance::new(ident!("overworld"), &dimensions, &biomes, &server); + schem + .0 + .paste(&mut instance, SPAWN_POS, |_| BiomeId::default()); commands.spawn(instance); } fn init_clients( - mut clients: Query<&mut Client, Added>, + mut clients: Query<(&mut Location, &mut Position, &mut GameMode), Added>, instances: Query>, - mut commands: Commands, ) { - for mut client in &mut clients { - let instance = instances.single(); - - client.set_flat(true); - client.set_game_mode(GameMode::Creative); - client.set_position([ + for (mut loc, mut pos, mut game_mode) in &mut clients { + *game_mode = GameMode::Creative; + pos.set([ SPAWN_POS.x as f64 + 0.5, SPAWN_POS.y as f64, SPAWN_POS.z as f64 + 0.5, ]); - client.set_instance(instance); - - commands.spawn(McEntity::with_uuid( - EntityKind::Player, - instance, - client.uuid(), - )); + loc.0 = instances.single(); } }