Skip to content

Commit 58001c9

Browse files
committed
Fix schem_loading example
1 parent 953a851 commit 58001c9

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed
Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::path::PathBuf;
22

33
use clap::Parser;
4-
use valence::bevy_app::AppExit;
5-
use valence::client::despawn_disconnected_clients;
6-
use valence::client::event::default_event_handler;
74
use valence::prelude::*;
85
use valence_schem::Schematic;
96

@@ -16,68 +13,61 @@ struct Cli {
1613
path: PathBuf,
1714
}
1815

16+
#[derive(Resource)]
17+
struct SchemRes(Schematic);
18+
1919
pub fn main() {
2020
tracing_subscriber::fmt().init();
2121

22-
App::new()
23-
.add_plugin(ServerPlugin::new(()))
24-
.add_startup_system(setup)
25-
.add_systems((
26-
default_event_handler.in_schedule(EventLoopSchedule),
27-
init_clients,
28-
despawn_disconnected_clients,
29-
))
30-
.add_systems(PlayerList::default_systems())
31-
.run();
32-
}
33-
34-
fn setup(mut commands: Commands, server: Res<Server>, mut exit: EventWriter<AppExit>) {
3522
let Cli { path } = Cli::parse();
36-
3723
if !path.exists() {
3824
eprintln!("File `{}` does not exist. Exiting.", path.display());
39-
exit.send_default();
25+
return;
4026
} else if !path.is_file() {
4127
eprintln!("`{}` is not a file. Exiting.", path.display());
42-
exit.send_default();
28+
return;
4329
}
44-
45-
let mut instance = server.new_instance(DimensionId::default());
46-
47-
match Schematic::load(path) {
48-
Ok(schem) => {
49-
schem.paste(&mut instance, SPAWN_POS, |_| BiomeId::default());
50-
}
30+
let schem = match Schematic::load(path) {
31+
Ok(schem) => schem,
5132
Err(err) => {
5233
eprintln!("Error loading schematic: {err}");
53-
exit.send_default();
34+
return;
5435
}
55-
}
36+
};
5637

38+
App::new()
39+
.add_plugins(DefaultPlugins)
40+
.insert_resource(SchemRes(schem))
41+
.add_startup_system(setup)
42+
.add_systems((init_clients, despawn_disconnected_clients))
43+
.run();
44+
}
45+
46+
fn setup(
47+
mut commands: Commands,
48+
dimensions: Query<&DimensionType>,
49+
biomes: Query<&Biome>,
50+
server: Res<Server>,
51+
schem: Res<SchemRes>,
52+
) {
53+
let mut instance = Instance::new(ident!("overworld"), &dimensions, &biomes, &server);
54+
schem
55+
.0
56+
.paste(&mut instance, SPAWN_POS, |_| BiomeId::default());
5757
commands.spawn(instance);
5858
}
5959

6060
fn init_clients(
61-
mut clients: Query<&mut Client, Added<Client>>,
61+
mut clients: Query<(&mut Location, &mut Position, &mut GameMode), Added<Client>>,
6262
instances: Query<Entity, With<Instance>>,
63-
mut commands: Commands,
6463
) {
65-
for mut client in &mut clients {
66-
let instance = instances.single();
67-
68-
client.set_flat(true);
69-
client.set_game_mode(GameMode::Creative);
70-
client.set_position([
64+
for (mut loc, mut pos, mut game_mode) in &mut clients {
65+
*game_mode = GameMode::Creative;
66+
pos.set([
7167
SPAWN_POS.x as f64 + 0.5,
7268
SPAWN_POS.y as f64,
7369
SPAWN_POS.z as f64 + 0.5,
7470
]);
75-
client.set_instance(instance);
76-
77-
commands.spawn(McEntity::with_uuid(
78-
EntityKind::Player,
79-
instance,
80-
client.uuid(),
81-
));
71+
loc.0 = instances.single();
8272
}
8373
}

0 commit comments

Comments
 (0)