Struct Region

Source
#[repr(C)]
pub struct Region { /* private fields */ }
Expand description

Represents a region file, with methods to access data within it.

https://minecraft.wiki/w/Region_file_format

Implementations§

Source§

impl Region

Source

pub fn from_slice(slice: &[u8]) -> Result<&Region>

Parse this slice into a Region. This does no input validation except confirm that the 8KiB header is there, further validation is done in Region::get_chunk and Chunk::parse to help prevent unnecessary memory allocation.

Note: Changing the data in the slice after calling this method will change the Region returned by this method, so it is advised against

Source

pub const unsafe fn from_array<const N: usize>(arr: &[u8; N]) -> &'static Region

Create a Region from an array if the size is known at compile time.

§Safety
  • N >= 8192
  • Array should contain valid bytes for a region file, though if it doesn’t, that issue will be caught in Region::get_chunk and Chunk::parse
§Usage

The intended usage of this method is as a const value:

const REGION: &Region = unsafe { Region::from_array(include_bytes!("../test/r.0.0.mca")) };

This method will panic if N < 8192, thus failing to compile when used as a const value:

const REGION: &Region = unsafe { Region::from_array(&[0; 16]) };
Source

pub fn from_reader<R>(r: &mut R) -> Result<Box<Region>>
where R: Read,

A method for ease of use, effectively does the same thing as calling Read::read_to_end and then passing that to Region::from_slice, with the only difference being that it returns an owned box rather than a reference.

§Usage
let mut file = File::open("./test/r.0.0.mca")?;
let region = Region::from_reader(&mut file)?;
Source

pub fn validate(&self) -> Result<()>

Validate that this Region contains all valid chunks by trying to parse every chunk.

§Important Note
  • This method is obviously slow and uses a decent amount of memory. It is recommended to assume the data is correct and validate it as you use the Region::get_chunk and Chunk::parse methods.
  • This method should only be used when you absolutely need to validate the data is correct and can’t use the Region::get_chunk and Chunk::parse methods
Source

pub const fn get_timestamp(&self, x: u32, z: u32) -> u32

Get a timestamp for a chunk in this Region

§Panics
  • If x and z are not within 0..=31
Source

pub const fn has_chunk(&self, x: u32, z: u32) -> bool

Check if the chunk at x and z have been generated

§Panics
  • If x and z are not within 0..=31
Source

pub fn get_chunk(&self, chunk_x: u32, chunk_z: u32) -> Result<Option<&Chunk>>

Get a chunk from this Region using relative coordinates within the region

§Return Values
  • Err if data is invalid
  • Ok(None) if the data is valid, but there is no chunk generated
  • Ok(Some(&Chunk)) if the data is valid and the chunk exists

This will return a &Chunk which references this Region, if you want an owned version, call Chunk::boxed on the returned chunk.

§Panics
  • If x and z are not within 0..=31
Source

pub fn get_chunk_from_block( &self, block_x: u32, block_z: u32, ) -> Result<Option<&Chunk>>

Get a chunk from this Region using relative block coordinates within the region

§Return Values
  • Err if data is invalid
  • Ok(None) if the data is valid, but there is no chunk generated
  • Ok(Some(&Chunk)) if the data is valid and the chunk exists

This will return a &Chunk which references this Region, if you want an owned version, call Chunk::boxed on the returned chunk.

§Panics
  • If x and z are not within 0..=511

Trait Implementations§

Source§

impl Debug for Region

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a Region> for RegionRef<'a>

Source§

fn from(value: &'a Region) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Region

Source§

fn eq(&self, other: &Region) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Region

Source§

impl StructuralPartialEq for Region

Auto Trait Implementations§

§

impl Freeze for Region

§

impl RefUnwindSafe for Region

§

impl Send for Region

§

impl !Sized for Region

§

impl Sync for Region

§

impl Unpin for Region

§

impl UnwindSafe for Region

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more