From a6bb67ecfe4fdbae2dbc1bbc80e99fb48eb8a97a Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 3 Jul 2022 18:45:11 -0700 Subject: [PATCH] Fix client respawning --- examples/conway.rs | 2 +- examples/cow_sphere.rs | 2 +- examples/terrain.rs | 2 +- src/client.rs | 57 +++++++++++++++++++++++------------------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/examples/conway.rs b/examples/conway.rs index 8a74e05..4a02a1d 100644 --- a/examples/conway.rs +++ b/examples/conway.rs @@ -118,7 +118,7 @@ impl Config for Game { return false; } - client.set_world(world_id); + client.spawn(world_id); client.set_game_mode(GameMode::Survival); client.teleport(spawn_pos, 0.0, 0.0); diff --git a/examples/cow_sphere.rs b/examples/cow_sphere.rs index 7726d66..eeee893 100644 --- a/examples/cow_sphere.rs +++ b/examples/cow_sphere.rs @@ -93,7 +93,7 @@ impl Config for Game { return false; } - client.set_world(world_id); + client.spawn(world_id); client.set_game_mode(GameMode::Creative); client.teleport([0.0, 200.0, 0.0], 0.0, 0.0); diff --git a/examples/terrain.rs b/examples/terrain.rs index 450db2a..443beb8 100644 --- a/examples/terrain.rs +++ b/examples/terrain.rs @@ -97,7 +97,7 @@ impl Config for Game { return false; } - client.set_world(world_id); + client.spawn(world_id); client.set_game_mode(GameMode::Creative); client.set_max_view_distance(32); client.teleport([0.0, 200.0, 0.0], 0.0, 0.0); diff --git a/src/client.rs b/src/client.rs index 722f546..025a053 100644 --- a/src/client.rs +++ b/src/client.rs @@ -103,8 +103,8 @@ pub struct Client { uuid: Uuid, username: String, textures: Option, - new_world: WorldId, - old_world: WorldId, + world: WorldId, + spawn: bool, on_ground: bool, new_position: Vec3, old_position: Vec3, @@ -162,8 +162,8 @@ impl Client { uuid: ncd.uuid, username: ncd.username, textures: ncd.textures, - new_world: WorldId::default(), - old_world: WorldId::default(), + world: WorldId::default(), + spawn: false, on_ground: false, new_position: Vec3::default(), old_position: Vec3::default(), @@ -209,11 +209,12 @@ impl Client { } pub fn world(&self) -> WorldId { - self.new_world + self.world } - pub fn set_world(&mut self, world: WorldId) { - self.new_world = world; + pub fn spawn(&mut self, world: WorldId) { + self.world = world; + self.spawn = true; } /// Sends a system message to the player. @@ -578,7 +579,7 @@ impl Client { return; } - let world = match worlds.get(self.new_world) { + let world = match worlds.get(self.world) { Some(world) => world, None => { log::warn!( @@ -600,15 +601,19 @@ impl Client { .player_list() .initial_packets(|pkt| self.send_packet(pkt)); + let mut dimension_names: Vec<_> = shared + .dimensions() + .map(|(id, _)| ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0)) + .collect(); + + dimension_names.push(ident!("{LIBRARY_NAMESPACE}:dummy_dimension")); + self.send_packet(Login { entity_id: 0, // EntityId 0 is reserved for clients. is_hardcore: false, // TODO gamemode: self.new_game_mode, previous_gamemode: self.old_game_mode, - dimension_names: shared - .dimensions() - .map(|(id, _)| ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0)) - .collect(), + dimension_names, registry_codec: Nbt(make_dimension_codec(shared)), dimension_type_name: ident!( "{LIBRARY_NAMESPACE}:dimension_type_{}", @@ -633,24 +638,24 @@ impl Client { self.teleport(self.position(), self.yaw(), self.pitch()); } else { - if self.new_world != self.old_world { + if self.spawn { self.loaded_entities.clear(); self.loaded_chunks.clear(); // TODO: clear player list. - // // Client bug workaround: send the client to a dummy dimension first. - // self.send_packet(Respawn { - // dimension_type_name: ident!("{LIBRARY_NAMESPACE}:dimension_type_0"), - // dimension_name: ident!("{LIBRARY_NAMESPACE}:dummy_dimension"), - // hashed_seed: 0, - // game_mode: self.game_mode(), - // previous_game_mode: self.game_mode(), - // is_debug: false, - // is_flat: false, - // copy_metadata: true, - // last_death_location: None, - // }); + // Client bug workaround: send the client to a dummy dimension first. + self.send_packet(Respawn { + dimension_type_name: ident!("{LIBRARY_NAMESPACE}:dimension_type_0"), + dimension_name: ident!("{LIBRARY_NAMESPACE}:dummy_dimension"), + hashed_seed: 0, + game_mode: self.game_mode(), + previous_game_mode: self.game_mode(), + is_debug: false, + is_flat: false, + copy_metadata: true, + last_death_location: None, + }); self.send_packet(Respawn { dimension_type_name: ident!( @@ -960,7 +965,7 @@ impl Client { ); self.old_position = self.new_position; - self.old_world = self.new_world; + self.spawn = false; } }