Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serde to BlockPos #172

Closed
wants to merge 10 commits into from
5 changes: 1 addition & 4 deletions azalea-chat/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ impl IntoIterator for FormattedText {
let siblings = base.siblings.clone();
let mut v: Vec<FormattedText> = Vec::with_capacity(siblings.len() + 1);
v.push(self);
for sibling in siblings {
v.extend(sibling);
}

v.extend(siblings);
v.into_iter()
}

Expand Down
12 changes: 6 additions & 6 deletions azalea-client/src/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Client {
pub struct LeftClickMine;

#[allow(clippy::type_complexity)]
fn handle_auto_mine(
pub fn handle_auto_mine(
mut query: Query<
(
&HitResultComponent,
Expand Down Expand Up @@ -155,7 +155,7 @@ pub struct StartMiningBlockEvent {
pub entity: Entity,
pub position: BlockPos,
}
fn handle_start_mining_block_event(
pub fn handle_start_mining_block_event(
mut events: EventReader<StartMiningBlockEvent>,
mut start_mining_events: EventWriter<StartMiningBlockWithDirectionEvent>,
mut query: Query<&HitResultComponent>,
Expand Down Expand Up @@ -184,7 +184,7 @@ pub struct StartMiningBlockWithDirectionEvent {
pub direction: Direction,
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
fn handle_start_mining_block_with_direction_event(
pub fn handle_start_mining_block_with_direction_event(
mut events: EventReader<StartMiningBlockWithDirectionEvent>,
mut finish_mining_events: EventWriter<FinishMiningBlockEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>,
Expand Down Expand Up @@ -418,7 +418,7 @@ pub struct FinishMiningBlockEvent {
pub position: BlockPos,
}

fn handle_finish_mining_block_event(
pub fn handle_finish_mining_block_event(
mut events: EventReader<FinishMiningBlockEvent>,
mut query: Query<(
&InstanceName,
Expand Down Expand Up @@ -484,7 +484,7 @@ fn handle_finish_mining_block_event(
pub struct StopMiningBlockEvent {
pub entity: Entity,
}
fn handle_stop_mining_block_event(
pub fn handle_stop_mining_block_event(
mut events: EventReader<StopMiningBlockEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>,
mut mine_block_progress_events: EventWriter<MineBlockProgressEvent>,
Expand Down Expand Up @@ -517,7 +517,7 @@ fn handle_stop_mining_block_event(
}

#[allow(clippy::too_many_arguments, clippy::type_complexity)]
fn continue_mining_block(
pub fn continue_mining_block(
mut query: Query<(
Entity,
&InstanceName,
Expand Down
49 changes: 47 additions & 2 deletions azalea-core/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
//! The most common ones are [`Vec3`] and [`BlockPos`], which are usually used
//! for entity positions and block positions, respectively.

use crate::resource_location::ResourceLocation;
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::{
fmt,
hash::Hash,
io::{Cursor, Write},
ops::{Add, AddAssign, Mul, Rem, Sub},
};

use crate::resource_location::ResourceLocation;
use std::num::{ParseFloatError, ParseIntError};

macro_rules! vec3_impl {
($name:ident, $type:ty) => {
Expand Down Expand Up @@ -211,6 +214,7 @@ macro_rules! vec3_impl {
/// Used to represent an exact position in the world where an entity could be.
/// For blocks, [`BlockPos`] is used instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, McBuf)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Vec3 {
pub x: f64,
pub y: f64,
Expand All @@ -235,6 +239,7 @@ impl Vec3 {
/// The coordinates of a block in the world. For entities (if the coordinate
/// with decimals), use [`Vec3`] instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct BlockPos {
pub x: i32,
pub y: i32,
Expand Down Expand Up @@ -571,6 +576,46 @@ impl fmt::Display for Vec3 {
}
}

impl FromStr for BlockPos {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let pos = s
.split(' ')
.collect::<Vec<_>>();

if pos.len() != 3 {
return Err("Must be a length of 3".to_string());
}

Ok(BlockPos {
x: pos[0].parse().map_err(|e: ParseIntError| e.to_string())?,
y: pos[1].parse().map_err(|e: ParseIntError| e.to_string())?,
z: pos[2].parse().map_err(|e: ParseIntError| e.to_string())?,
})
}
}

impl FromStr for Vec3 {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let pos = s
.split(' ')
.collect::<Vec<_>>();

if pos.len() != 3 {
return Err("Must be a length of 3".to_string());
}

Ok(Vec3 {
x: pos[0].parse().map_err(|e: ParseFloatError| e.to_string())?,
y: pos[1].parse().map_err(|e: ParseFloatError| e.to_string())?,
z: pos[2].parse().map_err(|e: ParseFloatError| e.to_string())?,
})
}
}

const PACKED_X_LENGTH: u64 = 1 + 25; // minecraft does something a bit more complicated to get this 25
const PACKED_Z_LENGTH: u64 = PACKED_X_LENGTH;
const PACKED_Y_LENGTH: u64 = 64 - PACKED_X_LENGTH - PACKED_Z_LENGTH;
Expand Down
2 changes: 1 addition & 1 deletion azalea-physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Plugin for PhysicsPlugin {
/// Move the entity with the given acceleration while handling friction,
/// gravity, collisions, and some other stuff.
#[allow(clippy::type_complexity)]
fn travel(
pub fn travel(
mut query: Query<
(
&mut Physics,
Expand Down
4 changes: 2 additions & 2 deletions azalea/src/nearest_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ where
position: &'a Position,
instance_name: &'a InstanceName,
max_distance: f64,
) -> impl Iterator<Item = (Entity, f64)> + '_ {
) -> impl Iterator<Item = (Entity, f64)> + 'a {
self.filtered_entities
.iter()
.filter_map(move |(target_entity, e_instance, e_pos)| {
Expand All @@ -156,7 +156,7 @@ where
&'a self,
entity: Entity,
max_distance: f64,
) -> impl Iterator<Item = (Entity, f64)> + '_ {
) -> impl Iterator<Item = (Entity, f64)> + 'a {
let position;
let instance_name;
if let Ok((pos, instance)) = self.all_entities.get(entity) {
Expand Down
2 changes: 1 addition & 1 deletion azalea/src/pathfinder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ pub fn recalculate_near_end_of_path(
}

#[allow(clippy::type_complexity)]
fn tick_execute_path(
pub fn tick_execute_path(
mut query: Query<(
Entity,
&mut ExecutingPath,
Expand Down
Loading