|
3 | 3 | //! The most common ones are [`Vec3`] and [`BlockPos`], which are usually used
|
4 | 4 | //! for entity positions and block positions, respectively.
|
5 | 5 |
|
| 6 | +use crate::resource_location::ResourceLocation; |
6 | 7 | use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
|
| 8 | +#[cfg(feature = "serde")] |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | +use std::fmt::Display; |
| 11 | +use std::str::FromStr; |
7 | 12 | use std::{
|
8 | 13 | fmt,
|
9 | 14 | hash::Hash,
|
10 | 15 | io::{Cursor, Write},
|
11 | 16 | ops::{Add, AddAssign, Mul, Rem, Sub},
|
12 | 17 | };
|
13 |
| -#[cfg(feature = "serde")] |
14 |
| -use serde::{Deserialize, Serialize}; |
15 |
| -use crate::resource_location::ResourceLocation; |
16 | 18 |
|
17 | 19 | macro_rules! vec3_impl {
|
18 | 20 | ($name:ident, $type:ty) => {
|
@@ -270,6 +272,33 @@ impl BlockPos {
|
270 | 272 | }
|
271 | 273 | }
|
272 | 274 |
|
| 275 | +impl Display for BlockPos { |
| 276 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 277 | + write!(f, "({}, {}, {})", self.x, self.y, self.z) |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +impl FromStr for BlockPos { |
| 282 | + type Err = &'static str; |
| 283 | + |
| 284 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 285 | + let pos = s |
| 286 | + .trim_matches(|c| c == '(' || c == ')') |
| 287 | + .split(',') |
| 288 | + .collect::<Vec<_>>(); |
| 289 | + |
| 290 | + if pos.len() != 3 { |
| 291 | + return Err("Must be a length of 3"); |
| 292 | + } |
| 293 | + |
| 294 | + Ok(BlockPos { |
| 295 | + x: pos[0].parse()?, |
| 296 | + y: pos[1].parse()?, |
| 297 | + z: pos[2].parse()?, |
| 298 | + }) |
| 299 | + } |
| 300 | +} |
| 301 | + |
273 | 302 | /// Chunk coordinates are used to represent where a chunk is in the world. You
|
274 | 303 | /// can convert the x and z to block coordinates by multiplying them by 16.
|
275 | 304 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
0 commit comments