diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-08-06 07:22:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-06 02:22:19 -0500 |
| commit | 5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch) | |
| tree | b006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-world/src/lib.rs | |
| parent | 1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff) | |
| download | azalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz | |
Better errors (#14)
* make reading use thiserror
* finish implementing all the error things
* clippy warnings related to ok_or
* fix some errors in other places
* thiserror in more places
* don't use closures in a couple places
* errors in writing packet
* rip backtraces
* change some BufReadError::Custom to UnexpectedEnumVariant
* Errors say what packet is bad
* error on leftover data and fix
it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-world/src/lib.rs')
| -rw-r--r-- | azalea-world/src/lib.rs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index 7822dcc4..646ba1d9 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -6,6 +6,7 @@ mod entity; mod palette; use azalea_block::BlockState; +use azalea_buf::BufReadError; use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta8}; use azalea_entity::Entity; pub use bit_storage::BitStorage; @@ -16,6 +17,7 @@ use std::{ ops::{Index, IndexMut}, sync::{Arc, Mutex}, }; +use thiserror::Error; use uuid::Uuid; #[cfg(test)] @@ -34,6 +36,12 @@ pub struct Dimension { entity_storage: EntityStorage, } +#[derive(Error, Debug)] +pub enum MoveEntityError { + #[error("Entity doesn't exist")] + EntityDoesNotExist, +} + impl Dimension { pub fn new(chunk_radius: u32, height: u32, min_y: i32) -> Self { Dimension { @@ -46,7 +54,7 @@ impl Dimension { &mut self, pos: &ChunkPos, data: &mut impl Read, - ) -> Result<(), String> { + ) -> Result<(), BufReadError> { self.chunk_storage.replace_with_packet_data(pos, data) } @@ -58,11 +66,15 @@ impl Dimension { self.chunk_storage.get_block_state(pos, self.min_y()) } - pub fn move_entity(&mut self, entity_id: u32, new_pos: EntityPos) -> Result<(), String> { + pub fn move_entity( + &mut self, + entity_id: u32, + new_pos: EntityPos, + ) -> Result<(), MoveEntityError> { let entity = self .entity_storage .get_mut_by_id(entity_id) - .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; + .ok_or(MoveEntityError::EntityDoesNotExist)?; let old_chunk = ChunkPos::from(entity.pos()); let new_chunk = ChunkPos::from(&new_pos); @@ -79,11 +91,11 @@ impl Dimension { &mut self, entity_id: u32, delta: &PositionDelta8, - ) -> Result<(), String> { + ) -> Result<(), MoveEntityError> { let entity = self .entity_storage .get_mut_by_id(entity_id) - .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; + .ok_or(MoveEntityError::EntityDoesNotExist)?; let new_pos = entity.pos().with_delta(delta); let old_chunk = ChunkPos::from(entity.pos()); |
