aboutsummaryrefslogtreecommitdiff
path: root/azalea-world/src
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-08-06 07:22:19 +0000
committerGitHub <noreply@github.com>2022-08-06 02:22:19 -0500
commit5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch)
treeb006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-world/src
parent1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff)
downloadazalea-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')
-rw-r--r--azalea-world/src/chunk.rs16
-rw-r--r--azalea-world/src/lib.rs22
-rw-r--r--azalea-world/src/palette.rs10
3 files changed, 33 insertions, 15 deletions
diff --git a/azalea-world/src/chunk.rs b/azalea-world/src/chunk.rs
index e4893ace..9d393c2d 100644
--- a/azalea-world/src/chunk.rs
+++ b/azalea-world/src/chunk.rs
@@ -2,6 +2,7 @@ use crate::palette::PalettedContainer;
use crate::palette::PalettedContainerType;
use crate::Dimension;
use azalea_block::BlockState;
+use azalea_buf::BufReadError;
use azalea_buf::{McBufReadable, McBufWritable};
use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos};
use std::fmt::Debug;
@@ -69,7 +70,7 @@ impl ChunkStorage {
&mut self,
pos: &ChunkPos,
data: &mut impl Read,
- ) -> Result<(), String> {
+ ) -> Result<(), BufReadError> {
if !self.in_range(pos) {
println!(
"Ignoring chunk since it's not in the view range: {}, {}",
@@ -109,14 +110,17 @@ pub struct Chunk {
}
impl Chunk {
- pub fn read_with_dimension(buf: &mut impl Read, data: &Dimension) -> Result<Self, String> {
+ pub fn read_with_dimension(
+ buf: &mut impl Read,
+ data: &Dimension,
+ ) -> Result<Self, BufReadError> {
Self::read_with_dimension_height(buf, data.height())
}
pub fn read_with_dimension_height(
buf: &mut impl Read,
dimension_height: u32,
- ) -> Result<Self, String> {
+ ) -> Result<Self, BufReadError> {
let section_count = dimension_height / SECTION_HEIGHT;
let mut sections = Vec::with_capacity(section_count as usize);
for _ in 0..section_count {
@@ -174,7 +178,7 @@ pub struct Section {
}
impl McBufReadable for Section {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let block_count = u16::read_from(buf)?;
// this is commented out because the vanilla server is wrong
@@ -187,11 +191,11 @@ impl McBufReadable for Section {
for i in 0..states.storage.size() {
if !BlockState::is_valid_state(states.storage.get(i) as u32) {
- return Err(format!(
+ return Err(BufReadError::Custom(format!(
"Invalid block state {} (index {}) found in section.",
states.storage.get(i),
i
- ));
+ )));
}
}
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());
diff --git a/azalea-world/src/palette.rs b/azalea-world/src/palette.rs
index 1b40f257..2473a7e5 100644
--- a/azalea-world/src/palette.rs
+++ b/azalea-world/src/palette.rs
@@ -1,4 +1,6 @@
-use azalea_buf::{McBufReadable, McBufVarReadable, McBufWritable, Readable, Writable};
+use azalea_buf::{
+ BufReadError, McBufReadable, McBufVarReadable, McBufWritable, Readable, Writable,
+};
use std::io::{Read, Write};
use crate::BitStorage;
@@ -22,7 +24,7 @@ impl PalettedContainer {
pub fn read_with_type(
buf: &mut impl Read,
type_: &'static PalettedContainerType,
- ) -> Result<Self, String> {
+ ) -> Result<Self, BufReadError> {
let bits_per_entry = buf.read_byte()?;
let palette = match type_ {
PalettedContainerType::BlockStates => {
@@ -90,7 +92,7 @@ impl Palette {
pub fn block_states_read_with_bits_per_entry(
buf: &mut impl Read,
bits_per_entry: u8,
- ) -> Result<Palette, String> {
+ ) -> Result<Palette, BufReadError> {
Ok(match bits_per_entry {
0 => Palette::SingleValue(u32::var_read_from(buf)?),
1..=4 => Palette::Linear(Vec::<u32>::var_read_from(buf)?),
@@ -102,7 +104,7 @@ impl Palette {
pub fn biomes_read_with_bits_per_entry(
buf: &mut impl Read,
bits_per_entry: u8,
- ) -> Result<Palette, String> {
+ ) -> Result<Palette, BufReadError> {
Ok(match bits_per_entry {
0 => Palette::SingleValue(u32::var_read_from(buf)?),
1..=3 => Palette::Linear(Vec::<u32>::var_read_from(buf)?),