Skip to content

Commit

Permalink
New clipps lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Oct 8, 2024
1 parent fb5171b commit 4967e9a
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 59 deletions.
25 changes: 11 additions & 14 deletions pumpkin/src/client/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Player {
let window_title = container
.as_ref()
.map(|container| container.window_name())
.unwrap_or(inventory.window_name());
.unwrap_or_else(|| inventory.window_name());
let title = TextComponent::text(window_title);

self.client.send_packet(&COpenScreen::new(
Expand All @@ -64,13 +64,12 @@ impl Player {
.map(Slot::from)
.collect_vec();

let carried_item = {
if let Some(item) = self.carried_item.load().as_ref() {
item.into()
} else {
Slot::empty()
}
};
let carried_item = self
.carried_item
.load()
.as_ref()
.map_or_else(Slot::empty, |item| item.into());

// Gets the previous value
let i = inventory
.state_id
Expand Down Expand Up @@ -378,7 +377,7 @@ impl Player {
}
}

async fn get_current_players_in_container(&self, server: &Server) -> Vec<Arc<Player>> {
async fn get_current_players_in_container(&self, server: &Server) -> Vec<Arc<Self>> {
let player_ids = {
let open_containers = server.open_containers.read();
open_containers
Expand Down Expand Up @@ -453,10 +452,8 @@ impl Player {
}

pub fn get_open_container(&self, server: &Server) -> Option<Arc<Mutex<Box<dyn Container>>>> {
if let Some(id) = self.open_container.load() {
server.try_get_container(self.entity_id(), id)
} else {
None
}
self.open_container
.load()
.map_or_else(|| None, |id| server.try_get_container(self.entity_id(), id))
}
}
2 changes: 1 addition & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ impl Client {
match self.connection_state.load() {
ConnectionState::Login => {
self.try_send_packet(&CLoginDisconnect::new(
&serde_json::to_string_pretty(&reason).unwrap_or("".into()),
&serde_json::to_string_pretty(&reason).unwrap_or_else(|_| "".into()),
))
.unwrap_or_else(|_| self.close());
}
Expand Down
12 changes: 6 additions & 6 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ impl Player {
&[self.client.token],
&CUpdateEntityPos::new(
entity_id.into(),
(x * 4096.0 - lastx * 4096.0) as i16,
(y * 4096.0 - lasty * 4096.0) as i16,
(z * 4096.0 - lastz * 4096.0) as i16,
x.mul_add(4096.0, -(lastx * 4096.0)) as i16,
y.mul_add(4096.0, -(lasty * 4096.0)) as i16,
z.mul_add(4096.0, -(lastz * 4096.0)) as i16,
position.ground,
),
);
Expand Down Expand Up @@ -180,9 +180,9 @@ impl Player {
&[self.client.token],
&CUpdateEntityPosRot::new(
entity_id.into(),
(x * 4096.0 - lastx * 4096.0) as i16,
(y * 4096.0 - lasty * 4096.0) as i16,
(z * 4096.0 - lastz * 4096.0) as i16,
x.mul_add(4096.0, -(lastx * 4096.0)) as i16,
y.mul_add(4096.0, -(lasty * 4096.0)) as i16,
z.mul_add(4096.0, -(lastz * 4096.0)) as i16,
yaw as u8,
pitch as u8,
position_rotation.ground,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_echest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const NAMES: [&str; 2] = ["echest", "enderchest"];
const DESCRIPTION: &str =
"Show your personal enderchest (this command is used for testing container behaviour)";

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, server, _| {
if let Some(player) = sender.as_mut_player() {
let entity_id = player.entity_id();
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_gamemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result<GameMode, Inva
}
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(&|sender| sender.permission_lvl() >= 2).with_child(
argument(ARG_GAMEMODE, consume_arg_gamemode)
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn parse_arg_command<'a>(
.map_err(|_| InvalidConsumptionError(Some(command_name.into())))
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION)
.with_child(
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, server, args| {
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn consume_arg_target(_src: &CommandSender, args: &mut RawArgs) -> Option<St
consume_arg_player(_src, args)
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).with_child(
argument(ARG_TARGET, consume_arg_target).execute(&|sender, server, args| {
let target = parse_arg_player(sender, server, ARG_TARGET, args)?;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_pumpkin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const NAMES: [&str; 1] = ["pumpkin"];

const DESCRIPTION: &str = "Display information about Pumpkin.";

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, _, _| {
let version = env!("CARGO_PKG_VERSION");
let description = env!("CARGO_PKG_DESCRIPTION");
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const NAMES: [&str; 1] = ["stop"];

const DESCRIPTION: &str = "Stop the server.";

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(&|sender| sender.permission_lvl() >= 4).execute(&|sender, _, _args| {
sender
Expand Down
6 changes: 3 additions & 3 deletions pumpkin/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ impl<'a> CommandSender<'a> {
}
}

pub fn is_player(&self) -> bool {
pub const fn is_player(&self) -> bool {
match self {
CommandSender::Console => false,
CommandSender::Player(_) => true,
CommandSender::Rcon(_) => false,
}
}

pub fn is_console(&self) -> bool {
pub const fn is_console(&self) -> bool {
match self {
CommandSender::Console => true,
CommandSender::Player(_) => false,
Expand All @@ -59,7 +59,7 @@ impl<'a> CommandSender<'a> {
}

/// todo: implement
pub fn permission_lvl(&self) -> i32 {
pub const fn permission_lvl(&self) -> i32 {
match self {
CommandSender::Rcon(_) => 4,
CommandSender::Console => 4,
Expand Down
14 changes: 7 additions & 7 deletions pumpkin/src/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use crate::commands::CommandSender;
use std::collections::{HashMap, VecDeque};

/// see [crate::commands::tree_builder::argument]
pub(crate) type RawArgs<'a> = Vec<&'a str>;
pub type RawArgs<'a> = Vec<&'a str>;

/// see [crate::commands::tree_builder::argument] and [CommandTree::execute]/[crate::commands::tree_builder::NonLeafNodeBuilder::execute]
pub(crate) type ConsumedArgs<'a> = HashMap<&'a str, String>;
pub type ConsumedArgs<'a> = HashMap<&'a str, String>;

/// see [crate::commands::tree_builder::argument]
pub(crate) type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;
pub type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;

pub(crate) struct Node<'a> {
pub struct Node<'a> {
pub(crate) children: Vec<usize>,
pub(crate) node_type: NodeType<'a>,
}

pub(crate) enum NodeType<'a> {
pub enum NodeType<'a> {
ExecuteLeaf {
run: &'a RunFunctionType,
},
Expand All @@ -32,12 +32,12 @@ pub(crate) enum NodeType<'a> {
},
}

pub(crate) enum Command<'a> {
pub enum Command<'a> {
Tree(CommandTree<'a>),
Alias(&'a str),
}

pub(crate) struct CommandTree<'a> {
pub struct CommandTree<'a> {
pub(crate) nodes: Vec<Node<'a>>,
pub(crate) children: Vec<usize>,
pub(crate) names: Vec<&'a str>,
Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/commands/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'a> NodeBuilder<'a> for NonLeafNodeBuilder<'a> {

impl<'a> NonLeafNodeBuilder<'a> {
/// Add a child [Node] to this one.
pub fn with_child(mut self, child: NonLeafNodeBuilder<'a>) -> Self {
pub fn with_child(mut self, child: Self) -> Self {
self.child_nodes.push(child);
self
}
Expand All @@ -124,7 +124,7 @@ impl<'a> NonLeafNodeBuilder<'a> {

/// Matches a sting literal.
#[expect(dead_code)] // todo: remove (so far no commands requiring this are implemented)
pub fn literal(string: &str) -> NonLeafNodeBuilder {
pub const fn literal(string: &str) -> NonLeafNodeBuilder {
NonLeafNodeBuilder {
node_type: NodeType::Literal { string },
child_nodes: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Entity {
// This has some vanilla magic
let mut x = x;
let mut z = z;
while x * x + z * z < 1.0E-5 {
while x.mul_add(x, z * z) < 1.0E-5 {
x = (rand::random::<f64>() - rand::random::<f64>()) * 0.01;
z = (rand::random::<f64>() - rand::random::<f64>()) * 0.01;
}
Expand Down
12 changes: 6 additions & 6 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ impl Player {
entity_id: EntityId,
gamemode: GameMode,
) -> Self {
let gameprofile = match client.gameprofile.lock().clone() {
Some(profile) => profile,
None => {
let gameprofile = client.gameprofile.lock().clone().map_or_else(
|| {
log::error!("No gameprofile?. Impossible");
GameProfile {
id: uuid::Uuid::new_v4(),
name: "".to_string(),
properties: vec![],
profile_actions: None,
}
}
};
},
|profile| profile,
);
let config = client.config.lock().clone().unwrap_or_default();
Self {
entity: Entity::new(entity_id, world, EntityType::Player, 1.62),
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Player {
self.entity.world.remove_player(self);
}

pub fn entity_id(&self) -> EntityId {
pub const fn entity_id(&self) -> EntityId {
self.entity.entity_id
}

Expand Down
13 changes: 13 additions & 0 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#![deny(clippy::all)]
// #![warn(clippy::pedantic)]
// #![warn(clippy::restriction)]
#![warn(clippy::nursery)]
#![warn(clippy::cargo)]
// expect
#![expect(clippy::cargo_common_metadata)]
#![expect(clippy::multiple_crate_versions)]
#![expect(clippy::while_float)]
#![expect(clippy::significant_drop_in_scrutinee)]
#![expect(clippy::significant_drop_tightening)]
#![expect(clippy::future_not_send)]
#![expect(clippy::single_call_fn)]
#![expect(clippy::await_holding_lock)]

#[cfg(target_os = "wasi")]
Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/rcon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum RCONError {

const SERVER: Token = Token(0);

pub struct RCONServer {}
pub struct RCONServer;

impl RCONServer {
pub async fn new(config: &RCONConfig, server: Arc<Server>) -> Result<Self, io::Error> {
Expand Down Expand Up @@ -122,7 +122,7 @@ pub struct RCONClient {
}

impl RCONClient {
pub fn new(connection: TcpStream) -> Self {
pub const fn new(connection: TcpStream) -> Self {
Self {
connection,
logged_in: false,
Expand Down
14 changes: 7 additions & 7 deletions pumpkin/src/rcon/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use thiserror::Error;
use tokio::io::AsyncReadExt;

/// Client -> Server
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ServerboundPacket {
/// Typically, the first packet sent by the client, which is used to authenticate the connection with the server.
Expand All @@ -19,7 +19,7 @@ pub enum ServerboundPacket {
}

impl ServerboundPacket {
pub fn from_i32(n: i32) -> Self {
pub const fn from_i32(n: i32) -> Self {
match n {
3 => Self::Auth,
2 => Self::ExecCommand,
Expand All @@ -28,7 +28,7 @@ impl ServerboundPacket {
}
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
/// Server -> Client
pub enum ClientboundPacket {
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct Packet {
}

impl Packet {
pub async fn deserialize(incoming: &mut Vec<u8>) -> Result<Option<Packet>, PacketError> {
pub async fn deserialize(incoming: &mut Vec<u8>) -> Result<Option<Self>, PacketError> {
if incoming.len() < 4 {
return Ok(None);
}
Expand All @@ -98,7 +98,7 @@ impl Packet {
}
incoming.drain(0..len as usize);

let packet = Packet {
let packet = Self {
id,
ptype: ServerboundPacket::from_i32(ty),
body: String::from_utf8(payload).map_err(PacketError::InvalidBody)?,
Expand All @@ -110,11 +110,11 @@ impl Packet {
&self.body
}

pub fn get_type(&self) -> ServerboundPacket {
pub const fn get_type(&self) -> ServerboundPacket {
self.ptype
}

pub fn get_id(&self) -> i32 {
pub const fn get_id(&self) -> i32 {
self.id
}
}
6 changes: 3 additions & 3 deletions pumpkin/src/server/connection_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub struct CachedBranding {
}

impl CachedBranding {
pub fn new() -> CachedBranding {
pub fn new() -> Self {
let cached_server_brand = Self::build_brand();
CachedBranding {
Self {
cached_server_brand,
}
}
Expand All @@ -46,7 +46,7 @@ impl CachedStatus {
let status_response_json = serde_json::to_string(&status_response)
.expect("Failed to parse Status response into JSON");

CachedStatus {
Self {
_status_response: status_response,
status_response_json,
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/server/key_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl KeyStore {
&private_key.e().to_bytes_be(),
)
.into_boxed_slice();
KeyStore {
Self {
_public_key: public_key,
private_key,
public_key_der,
Expand Down

0 comments on commit 4967e9a

Please sign in to comment.