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

Commit

Permalink
Implement tests getters and setters
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriTimoz committed Dec 28, 2023
1 parent 05ceb8e commit 23cc58b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 5 additions & 5 deletions minecraft-server/src/world/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,15 @@ impl LightManager {
}
}

async fn set_light_level(&mut self, position: LightPosition, level: u8) {
pub async fn set_light_level(&mut self, position: LightPosition, level: u8) {
let chunk_col_position = ChunkColumnPosition::from(position.clone());
let shard_id = ChunkColumnPosition::from(chunk_col_position.clone()).shard(self.world_map.get_shard_count());
let shard_id = chunk_col_position.clone().shard(self.world_map.get_shard_count());
self.ensure_shard(shard_id).await;

if let Some(shard) = &mut self.current_shard {
// Here, we use a reference to `shard` instead of trying to move it
if let Some(col) = shard.get_mut(&chunk_col_position) {
if let Ok(_) = col.light.sky_light.set_level(LightPositionInChunkColumn::from(position), level) {
return;
if col.light.sky_light.set_level(LightPositionInChunkColumn::from(position), level).is_ok() {
} else {
error!("Chunk column found at {:?} in shard {} but light level not found", chunk_col_position, shard_id);
}
Expand All @@ -313,7 +312,7 @@ impl LightManager {

pub async fn get_light_level(&mut self, position: LightPosition) -> u8 {
let chunk_col_position = ChunkColumnPosition::from(position.clone());
let shard_id = ChunkColumnPosition::from(chunk_col_position.clone()).shard(self.world_map.get_shard_count());
let shard_id = chunk_col_position.clone().shard(self.world_map.get_shard_count());
self.ensure_shard(shard_id).await;

if let Some(shard) = &mut self.current_shard {
Expand All @@ -335,6 +334,7 @@ impl LightManager {
}
}


async fn set_block(&mut self, block_position: BlockPosition, block: Block) {
let mut to_explore = BinaryHeap::new();
let position = LightPosition::from(block_position.clone());
Expand Down
13 changes: 13 additions & 0 deletions minecraft-server/src/world/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,17 @@ mod tests {
let movement = world.map.try_move(&positionned_box, &movement).await;
assert_eq!(movement, Translation { x: 0.2200000000000003, y: -1.1000000000000014, z: 0.0 }); // It falls down but doesn't get through
}

#[tokio::test]
async fn test_light_getters_and_setters() {
let world = Box::leak(Box::new(World::new(broadcast_channel(100).1)));
world.map.load(ChunkColumnPosition { cx: 0, cz: 0 }).await;

// Test set_light_level
world.set_light_level(BlockPosition { x: 0, y: 0, z: 0 }, 15).await;
assert_eq!(world.get_light_level(BlockPosition { x: 0, y: 0, z: 0 }).await, 15);

world.set_light_level(BlockPosition { x: 0, y: 10, z: 0 }, 0).await;
assert_eq!(world.get_light_level(BlockPosition { x: 0, y: 10, z: 0 }).await, 0);
}
}
4 changes: 4 additions & 0 deletions minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ impl World {
LightManager::new(&self.map).get_light_level(LightPosition::from(position)).await
}

pub async fn set_light_level(&'static self, position: BlockPosition, light_level: u8) {
LightManager::new(&self.map).set_light_level(LightPosition::from(position), light_level).await;
}

}

#[cfg(test)]
Expand Down

0 comments on commit 23cc58b

Please sign in to comment.