Struct memory_management::world::World
source · pub struct World { /* private fields */ }Expand description
A struct to manage a world
Implementations§
source§impl World
impl World
sourcepub fn new(region_file_location: &'static str) -> Self
pub fn new(region_file_location: &'static str) -> Self
Creates a new instance of World.
This function initializes a new World with empty players, a new generator,
and a ChunkHolder initialized with the provided region file location.
§Arguments
region_file_location- A static string slice that specifies the location of the region file to be used by theChunkHolder.
§Returns
- A new instance of
World.
§Example
use std::collections::HashMap;
use memory_management::world::Generator;
use memory_management::world::chunks::ChunkHolder;
use memory_management::world::World;
fn main() {
// Specify the region file location
let region_file_location = "../../../nbt_lib/test_data/test_world/region/";
// Create a new World instance
let world = World::new(region_file_location);
// Use the World instance
println!("New world created with region file: {}", region_file_location);
}§See Also
sourcepub fn get_chunk(&mut self, x: i64, z: i64) -> Result<Rc<ChunkData>, ()>
pub fn get_chunk(&mut self, x: i64, z: i64) -> Result<Rc<ChunkData>, ()>
Retrieves a chunk at the specified coordinates (x, z).
This function first attempts to get an existing chunk from the internal storage.
If the chunk is found and its generation status is GenerationStatus::Full, it is returned wrapped in Ok.
If the chunk is not found or its generation status is not Full, a new chunk is generated for the specified coordinates.
After generation, the chunk is inserted into the internal storage. If the insertion
is successful, the generated chunk is returned wrapped in Ok.
If both retrieving and generating the chunk fail, the function returns Err.
§Arguments
x- The x-coordinate of the chunk.z- The z-coordinate of the chunk.
§Returns
Ok(Rc<ChunkData>)- If the chunk is found and has aFullgeneration status or is successfully generated.Err(())- If the chunk cannot be retrieved or generated.
§Errors
This function returns Err(()) if the chunk cannot be retrieved or generated, or if the retrieved chunk’s generation status is not Full.
§Example
use memory_management::world::World;
use level_lib::anvil::region::chunk::chunk_data::ChunkData;
let mut world = World::new("../../../nbt_lib/test_data/test_world/region/");
let chunk = world.get_chunk(1000, 1000).expect("This should not fail, because a chunks is getting
generated, if the read chunk does not exist or is incomplete");
assert!(matches!(chunk.as_ref(), &ChunkData { .. }));