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

fix out of bounds y position

This commit is contained in:
Ubuntu 2022-10-24 18:20:18 +00:00
parent 3869fd622f
commit 61642f165b
2 changed files with 34 additions and 13 deletions

View file

@ -79,23 +79,21 @@ impl ChunkStorage {
&& (chunk_pos.z - self.view_center.z).unsigned_abs() <= self.chunk_radius
}
pub fn get_block_state(&self, pos: &BlockPos, min_y: i32) -> 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 = self[&chunk_pos].as_ref()?;
let chunk = chunk.lock().unwrap();
Some(chunk.get(&ChunkBlockPos::from(pos), min_y))
Some(chunk.get(&ChunkBlockPos::from(pos), self.min_y))
}
pub fn set_block_state(
&self,
pos: &BlockPos,
state: BlockState,
min_y: i32,
) -> Option<BlockState> {
pub fn set_block_state(&self, pos: &BlockPos, state: BlockState) -> Option<BlockState> {
let chunk_pos = ChunkPos::from(pos);
let chunk = self[&chunk_pos].as_ref()?;
let mut chunk = chunk.lock().unwrap();
Some(chunk.get_and_set(&ChunkBlockPos::from(pos), state, min_y))
Some(chunk.get_and_set(&ChunkBlockPos::from(pos), state, self.min_y))
}
pub fn replace_with_packet_data(
@ -166,9 +164,14 @@ impl Chunk {
}
pub fn get(&self, pos: &ChunkBlockPos, min_y: i32) -> BlockState {
let section_index = self.section_index(pos.y, min_y);
let section_index = self.section_index(pos.y, min_y) as usize;
assert!(
section_index < self.sections.len(),
"Given y position ({}) is out of bounds",
pos.y
);
// TODO: make sure the section exists
let section = &self.sections[section_index as usize];
let section = &self.sections[section_index];
let chunk_section_pos = ChunkSectionBlockPos::from(pos);
section.get(chunk_section_pos)
}
@ -304,4 +307,22 @@ mod tests {
assert_eq!(chunk.section_index(-48, -64), 1);
assert_eq!(chunk.section_index(128, -64), 12);
}
#[test]
fn test_out_of_bounds_y() {
let mut chunk_storage = ChunkStorage::default();
chunk_storage[&ChunkPos { x: 0, z: 0 }] = Some(Arc::new(Mutex::new(Chunk::default())));
assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: 319, z: 0 })
.is_some());
assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: 320, z: 0 })
.is_none());
assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: -64, z: 0 })
.is_some());
assert!(chunk_storage
.get_block_state(&BlockPos { x: 0, y: -65, z: 0 })
.is_none());
}
}

View file

@ -62,11 +62,11 @@ impl Dimension {
}
pub fn get_block_state(&self, pos: &BlockPos) -> Option<BlockState> {
self.chunk_storage.get_block_state(pos, self.min_y())
self.chunk_storage.get_block_state(pos)
}
pub fn set_block_state(&mut self, pos: &BlockPos, state: BlockState) -> Option<BlockState> {
self.chunk_storage.set_block_state(pos, state, self.min_y())
self.chunk_storage.set_block_state(pos, state)
}
pub fn set_entity_pos(&mut self, entity_id: u32, new_pos: Vec3) -> Result<(), MoveEntityError> {