1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

actually fix out of bounds errors

This commit is contained in:
Ubuntu 2022-10-24 19:01:28 +00:00
parent 61642f165b
commit 0d55c71cee
2 changed files with 16 additions and 12 deletions

View file

@ -95,7 +95,9 @@ impl<'a> Iterator for BlockCollisions<'a> {
let chunk_lock = chunk.lock().unwrap(); let chunk_lock = chunk.lock().unwrap();
let pos = item.pos; let pos = item.pos;
let block_state: BlockState = chunk_lock.get(&(&pos).into(), self.dimension.min_y()); let block_state: BlockState = chunk_lock
.get(&(&pos).into(), self.dimension.min_y())
.unwrap_or(BlockState::Air);
// TODO: continue if self.only_suffocating_blocks and the block is not suffocating // TODO: continue if self.only_suffocating_blocks and the block is not suffocating

View file

@ -80,16 +80,16 @@ impl ChunkStorage {
} }
pub fn get_block_state(&self, pos: &BlockPos) -> Option<BlockState> { pub fn get_block_state(&self, pos: &BlockPos) -> Option<BlockState> {
if pos.y < self.min_y || pos.y >= (self.min_y + self.height as i32) {
return None;
}
let chunk_pos = ChunkPos::from(pos); let chunk_pos = ChunkPos::from(pos);
let chunk = self[&chunk_pos].as_ref()?; let chunk = self[&chunk_pos].as_ref()?;
let chunk = chunk.lock().unwrap(); let chunk = chunk.lock().unwrap();
Some(chunk.get(&ChunkBlockPos::from(pos), self.min_y)) chunk.get(&ChunkBlockPos::from(pos), self.min_y)
} }
pub fn set_block_state(&self, pos: &BlockPos, state: BlockState) -> Option<BlockState> { pub fn set_block_state(&self, pos: &BlockPos, state: BlockState) -> Option<BlockState> {
if pos.y < self.min_y || pos.y >= (self.min_y + self.height as i32) {
return None;
}
let chunk_pos = ChunkPos::from(pos); let chunk_pos = ChunkPos::from(pos);
let chunk = self[&chunk_pos].as_ref()?; let chunk = self[&chunk_pos].as_ref()?;
let mut chunk = chunk.lock().unwrap(); let mut chunk = chunk.lock().unwrap();
@ -163,17 +163,16 @@ impl Chunk {
(y.div_floor(16) - min_section_index) as u32 (y.div_floor(16) - min_section_index) as u32
} }
pub fn get(&self, pos: &ChunkBlockPos, min_y: i32) -> BlockState { pub fn get(&self, pos: &ChunkBlockPos, min_y: i32) -> Option<BlockState> {
let section_index = self.section_index(pos.y, min_y) as usize; let section_index = self.section_index(pos.y, min_y) as usize;
assert!( if section_index >= self.sections.len() {
section_index < self.sections.len(), // y position is out of bounds
"Given y position ({}) is out of bounds", return None;
pos.y };
);
// TODO: make sure the section exists // TODO: make sure the section exists
let section = &self.sections[section_index]; let section = &self.sections[section_index];
let chunk_section_pos = ChunkSectionBlockPos::from(pos); let chunk_section_pos = ChunkSectionBlockPos::from(pos);
section.get(chunk_section_pos) Some(section.get(chunk_section_pos))
} }
pub fn get_and_set( pub fn get_and_set(
@ -318,6 +317,9 @@ mod tests {
assert!(chunk_storage assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: 320, z: 0 }) .get_block_state(&BlockPos { x: 0, y: 320, z: 0 })
.is_none()); .is_none());
assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: 338, z: 0 })
.is_none());
assert!(chunk_storage assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: -64, z: 0 }) .get_block_state(&BlockPos { x: 0, y: -64, z: 0 })
.is_some()); .is_some());