From cc0f30848ea597ef702558cb1e5fc57ed2a20372 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Thu, 19 Dec 2024 01:25:19 +0100 Subject: [PATCH] More clippy lints Also made some vars NonZero --- .github/workflows/rust.yml | 4 +-- pumpkin-config/src/lib.rs | 18 ++++++---- pumpkin-protocol/src/lib.rs | 4 ++- pumpkin-protocol/src/var_int.rs | 10 +++--- pumpkin-protocol/src/var_long.rs | 10 +++--- .../src/cylindrical_chunk_iterator.rs | 20 ++++++----- pumpkin/src/block/block_manager.rs | 3 +- pumpkin/src/client/client_packet.rs | 16 +++++---- pumpkin/src/client/mod.rs | 5 +-- pumpkin/src/client/player_packet.rs | 35 ++++++++++--------- pumpkin/src/command/args/arg_block.rs | 14 ++++---- pumpkin/src/command/args/arg_bounded_num.rs | 4 +-- pumpkin/src/command/args/arg_command.rs | 9 +++-- pumpkin/src/command/args/arg_entities.rs | 2 +- pumpkin/src/command/args/arg_entity.rs | 2 +- pumpkin/src/command/args/arg_gamemode.rs | 2 +- pumpkin/src/command/args/arg_item.rs | 14 ++++---- pumpkin/src/command/args/arg_message.rs | 2 +- pumpkin/src/command/args/arg_players.rs | 8 ++--- pumpkin/src/command/args/arg_position_2d.rs | 2 +- pumpkin/src/command/args/arg_position_3d.rs | 2 +- .../src/command/args/arg_position_block.rs | 2 +- pumpkin/src/command/args/arg_rotation.rs | 2 +- pumpkin/src/command/dispatcher.rs | 4 +-- pumpkin/src/entity/player.rs | 3 +- pumpkin/src/main.rs | 19 ++++++++-- pumpkin/src/server/connection_cache.rs | 7 ++-- pumpkin/src/world/bossbar.rs | 2 +- pumpkin/src/world/custom_bossbar.rs | 2 +- pumpkin/src/world/mod.rs | 6 ++-- pumpkin/src/world/player_chunker.rs | 14 ++++---- 31 files changed, 141 insertions(+), 106 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ccfd6ea8..193c05eb 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -32,7 +32,7 @@ jobs: - uses: actions/checkout@v4 - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} - uses: Swatinem/rust-cache@v2 - - run: cargo clippy --all-targets --all-features --no-default-features + - run: cargo clippy --all-targets --all-features build_and_test: name: Build project and test runs-on: ${{ matrix.os }} @@ -77,4 +77,4 @@ jobs: - uses: actions/checkout@v4 - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} - uses: Swatinem/rust-cache@v2 - - run: cargo clippy --release --all-targets --all-features --no-default-features + - run: cargo clippy --release --all-targets --all-features diff --git a/pumpkin-config/src/lib.rs b/pumpkin-config/src/lib.rs index a69c5349..6e2035c0 100644 --- a/pumpkin-config/src/lib.rs +++ b/pumpkin-config/src/lib.rs @@ -7,6 +7,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::{ fs, net::{Ipv4Addr, SocketAddr}, + num::NonZeroU8, path::Path, sync::LazyLock, }; @@ -71,9 +72,9 @@ pub struct BasicConfiguration { /// The maximum number of players allowed on the server. Specifying `0` disables the limit. pub max_players: u32, /// The maximum view distance for players. - pub view_distance: u8, + pub view_distance: NonZeroU8, /// The maximum simulated view distance. - pub simulation_distance: u8, + pub simulation_distance: NonZeroU8, /// The default game difficulty. pub default_difficulty: Difficulty, /// Whether the Nether dimension is enabled. @@ -103,8 +104,8 @@ impl Default for BasicConfiguration { server_address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565), seed: "".to_string(), max_players: 100000, - view_distance: 10, - simulation_distance: 10, + view_distance: NonZeroU8::new(10).unwrap(), + simulation_distance: NonZeroU8::new(10).unwrap(), default_difficulty: Difficulty::Normal, allow_nether: true, hardcore: false, @@ -176,9 +177,14 @@ impl LoadConfiguration for BasicConfiguration { } fn validate(&self) { - assert!(self.view_distance >= 2, "View distance must be at least 2"); assert!( - self.view_distance <= 32, + self.view_distance + .ge(unsafe { &NonZeroU8::new_unchecked(2) }), + "View distance must be at least 2" + ); + assert!( + self.view_distance + .le(unsafe { &NonZeroU8::new_unchecked(32) }), "View distance must be less than 32" ); if self.online_mode { diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index 336e86f0..f5b5db46 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroU16; + use bytebuf::{packet_id::Packet, ReadingError}; use bytes::{Bytes, BytesMut}; use pumpkin_core::text::{style::Style, TextComponent}; @@ -19,7 +21,7 @@ pub use var_long::*; /// To current Minecraft protocol /// Don't forget to change this when porting -pub const CURRENT_MC_PROTOCOL: u32 = 769; +pub const CURRENT_MC_PROTOCOL: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(769) }; pub const MAX_PACKET_SIZE: i32 = 2097152; diff --git a/pumpkin-protocol/src/var_int.rs b/pumpkin-protocol/src/var_int.rs index 608287fd..1401230a 100644 --- a/pumpkin-protocol/src/var_int.rs +++ b/pumpkin-protocol/src/var_int.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroUsize; + use bytes::{Buf, BufMut}; use serde::{ de::{SeqAccess, Visitor}, @@ -15,7 +17,7 @@ pub struct VarInt(pub VarIntType); impl VarInt { /// The maximum number of bytes a `VarInt` can occupy. - pub const MAX_SIZE: usize = 5; + pub const MAX_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(5) }; /// Returns the exact number of bytes this varint will write when /// [`Encode::encode`] is called, assuming no error occurs. @@ -28,7 +30,7 @@ impl VarInt { pub fn encode(&self, w: &mut impl BufMut) { let mut val = self.0; - for _ in 0..Self::MAX_SIZE { + for _ in 0..Self::MAX_SIZE.get() { let b: u8 = val as u8 & 0b01111111; val >>= 7; w.put_u8(if val == 0 { b } else { b | 0b10000000 }); @@ -40,7 +42,7 @@ impl VarInt { pub fn decode(r: &mut impl Buf) -> Result { let mut val = 0; - for i in 0..Self::MAX_SIZE { + for i in 0..Self::MAX_SIZE.get() { if !r.has_remaining() { return Err(VarIntDecodeError::Incomplete); } @@ -130,7 +132,7 @@ impl<'de> Deserialize<'de> for VarInt { A: SeqAccess<'de>, { let mut val = 0; - for i in 0..VarInt::MAX_SIZE { + for i in 0..VarInt::MAX_SIZE.get() { if let Some(byte) = seq.next_element::()? { val |= (i32::from(byte) & 0b01111111) << (i * 7); if byte & 0b10000000 == 0 { diff --git a/pumpkin-protocol/src/var_long.rs b/pumpkin-protocol/src/var_long.rs index cbb800c5..72f5c11e 100644 --- a/pumpkin-protocol/src/var_long.rs +++ b/pumpkin-protocol/src/var_long.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroUsize; + use bytes::{Buf, BufMut}; use serde::{ de::{self, SeqAccess, Visitor}, @@ -15,7 +17,7 @@ pub struct VarLong(pub VarLongType); impl VarLong { /// The maximum number of bytes a `VarLong` - pub const MAX_SIZE: usize = 10; + pub const MAX_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10) }; /// Returns the exact number of bytes this varlong will write when /// [`Encode::encode`] is called, assuming no error occurs. @@ -28,7 +30,7 @@ impl VarLong { pub fn encode(&self, w: &mut impl BufMut) { let mut x = self.0; - for _ in 0..Self::MAX_SIZE { + for _ in 0..Self::MAX_SIZE.get() { let byte = (x & 0x7F) as u8; x >>= 7; if x == 0 { @@ -41,7 +43,7 @@ impl VarLong { pub fn decode(r: &mut impl Buf) -> Result { let mut val = 0; - for i in 0..Self::MAX_SIZE { + for i in 0..Self::MAX_SIZE.get() { if !r.has_remaining() { return Err(VarLongDecodeError::Incomplete); } @@ -131,7 +133,7 @@ impl<'de> Deserialize<'de> for VarLong { A: SeqAccess<'de>, { let mut val = 0; - for i in 0..VarLong::MAX_SIZE { + for i in 0..VarLong::MAX_SIZE.get() { if let Some(byte) = seq.next_element::()? { val |= (i64::from(byte) & 0b01111111) << (i * 7); if byte & 0b10000000 == 0 { diff --git a/pumpkin-world/src/cylindrical_chunk_iterator.rs b/pumpkin-world/src/cylindrical_chunk_iterator.rs index 7e388261..d8b65fe3 100644 --- a/pumpkin-world/src/cylindrical_chunk_iterator.rs +++ b/pumpkin-world/src/cylindrical_chunk_iterator.rs @@ -1,13 +1,15 @@ +use std::num::NonZeroU8; + use pumpkin_core::math::vector2::Vector2; #[derive(Debug, Clone, Copy, PartialEq)] pub struct Cylindrical { pub center: Vector2, - pub view_distance: u8, + pub view_distance: NonZeroU8, } impl Cylindrical { - pub fn new(center: Vector2, view_distance: u8) -> Self { + pub fn new(center: Vector2, view_distance: NonZeroU8) -> Self { Self { center, view_distance, @@ -36,19 +38,19 @@ impl Cylindrical { } fn left(&self) -> i32 { - self.center.x - self.view_distance as i32 - 1 + self.center.x - self.view_distance.get() as i32 - 1 } fn bottom(&self) -> i32 { - self.center.z - self.view_distance as i32 - 1 + self.center.z - self.view_distance.get() as i32 - 1 } fn right(&self) -> i32 { - self.center.x + self.view_distance as i32 + 1 + self.center.x + self.view_distance.get() as i32 + 1 } fn top(&self) -> i32 { - self.center.z + self.view_distance as i32 + 1 + self.center.z + self.view_distance.get() as i32 + 1 } fn is_within_distance(&self, x: i32, z: i32) -> bool { @@ -59,7 +61,7 @@ impl Cylindrical { let min_leg = rel_x.min(rel_z) as i64; let hyp_sqr = max_leg * max_leg + min_leg * min_leg; - hyp_sqr < (self.view_distance as i64 * self.view_distance as i64) + hyp_sqr < (self.view_distance.get() as i64 * self.view_distance.get() as i64) } /// Returns an iterator of all chunks within this cylinder @@ -82,12 +84,14 @@ impl Cylindrical { #[cfg(test)] mod test { + use std::num::NonZeroU8; + use super::Cylindrical; use pumpkin_core::math::vector2::Vector2; #[test] fn test_bounds() { - let cylinder = Cylindrical::new(Vector2::new(0, 0), 10); + let cylinder = Cylindrical::new(Vector2::new(0, 0), unsafe { NonZeroU8::new_unchecked(1) }); for chunk in cylinder.all_chunks_within() { assert!(chunk.x >= cylinder.left() && chunk.x <= cylinder.right()); assert!(chunk.z >= cylinder.bottom() && chunk.z <= cylinder.top()); diff --git a/pumpkin/src/block/block_manager.rs b/pumpkin/src/block/block_manager.rs index 01b3c08a..92395bb8 100644 --- a/pumpkin/src/block/block_manager.rs +++ b/pumpkin/src/block/block_manager.rs @@ -21,8 +21,7 @@ pub struct BlockManager { impl BlockManager { pub fn register(&mut self, block: T) { - self.blocks - .insert(block.name().to_string(), Arc::new(block)); + self.blocks.insert(block.name(), Arc::new(block)); } pub async fn on_use( diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index a6e41944..dfe913e1 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -29,7 +29,10 @@ use pumpkin_protocol::{ }, ConnectionState, KnownPack, VarInt, CURRENT_MC_PROTOCOL, }; -use std::sync::LazyLock; +use std::{ + num::{NonZeroI32, NonZeroU8}, + sync::LazyLock, +}; use uuid::Uuid; static LINKS: LazyLock> = LazyLock::new(|| { @@ -106,7 +109,7 @@ impl Client { self.connection_state.store(handshake.next_state); if self.connection_state.load() != ConnectionState::Status { let protocol = version; - match protocol.cmp(&(CURRENT_MC_PROTOCOL as i32)) { + match protocol.cmp(&NonZeroI32::from(CURRENT_MC_PROTOCOL).get()) { std::cmp::Ordering::Less => { self.kick(&format!("Client outdated ({protocol}), Server uses Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL}")).await; } @@ -132,10 +135,7 @@ impl Client { } fn is_valid_player_name(name: &str) -> bool { - name.len() <= 16 - && name - .chars() - .all(|c| c > 32_u8 as char && c < 127_u8 as char) + name.len() <= 16 && name.chars().all(|c| c > 32u8 as char && c < 127u8 as char) } pub async fn handle_login_start(&self, server: &Server, login_start: SLoginStart) { @@ -400,7 +400,9 @@ impl Client { ) { *self.config.lock().await = Some(PlayerConfig { locale: client_information.locale, - view_distance: client_information.view_distance as u8, + view_distance: unsafe { + NonZeroU8::new_unchecked(client_information.view_distance as u8) + }, chat_mode, chat_colors: client_information.chat_colors, skin_parts: client_information.skin_parts, diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index 9fd6fed0..1e3ce5e5 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -1,6 +1,7 @@ use std::{ collections::VecDeque, net::SocketAddr, + num::NonZeroU8, sync::{ atomic::{AtomicBool, AtomicI32}, Arc, @@ -53,7 +54,7 @@ pub struct PlayerConfig { /// The player's preferred language. pub locale: String, // 16 /// The maximum distance at which chunks are rendered. - pub view_distance: u8, + pub view_distance: NonZeroU8, /// The player's chat mode settings pub chat_mode: ChatMode, /// Whether chat colors are enabled. @@ -72,7 +73,7 @@ impl Default for PlayerConfig { fn default() -> Self { Self { locale: "en_us".to_string(), - view_distance: 2, + view_distance: unsafe { NonZeroU8::new_unchecked(10) }, chat_mode: ChatMode::Enabled, chat_colors: true, skin_parts: 0, diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index b5dfd447..920de697 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -1,3 +1,4 @@ +use std::num::NonZeroU8; use std::sync::Arc; use super::PlayerConfig; @@ -436,7 +437,7 @@ impl Player { } pub async fn handle_client_information( - self: &Arc, + self: &Arc, client_information: SClientInformationPlay, ) { if let (Some(main_hand), Some(chat_mode)) = ( @@ -458,25 +459,27 @@ impl Player { let old_view_distance = config.view_distance; - let update_watched = if old_view_distance == client_information.view_distance as u8 - { - false - } else { - log::debug!( - "Player {} ({}) updated render distance: {} -> {}.", - self.gameprofile.name, - self.client.id, - old_view_distance, - client_information.view_distance - ); - - true - }; + let update_watched = + if old_view_distance.get() == client_information.view_distance as u8 { + false + } else { + log::debug!( + "Player {} ({}) updated render distance: {} -> {}.", + self.gameprofile.name, + self.client.id, + old_view_distance, + client_information.view_distance + ); + + true + }; *config = PlayerConfig { locale: client_information.locale, // A Negative view distance would be impossible and make no sense right ?, Mojang: Lets make is signed :D - view_distance: client_information.view_distance as u8, + view_distance: unsafe { + NonZeroU8::new_unchecked(client_information.view_distance as u8) + }, chat_mode, chat_colors: client_information.chat_colors, skin_parts: client_information.skin_parts, diff --git a/pumpkin/src/command/args/arg_block.rs b/pumpkin/src/command/args/arg_block.rs index 41e3ebd3..fe11228f 100644 --- a/pumpkin/src/command/args/arg_block.rs +++ b/pumpkin/src/command/args/arg_block.rs @@ -65,12 +65,14 @@ impl<'a> FindArg<'a> for BlockArgumentConsumer { fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result { match args.get(name) { - Some(Arg::Block(name)) => match block_registry::get_block(name) { - Some(block) => Ok(block), - None => Err(CommandError::GeneralCommandIssue(format!( - "Block {name} does not exist." - ))), - }, + Some(Arg::Block(name)) => block_registry::get_block(name).map_or_else( + || { + Err(CommandError::GeneralCommandIssue(format!( + "Block {name} does not exist." + ))) + }, + Result::Ok, + ), _ => Err(CommandError::InvalidConsumption(Some(name.to_string()))), } } diff --git a/pumpkin/src/command/args/arg_bounded_num.rs b/pumpkin/src/command/args/arg_bounded_num.rs index 4a9f718b..41410367 100644 --- a/pumpkin/src/command/args/arg_bounded_num.rs +++ b/pumpkin/src/command/args/arg_bounded_num.rs @@ -22,7 +22,7 @@ pub(crate) struct BoundedNumArgumentConsumer { #[async_trait] impl ArgumentConsumer for BoundedNumArgumentConsumer where - BoundedNumArgumentConsumer: GetClientSideArgParser, + Self: GetClientSideArgParser, { async fn consume<'a>( &self, @@ -236,7 +236,7 @@ impl GetClientSideArgParser for BoundedNumArgumentConsumer { impl DefaultNameArgConsumer for BoundedNumArgumentConsumer where - BoundedNumArgumentConsumer: ArgumentConsumer, + Self: ArgumentConsumer, { fn default_name(&self) -> &'static str { // setting a single default name for all BoundedNumArgumentConsumer variants is probably a bad idea since it would lead to confusion diff --git a/pumpkin/src/command/args/arg_command.rs b/pumpkin/src/command/args/arg_command.rs index ef933977..93955987 100644 --- a/pumpkin/src/command/args/arg_command.rs +++ b/pumpkin/src/command/args/arg_command.rs @@ -38,10 +38,9 @@ impl ArgumentConsumer for CommandTreeArgumentConsumer { let s = args.pop()?; let dispatcher = &server.command_dispatcher; - return match dispatcher.get_tree(s) { - Ok(tree) => Some(Arg::CommandTree(tree)), - Err(_) => None, - }; + return dispatcher + .get_tree(s) + .map_or_else(|_| None, |tree| Some(Arg::CommandTree(tree))); } async fn suggest<'a>( @@ -71,7 +70,7 @@ impl DefaultNameArgConsumer for CommandTreeArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &CommandTreeArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_entities.rs b/pumpkin/src/command/args/arg_entities.rs index 9f077597..102a177b 100644 --- a/pumpkin/src/command/args/arg_entities.rs +++ b/pumpkin/src/command/args/arg_entities.rs @@ -62,7 +62,7 @@ impl DefaultNameArgConsumer for EntitiesArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &EntitiesArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_entity.rs b/pumpkin/src/command/args/arg_entity.rs index dce52240..37e4dac0 100644 --- a/pumpkin/src/command/args/arg_entity.rs +++ b/pumpkin/src/command/args/arg_entity.rs @@ -84,7 +84,7 @@ impl DefaultNameArgConsumer for EntityArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &EntityArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_gamemode.rs b/pumpkin/src/command/args/arg_gamemode.rs index 469a92f9..d520b25c 100644 --- a/pumpkin/src/command/args/arg_gamemode.rs +++ b/pumpkin/src/command/args/arg_gamemode.rs @@ -65,7 +65,7 @@ impl DefaultNameArgConsumer for GamemodeArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &GamemodeArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_item.rs b/pumpkin/src/command/args/arg_item.rs index 3deeb2fe..481feb7d 100644 --- a/pumpkin/src/command/args/arg_item.rs +++ b/pumpkin/src/command/args/arg_item.rs @@ -63,12 +63,14 @@ impl<'a> FindArg<'a> for ItemArgumentConsumer { fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result { match args.get(name) { - Some(Arg::Item(name)) => match item_registry::get_item(name) { - Some(item) => Ok((name, item)), - None => Err(CommandError::GeneralCommandIssue(format!( - "Item {name} does not exist." - ))), - }, + Some(Arg::Item(name)) => item_registry::get_item(name).map_or_else( + || { + Err(CommandError::GeneralCommandIssue(format!( + "Item {name} does not exist." + ))) + }, + |item| Ok((*name, item)), + ), _ => Err(CommandError::InvalidConsumption(Some(name.to_string()))), } } diff --git a/pumpkin/src/command/args/arg_message.rs b/pumpkin/src/command/args/arg_message.rs index e2815de2..00af2d87 100644 --- a/pumpkin/src/command/args/arg_message.rs +++ b/pumpkin/src/command/args/arg_message.rs @@ -60,7 +60,7 @@ impl DefaultNameArgConsumer for MsgArgConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &MsgArgConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_players.rs b/pumpkin/src/command/args/arg_players.rs index 7c06f8e4..0e8876a7 100644 --- a/pumpkin/src/command/args/arg_players.rs +++ b/pumpkin/src/command/args/arg_players.rs @@ -53,11 +53,7 @@ impl ArgumentConsumer for PlayersArgumentConsumer { _ => None, }, "@r" => { - if let Some(p) = server.get_random_player().await { - Some(vec![p.clone()]) - } else { - Some(vec![]) - } + (server.get_random_player().await).map_or_else(|| Some(vec![]), |p| Some(vec![p])) } "@a" | "@e" => Some(server.get_all_players().await), name => server.get_player_by_name(name).await.map(|p| vec![p]), @@ -82,7 +78,7 @@ impl DefaultNameArgConsumer for PlayersArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &PlayersArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_position_2d.rs b/pumpkin/src/command/args/arg_position_2d.rs index 9dc45073..107ea432 100644 --- a/pumpkin/src/command/args/arg_position_2d.rs +++ b/pumpkin/src/command/args/arg_position_2d.rs @@ -78,7 +78,7 @@ impl DefaultNameArgConsumer for Position2DArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &Position2DArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_position_3d.rs b/pumpkin/src/command/args/arg_position_3d.rs index 4940b4db..1e58ad81 100644 --- a/pumpkin/src/command/args/arg_position_3d.rs +++ b/pumpkin/src/command/args/arg_position_3d.rs @@ -81,7 +81,7 @@ impl DefaultNameArgConsumer for Position3DArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &Position3DArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_position_block.rs b/pumpkin/src/command/args/arg_position_block.rs index b8ae25db..861eec12 100644 --- a/pumpkin/src/command/args/arg_position_block.rs +++ b/pumpkin/src/command/args/arg_position_block.rs @@ -82,7 +82,7 @@ impl DefaultNameArgConsumer for BlockPosArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &BlockPosArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/args/arg_rotation.rs b/pumpkin/src/command/args/arg_rotation.rs index 4efdbde0..6e53f2f9 100644 --- a/pumpkin/src/command/args/arg_rotation.rs +++ b/pumpkin/src/command/args/arg_rotation.rs @@ -66,7 +66,7 @@ impl DefaultNameArgConsumer for RotationArgumentConsumer { } fn get_argument_consumer(&self) -> &dyn ArgumentConsumer { - &RotationArgumentConsumer + &Self } } diff --git a/pumpkin/src/command/dispatcher.rs b/pumpkin/src/command/dispatcher.rs index 9448c505..e63a67ac 100644 --- a/pumpkin/src/command/dispatcher.rs +++ b/pumpkin/src/command/dispatcher.rs @@ -32,11 +32,11 @@ impl CommandError { pub fn into_string_or_pumpkin_error(self, cmd: &str) -> Result> { match self { InvalidConsumption(s) => { - println!("Error while parsing command \"{cmd}\": {s:?} was consumed, but couldn't be parsed"); + log::error!("Error while parsing command \"{cmd}\": {s:?} was consumed, but couldn't be parsed"); Ok("Internal Error (See logs for details)".into()) } InvalidRequirement => { - println!("Error while parsing command \"{cmd}\": a requirement that was expected was not met."); + log::error!("Error while parsing command \"{cmd}\": a requirement that was expected was not met."); Ok("Internal Error (See logs for details)".into()) } GeneralCommandIssue(s) => Ok(s), diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index cc8af323..31ef1131 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -1,4 +1,5 @@ use std::{ + num::NonZeroU8, sync::{ atomic::{AtomicBool, AtomicI32, AtomicI64, AtomicU32, AtomicU8}, Arc, @@ -176,7 +177,7 @@ impl Player { // (We left shift by one so we can search around that chunk) watched_section: AtomicCell::new(Cylindrical::new( Vector2::new(i32::MAX >> 1, i32::MAX >> 1), - 0, + unsafe { NonZeroU8::new_unchecked(1) }, )), wait_for_keep_alive: AtomicBool::new(false), keep_alive_id: AtomicI64::new(0), diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index d33cd9e1..c2547e78 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -2,7 +2,22 @@ #![deny(clippy::pedantic)] // #![warn(clippy::restriction)] #![deny(clippy::cargo)] +// to keep consistency #![deny(clippy::if_then_some_else_none)] +#![deny(clippy::empty_enum_variants_with_brackets)] +#![deny(clippy::empty_structs_with_brackets)] +#![deny(clippy::separated_literal_suffix)] +#![deny(clippy::semicolon_outside_block)] +#![deny(clippy::non_zero_suggestions)] +#![deny(clippy::string_lit_chars_any)] +#![deny(clippy::use_self)] +#![deny(clippy::useless_let_if_seq)] +#![deny(clippy::branches_sharing_code)] +#![deny(clippy::equatable_if_let)] +#![deny(clippy::option_if_let_else)] +// use log crate +#![deny(clippy::print_stdout)] +#![deny(clippy::print_stderr)] // REMOVE SOME WHEN RELEASE #![expect(clippy::cargo_common_metadata)] #![expect(clippy::multiple_crate_versions)] @@ -182,8 +197,8 @@ async fn main() { let server = server.clone(); tokio::spawn(async move { ticker.run(&server).await; - }); - } + }) + }; let mut master_client_id: u16 = 0; loop { diff --git a/pumpkin/src/server/connection_cache.rs b/pumpkin/src/server/connection_cache.rs index 8e6fce8f..4171ea13 100644 --- a/pumpkin/src/server/connection_cache.rs +++ b/pumpkin/src/server/connection_cache.rs @@ -2,6 +2,7 @@ use core::error; use std::{ fs::File, io::{Cursor, Read}, + num::NonZeroU32, path::Path, }; @@ -105,7 +106,7 @@ impl CachedStatus { } pub fn build_response(config: &BasicConfiguration) -> StatusResponse { - let icon = if config.use_favicon { + let favicon = if config.use_favicon { let icon_path = &config.favicon_path; log::debug!("Loading server favicon from '{}'", icon_path); match load_icon_from_file(icon_path).or_else(|err| { @@ -142,7 +143,7 @@ impl CachedStatus { StatusResponse { version: Some(Version { name: CURRENT_MC_VERSION.into(), - protocol: CURRENT_MC_PROTOCOL, + protocol: NonZeroU32::from(CURRENT_MC_PROTOCOL).get(), }), players: Some(Players { max: config.max_players, @@ -150,7 +151,7 @@ impl CachedStatus { sample: vec![], }), description: config.motd.clone(), - favicon: icon, + favicon, enforce_secure_chat: false, } } diff --git a/pumpkin/src/world/bossbar.rs b/pumpkin/src/world/bossbar.rs index fdc06243..dc9bb74d 100644 --- a/pumpkin/src/world/bossbar.rs +++ b/pumpkin/src/world/bossbar.rs @@ -43,7 +43,7 @@ pub struct Bossbar { impl Bossbar { #[must_use] - pub fn new(title: String) -> Bossbar { + pub fn new(title: String) -> Self { let uuid = Uuid::new_v4(); Self { diff --git a/pumpkin/src/world/custom_bossbar.rs b/pumpkin/src/world/custom_bossbar.rs index f6d02679..99a7a0b8 100644 --- a/pumpkin/src/world/custom_bossbar.rs +++ b/pumpkin/src/world/custom_bossbar.rs @@ -53,7 +53,7 @@ impl Default for CustomBossbars { impl CustomBossbars { #[must_use] - pub fn new() -> CustomBossbars { + pub fn new() -> Self { Self { custom_bossbars: HashMap::new(), } diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 54c5d105..dcfab4b9 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -242,8 +242,8 @@ impl World { base_config.hardcore, &dimensions, base_config.max_players.into(), - base_config.view_distance.into(), // TODO: view distance - base_config.simulation_distance.into(), // TODO: sim view dinstance + base_config.view_distance.get().into(), // TODO: view distance + base_config.simulation_distance.get().into(), // TODO: sim view dinstance false, true, false, @@ -324,7 +324,7 @@ impl World { .client .send_packet(&CPlayerInfoUpdate::new(0x01 | 0x08, &entries)) .await; - } + }; let gameprofile = &player.gameprofile; diff --git a/pumpkin/src/world/player_chunker.rs b/pumpkin/src/world/player_chunker.rs index a7c6f8a5..c0ba854e 100644 --- a/pumpkin/src/world/player_chunker.rs +++ b/pumpkin/src/world/player_chunker.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{num::NonZeroU8, sync::Arc}; use pumpkin_config::BASIC_CONFIG; use pumpkin_core::{ @@ -10,13 +10,11 @@ use pumpkin_world::cylindrical_chunk_iterator::Cylindrical; use crate::entity::player::Player; -pub async fn get_view_distance(player: &Player) -> u8 { - player - .config - .lock() - .await - .view_distance - .clamp(2, BASIC_CONFIG.view_distance) +pub async fn get_view_distance(player: &Player) -> NonZeroU8 { + player.config.lock().await.view_distance.clamp( + unsafe { NonZeroU8::new_unchecked(2) }, + BASIC_CONFIG.view_distance, + ) } pub async fn player_join(player: &Arc) {