Skip to content

Commit

Permalink
Fix schem_loading example
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlnHi committed Apr 23, 2023
1 parent 953a851 commit 58001c9
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions crates/valence/examples/schem_loading.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Server>, mut exit: EventWriter<AppExit>) {
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<Server>,
schem: Res<SchemRes>,
) {
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<Client>>,
mut clients: Query<(&mut Location, &mut Position, &mut GameMode), Added<Client>>,
instances: Query<Entity, With<Instance>>,
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();
}
}

0 comments on commit 58001c9

Please sign in to comment.