Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryntet committed Oct 23, 2024
1 parent 6e186ee commit 4723459
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 61 deletions.
7 changes: 2 additions & 5 deletions pumpkin/src/client/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 17 additions & 17 deletions pumpkin/src/entity/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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. };
Expand All @@ -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);
Expand All @@ -289,16 +289,16 @@ enum PickupEvent {

fn toss_velocity(player: &Entity) -> Vector3<f64> {
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),
}
}
55 changes: 28 additions & 27 deletions pumpkin/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
}

Expand All @@ -190,44 +190,44 @@ 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<Vector3<f64>> {
let velocity = self.velocity.load();
let pos = self.pos.load();
let blocks = |velocity: f64, out: &mut Vec<f64>, pos: f64| {
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());
Expand All @@ -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());
Expand All @@ -263,18 +263,18 @@ 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()
}

pub async fn send_position(&self, old_position: Vector3<f64>, server: &Arc<Server>) {
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(
Expand All @@ -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.
///
Expand Down Expand Up @@ -403,6 +403,7 @@ pub enum Flag {
FallFlying,
}

#[must_use]
pub fn random_float() -> f64 {
rand::thread_rng().gen_range(0.0..=1.0)
}
16 changes: 4 additions & 12 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Vector2<i32>>) -> Receiver<Arc<RwLock<ChunkData>>> {
if chunks.len() == 0 {
if chunks.is_empty() {
return vec![];
}
let (sender, receive) = mpsc::channel(chunks.len());
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4723459

Please sign in to comment.