diff --git a/pumpkin/src/client/container.rs b/pumpkin/src/client/container.rs index a4c0d9f72..02b37a8e3 100644 --- a/pumpkin/src/client/container.rs +++ b/pumpkin/src/client/container.rs @@ -16,7 +16,6 @@ use pumpkin_protocol::client::play::{ use pumpkin_protocol::server::play::SClickContainer; use pumpkin_protocol::slot::Slot; use pumpkin_world::item::ItemStack; -use std::ops::DerefMut; use std::sync::Arc; #[expect(unused)] @@ -211,10 +210,8 @@ impl Player { self.send_whole_container_change(server).await?; } else if let container_click::Slot::Normal(slot_index) = click.slot { let mut inventory = self.inventory.lock().await; - let combined_container = OptionallyCombinedContainer::new( - &mut inventory, - Some(opened_container.deref_mut()), - ); + let combined_container = + OptionallyCombinedContainer::new(&mut inventory, Some(&mut *opened_container)); if let Some(slot) = combined_container.get_slot_excluding_inventory(slot_index) { let slot = Slot::from(slot); drop(opened_container); diff --git a/pumpkin/src/entity/item.rs b/pumpkin/src/entity/item.rs index c99556679..f4e34e592 100644 --- a/pumpkin/src/entity/item.rs +++ b/pumpkin/src/entity/item.rs @@ -3,6 +3,8 @@ use crate::entity::{random_float, Entity}; use crate::server::Server; use crate::world::World; use crossbeam::atomic::AtomicCell; +use pumpkin_core::math::position::WorldPosition; +use pumpkin_core::math::vector2::Vector2; use pumpkin_core::math::vector3::Vector3; use pumpkin_entity::entity_type::EntityType; use pumpkin_entity::pose::EntityPose; @@ -52,8 +54,8 @@ impl ItemEntity { entity_type: EntityType::Item, world, pos: AtomicCell::new(empty), - block_pos: AtomicCell::new(Default::default()), - chunk_pos: AtomicCell::new(Default::default()), + block_pos: AtomicCell::new(WorldPosition::default()), + chunk_pos: AtomicCell::new(Vector2::default()), sneaking: false.into(), sprinting: false.into(), fall_flying: false.into(), @@ -97,17 +99,17 @@ impl ItemEntity { let player_pos = player_entity.pos.load(); let pos = Vector3 { x: player_pos.x, - y: player_pos.y + player_entity.standing_eye_height as f64 - 0.3, + y: player_pos.y + f64::from(player_entity.standing_eye_height) - 0.3, z: player_pos.z, }; Self::spawn( pos, - toss_velocity(player_entity).into(), + toss_velocity(player_entity), player_entity.world.clone(), item_stack, server, ) - .await + .await; } pub(self) async fn check_pickup(self) -> PickupEvent { @@ -247,7 +249,7 @@ impl ItemEntity { || self.entity.velocity.load().horizontal_length_squared() > 1.0e-5 || ticks % 4 == 0 { - self.entity.advance_position().await; + self.entity.advance_position(); self.entity.collision_check(true).await; let on_ground = self.entity.on_ground.load(Ordering::Relaxed); let slipperiness = 0.98 * if on_ground { 0.6 } else { 1. }; @@ -262,10 +264,8 @@ impl ItemEntity { velocity.y = 0.; velocity.x = 0.; } - if on_ground { - if velocity.y < 0. { - velocity = velocity.multiply(1., -0.5, 1.); - } + if on_ground && velocity.y < 0. { + velocity = velocity.multiply(1., -0.5, 1.); } self.entity.velocity.store(velocity); @@ -289,16 +289,16 @@ enum PickupEvent { fn toss_velocity(player: &Entity) -> Vector3 { use std::f64::consts::PI; - let pitch_sin = f64::sin(player.pitch.load() as f64 * (PI / 180.0)); - let pitch_cos = f64::cos(player.pitch.load() as f64 * (PI / 180.0)); - let yaw_sin = f64::sin(player.yaw.load() as f64 * (PI / 180.0)); - let yaw_cos = f64::cos(player.yaw.load() as f64 * (PI / 180.0)); + let pitch_sin = f64::sin(f64::from(player.pitch.load()) * (PI / 180.0)); + let pitch_cos = f64::cos(f64::from(player.pitch.load()) * (PI / 180.0)); + let yaw_sin = f64::sin(f64::from(player.yaw.load()) * (PI / 180.0)); + let yaw_cos = f64::cos(f64::from(player.yaw.load()) * (PI / 180.0)); let random_angle = random_float() * (2.0 * PI); let random_offset = 0.02 * random_float(); Vector3 { - x: (-yaw_sin * pitch_cos * 0.3) + f64::cos(random_angle) * random_offset, - y: -pitch_sin * 0.3 + 0.1 + (random_float() - random_float()) * 0.1, - z: (yaw_cos * pitch_cos * 0.3) + f64::sin(random_angle) * random_offset, + x: (-yaw_sin * pitch_cos).mul_add(0.3, f64::cos(random_angle) * random_offset), + y: (-pitch_sin).mul_add(0.3, (random_float() - random_float()).mul_add(0.1, 0.1)), + z: (yaw_cos * pitch_cos).mul_add(0.3, f64::sin(random_angle) * random_offset), } } diff --git a/pumpkin/src/entity/mod.rs b/pumpkin/src/entity/mod.rs index 27b52fb15..bcaacaf43 100644 --- a/pumpkin/src/entity/mod.rs +++ b/pumpkin/src/entity/mod.rs @@ -141,7 +141,7 @@ impl Entity { self.world.remove_entity(self).await; } - pub async fn advance_position(&self) { + pub fn advance_position(&self) { let mut velocity = self.velocity.load(); let on_ground = self.on_ground.load(Ordering::Relaxed); if on_ground && velocity.y.is_sign_negative() { @@ -164,7 +164,7 @@ impl Entity { let z_section = get_section_cord(future_position.z.floor() as i32); let chunk_pos = Vector2::new(x_section, z_section); if !chunks.contains(&chunk_pos) { - chunks.push(chunk_pos) + chunks.push(chunk_pos); } } @@ -190,25 +190,25 @@ impl Entity { z: future_position.z.floor(), y: future_position.y.floor(), })); - if !block_id.is_air() { - if pos.y > future_position.y || !self.on_ground.load(Ordering::Relaxed) { - let mut new_pos = pos; - new_pos.y = pos.y.floor(); - if self.on_ground.load(Ordering::Relaxed) { - let velocity = self.velocity.load(); - self.velocity.store(velocity.multiply(1., 0., 1.)); - } - self.on_ground.store(true, Ordering::Relaxed); - if snap { - self.set_pos(new_pos.x, new_pos.y, new_pos.z); - } - } - } else { + if block_id.is_air() { self.on_ground.store(false, Ordering::Relaxed); + } else if pos.y > future_position.y || !self.on_ground.load(Ordering::Relaxed) { + let mut new_pos = pos; + new_pos.y = pos.y.floor(); + if self.on_ground.load(Ordering::Relaxed) { + let velocity = self.velocity.load(); + self.velocity.store(velocity.multiply(1., 0., 1.)); + } + self.on_ground.store(true, Ordering::Relaxed); + if snap { + self.set_pos(new_pos.x, new_pos.y, new_pos.z); + } } } } } + + #[allow(clippy::cast_precision_loss)] fn add_velocity_block_by_block(&self) -> Vec> { let velocity = self.velocity.load(); let pos = self.pos.load(); @@ -216,18 +216,18 @@ impl Entity { if velocity > 0. { if velocity > 1. { for i in (1..=(velocity.ceil() as i32)).rev() { - out.push(i as f64); + out.push(f64::from(i)); } } else { - out.push(1.) + out.push(1.); } } else if velocity < 0. { if velocity < -1. { for i in ((velocity.floor() as i32)..0).rev() { - out.push(i as f64); + out.push(f64::from(i)); } } else { - out.push(-1.) + out.push(-1.); } } out.iter_mut().for_each(|velocity| *velocity += pos.round()); @@ -247,7 +247,7 @@ impl Entity { let increment = (last - first) / length as f64; *other = (0..length) .map(|i| first + increment * i as f64) - .collect_vec() + .collect_vec(); } }; let (x_len, y_len, z_len) = (x.len(), y.len(), z.len()); @@ -263,8 +263,8 @@ impl Entity { } x.into_iter() - .zip(y.into_iter()) - .zip(z.into_iter()) + .zip(y) + .zip(z) .map(|((x, y), z)| Vector3 { x, y, z }) .collect_vec() } @@ -272,9 +272,9 @@ impl Entity { pub async fn send_position(&self, old_position: Vector3, server: &Arc) { let pos = self.pos.load(); let (dx, dy, dz) = ( - pos.x * 4096. - old_position.x * 4096., - pos.y * 4096. - old_position.y * 4096., - pos.z * 4096. - old_position.z * 4096., + pos.x.mul_add(4096., -(old_position.x * 4096.)), + pos.y.mul_add(4096., -(old_position.y * 4096.)), + pos.z.mul_add(4096., -(old_position.z * 4096.)), ); server .broadcast_packet_all(&CUpdateEntityPos::new( @@ -291,7 +291,7 @@ impl Entity { self.velocity.load(); let entity_id = self.entity_id.into(); let packet = CEntityVelocity::new(&entity_id, self.velocity.load()); - server.broadcast_packet_all(&packet).await + server.broadcast_packet_all(&packet).await; } /// Applies knockback to the entity, following vanilla Minecraft's mechanics. /// @@ -403,6 +403,7 @@ pub enum Flag { FallFlying, } +#[must_use] pub fn random_float() -> f64 { rand::thread_rng().gen_range(0.0..=1.0) } diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 160d04a52..95ec50fbe 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -212,7 +212,7 @@ impl World { let id = player.gameprofile.id; for (_, existing_player) in self.current_players.lock().await.iter().filter(|c| c.0 != &id) { let entity = &existing_player.living_entity.entity; - player.client.send_packet(&CSpawnEntity::from(entity)).await + player.client.send_packet(&CSpawnEntity::from(entity)).await; } // entity meta data // set skin parts @@ -350,7 +350,7 @@ impl World { // Stream the chunks (don't collect them and then do stuff with them) pub fn receive_chunks(&self, chunks: Vec>) -> Receiver>> { - if chunks.len() == 0 { + if chunks.is_empty() { return vec![]; } let (sender, receive) = mpsc::channel(chunks.len()); @@ -381,16 +381,8 @@ impl World { item_count: 1, }; ItemEntity::spawn( - Vector3 { - x: position.0.x as f64, - y: position.0.y as f64, - z: position.0.z as f64, - }, - Vector3 { - x: 0., - y: 0., - z: 0., - }, + Vector3::default(), + Vector3::default(), self.clone(), fake_item_stack, server,