From dd312aaba331e2e00f72ed105580b4a75fe437e0 Mon Sep 17 00:00:00 2001 From: DimitriTimoz Date: Mon, 25 Dec 2023 18:09:36 +0100 Subject: [PATCH] ensure loaded chunks --- minecraft-server/Cargo.toml | 4 ++-- minecraft-server/src/entities/player.rs | 2 +- minecraft-server/src/player_handler/connect.rs | 3 +-- minecraft-server/src/player_handler/handshake.rs | 2 +- minecraft-server/src/world/loading_manager.rs | 3 +-- minecraft-server/src/world/mod.rs | 12 +++++++++--- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/minecraft-server/Cargo.toml b/minecraft-server/Cargo.toml index b3fc1e4c..fa7cdd6e 100644 --- a/minecraft-server/Cargo.toml +++ b/minecraft-server/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" env_logger = "0.10.0" tokio = { version = "1.33.0", features = ["full"] } futures = "0.3.29" -minecraft-protocol = { path="../minecraft-protocol" } +minecraft-protocol = { path="../minecraft-protocol"} minecraft-positions = { path="../minecraft-positions" } minecraft-entities-derive = { path="../minecraft-entities-derive" } rand = "0.8.4" @@ -21,4 +21,4 @@ tracing = { version = "0.1", features = ["attributes"] } [features] default = [] -tracing = ["tracy-client", "tracing-tracy"] +trace = ["tracy-client", "tracing-tracy" ] diff --git a/minecraft-server/src/entities/player.rs b/minecraft-server/src/entities/player.rs index 340e3724..b6210000 100644 --- a/minecraft-server/src/entities/player.rs +++ b/minecraft-server/src/entities/player.rs @@ -126,7 +126,7 @@ impl Handler { }).await.flatten() else { return }; // Tell the world about the changes - self.world.update_loaded_chunks(uuid, loaded_chunks_after).await; + self.world.ensure_loaded_chunks(uuid, loaded_chunks_after).await; // Send the chunks to the client let mut heightmaps = HashMap::new(); diff --git a/minecraft-server/src/player_handler/connect.rs b/minecraft-server/src/player_handler/connect.rs index db280d35..39d23774 100644 --- a/minecraft-server/src/player_handler/connect.rs +++ b/minecraft-server/src/player_handler/connect.rs @@ -1,8 +1,7 @@ use super::*; -#[cfg_attr(feature = "tracing", instrument(skip_all))] - +#[cfg_attr(feature = "trace", instrument(skip_all))] pub async fn handle_connection( mut stream: TcpStream, addr: SocketAddr, diff --git a/minecraft-server/src/player_handler/handshake.rs b/minecraft-server/src/player_handler/handshake.rs index d955dfb2..40b33982 100644 --- a/minecraft-server/src/player_handler/handshake.rs +++ b/minecraft-server/src/player_handler/handshake.rs @@ -315,7 +315,7 @@ pub async fn handshake(stream: &mut TcpStream, logged_in_player_info: LoggedInPl loaded_chunks.insert(ChunkColumnPosition { cx, cz }); } } - world.update_loaded_chunks(logged_in_player_info.uuid, loaded_chunks).await; + world.ensure_loaded_chunks(logged_in_player_info.uuid, loaded_chunks).await; let mut heightmaps = HashMap::new(); heightmaps.insert(String::from("MOTION_BLOCKING"), NbtTag::LongArray(vec![0; 37])); diff --git a/minecraft-server/src/world/loading_manager.rs b/minecraft-server/src/world/loading_manager.rs index 6995af76..87953e59 100644 --- a/minecraft-server/src/world/loading_manager.rs +++ b/minecraft-server/src/world/loading_manager.rs @@ -7,8 +7,7 @@ pub(super) struct WorldLoadingManager { } impl WorldLoadingManager { - #[cfg_attr(feature = "tracing", instrument(skip_all))] - + #[cfg_attr(feature = "trace", instrument(skip_all))] pub(super) fn update_loaded_chunks(&mut self, uuid: UUID, loaded_chunks: HashSet) { let loaded_before = self.loaded_chunks.entry(uuid).or_default(); for just_unloaded in loaded_before.difference(&loaded_chunks) { diff --git a/minecraft-server/src/world/mod.rs b/minecraft-server/src/world/mod.rs index 98fbed5a..c04f3488 100644 --- a/minecraft-server/src/world/mod.rs +++ b/minecraft-server/src/world/mod.rs @@ -63,7 +63,7 @@ impl World { self.change_senders.write().await.remove(&uuid); } - pub async fn update_loaded_chunks(&self, uuid: UUID, loaded_chunks: HashSet) { + pub async fn ensure_loaded_chunks(&self, uuid: UUID, loaded_chunks: HashSet) { let mut loading_manager = self.loading_manager.write().await; let loaded_chunks_before = loading_manager.get_loaded_chunks(); loading_manager.update_loaded_chunks(uuid, loaded_chunks); @@ -160,6 +160,12 @@ impl World { } } } + + #[cfg_attr(feature = "trace", instrument(skip(self)))] + pub async fn get_network_chunk_column_data<'a>(&self, position: ChunkColumnPosition) -> Option> { + self.map.get_network_chunk_column_data(position).await + } + } #[cfg(test)] @@ -173,8 +179,8 @@ mod tests { let mut receiver1 = world.add_loader(1).await; let mut receiver2 = world.add_loader(2).await; - world.update_loaded_chunks(1, vec![ChunkColumnPosition{cx: 0, cz: 0}].into_iter().collect()).await; - world.update_loaded_chunks(2, vec![ChunkColumnPosition{cx: 1, cz: 1}].into_iter().collect()).await; + world.ensure_loaded_chunks(1, vec![ChunkColumnPosition{cx: 0, cz: 0}].into_iter().collect()).await; + world.ensure_loaded_chunks(2, vec![ChunkColumnPosition{cx: 1, cz: 1}].into_iter().collect()).await; world.set_block(BlockPosition{x: 1, y: 1, z: 1}, BlockWithState::Air).await; assert!(matches!(receiver1.try_recv(), Ok(WorldChange::Block(BlockPosition{x: 1, y: 1, z: 1}, BlockWithState::Air))));