Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
ensure loaded chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriTimoz committed Dec 25, 2023
1 parent a0e8696 commit dd312aa
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions minecraft-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,4 +21,4 @@ tracing = { version = "0.1", features = ["attributes"] }

[features]
default = []
tracing = ["tracy-client", "tracing-tracy"]
trace = ["tracy-client", "tracing-tracy" ]
2 changes: 1 addition & 1 deletion minecraft-server/src/entities/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Handler<Player> {
}).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();
Expand Down
3 changes: 1 addition & 2 deletions minecraft-server/src/player_handler/connect.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion minecraft-server/src/player_handler/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
3 changes: 1 addition & 2 deletions minecraft-server/src/world/loading_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChunkColumnPosition>) {
let loaded_before = self.loaded_chunks.entry(uuid).or_default();
for just_unloaded in loaded_before.difference(&loaded_chunks) {
Expand Down
12 changes: 9 additions & 3 deletions minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChunkColumnPosition>) {
pub async fn ensure_loaded_chunks(&self, uuid: UUID, loaded_chunks: HashSet<ChunkColumnPosition>) {
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);
Expand Down Expand Up @@ -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<Vec<u8>> {
self.map.get_network_chunk_column_data(position).await
}

}

#[cfg(test)]
Expand All @@ -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))));
Expand Down

0 comments on commit dd312aa

Please sign in to comment.