Add .set_block on ChunkData
This commit is contained in:
parent
b37039126a
commit
0d05e5ef36
@ -168,10 +168,7 @@ impl ByteBuffer {
|
|||||||
/// some, then it also calls the `write` closure.
|
/// some, then it also calls the `write` closure.
|
||||||
pub fn put_option<T>(&mut self, val: &Option<T>, write: impl FnOnce(&mut Self, &T)) {
|
pub fn put_option<T>(&mut self, val: &Option<T>, write: impl FnOnce(&mut Self, &T)) {
|
||||||
self.put_bool(val.is_some());
|
self.put_bool(val.is_some());
|
||||||
match val {
|
if let Some(v) = val { write(self, v) }
|
||||||
Some(v) => write(self, v),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_list<T>(&mut self, val: impl Fn(&mut Self) -> T) -> Vec<T> {
|
pub fn get_list<T>(&mut self, val: impl Fn(&mut Self) -> T) -> Vec<T> {
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use fastnbt::LongArray;
|
use fastnbt::LongArray;
|
||||||
|
|
||||||
use crate::{level::WorldError, WORLD_HEIGHT};
|
use crate::{level::WorldError, vector3::Vector3, WORLD_HEIGHT, WORLD_Y_START_AT};
|
||||||
|
|
||||||
pub struct ChunkData {
|
pub struct ChunkData {
|
||||||
pub blocks: Box<[i32; 16 * 16 * WORLD_HEIGHT]>,
|
pub blocks: Box<[i32; 16 * 16 * WORLD_HEIGHT]>,
|
||||||
@ -110,4 +110,21 @@ impl ChunkData {
|
|||||||
heightmaps: chunk_data.heightmaps,
|
heightmaps: chunk_data.heightmaps,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
/// Sets the given block in the chunk, returning the old block
|
||||||
|
pub fn set_block(&mut self, at: Vector3<i32>, block_id: i32) -> Result<i32, WorldError> {
|
||||||
|
let x = at.x - self.position.0 * 16;
|
||||||
|
let z = at.z - self.position.1 * 16;
|
||||||
|
let y = at.y - WORLD_Y_START_AT;
|
||||||
|
if !(0..16).contains(&x)
|
||||||
|
|| !(0..16).contains(&z)
|
||||||
|
|| !(0..(WORLD_HEIGHT as i32)).contains(&y)
|
||||||
|
{
|
||||||
|
return Err(WorldError::BlockOutsideChunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(std::mem::replace(
|
||||||
|
&mut self.blocks[(y * 16 * 16 + z * 16 + x) as usize],
|
||||||
|
block_id,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,6 +36,8 @@ pub enum WorldError {
|
|||||||
ErrorDeserializingChunk(String),
|
ErrorDeserializingChunk(String),
|
||||||
#[error("The requested block state id does not exist")]
|
#[error("The requested block state id does not exist")]
|
||||||
BlockStateIdNotFound,
|
BlockStateIdNotFound,
|
||||||
|
#[error("The block is not inside of the chunk")]
|
||||||
|
BlockOutsideChunk,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user