From 5354835d2827d916e5613002ceaab3449e33807b Mon Sep 17 00:00:00 2001 From: vyPal Date: Thu, 26 Dec 2024 15:09:19 +0100 Subject: [PATCH] Fix fmt and clippy --- plugins/hello-plugin-source/src/lib.rs | 27 +++++++------- pumpkin/src/command/tree_builder.rs | 3 ++ pumpkin/src/main.rs | 2 +- pumpkin/src/net/mod.rs | 50 -------------------------- pumpkin/src/plugin/api/context.rs | 2 +- pumpkin/src/plugin/mod.rs | 8 ++--- 6 files changed, 23 insertions(+), 69 deletions(-) diff --git a/plugins/hello-plugin-source/src/lib.rs b/plugins/hello-plugin-source/src/lib.rs index e006a4d0f..72ad6a008 100644 --- a/plugins/hello-plugin-source/src/lib.rs +++ b/plugins/hello-plugin-source/src/lib.rs @@ -1,18 +1,17 @@ +use async_trait::async_trait; +use pumpkin::command::args::ConsumedArgs; +use pumpkin::command::dispatcher::CommandError; +use pumpkin::command::tree::CommandTree; +use pumpkin::command::CommandExecutor; +use pumpkin::command::CommandSender; use pumpkin::plugin::api::types::player::PlayerEvent; use pumpkin::plugin::*; +use pumpkin::server::Server; use pumpkin_api_macros::{plugin_event, plugin_impl, plugin_method}; use pumpkin_core::text::color::NamedColor; use pumpkin_core::text::TextComponent; use serde::{Deserialize, Serialize}; use std::fs; -use pumpkin::command::tree::CommandTree; -use pumpkin::command::dispatcher::CommandError; -use pumpkin::command::args::ConsumedArgs; -use pumpkin::server::Server; -use pumpkin::command::CommandSender; -use pumpkin::command::CommandExecutor; -use async_trait::async_trait; -use pumpkin_protocol::client::play::CSystemChatMessage; #[derive(Serialize, Deserialize, Debug)] struct Config { @@ -80,11 +79,7 @@ async fn on_unload(&mut self, server: &Context) -> Result<(), String> { } #[plugin_event(blocking = true, priority = Highest)] -async fn on_player_join( - &mut self, - server: &Context, - player: &PlayerEvent, -) -> Result { +async fn on_player_join(&mut self, server: &Context, player: &PlayerEvent) -> Result { server.get_logger().info( format!( "Player {} joined the game. Config is {:#?}", @@ -138,3 +133,9 @@ impl MyPlugin { } } } + +impl Default for MyPlugin { + fn default() -> Self { + Self::new() + } +} diff --git a/pumpkin/src/command/tree_builder.rs b/pumpkin/src/command/tree_builder.rs index e750ea0bb..30945eb56 100644 --- a/pumpkin/src/command/tree_builder.rs +++ b/pumpkin/src/command/tree_builder.rs @@ -7,6 +7,7 @@ use super::CommandExecutor; impl<'a> CommandTree<'a> { /// Add a child [Node] to the root of this [`CommandTree`]. + #[must_use] pub fn with_child(mut self, child: impl NodeBuilder<'a>) -> Self { let node = child.build(&mut self); self.children.push(self.nodes.len()); @@ -15,6 +16,7 @@ impl<'a> CommandTree<'a> { } /// provide at least one name + #[must_use] pub fn new( names: [&'a str; NAME_COUNT], description: &'a str, @@ -42,6 +44,7 @@ impl<'a> CommandTree<'a> { /// desired type. /// /// Also see [`NonLeafNodeBuilder::execute`]. + #[must_use] pub fn execute(mut self, executor: &'a dyn CommandExecutor) -> Self { let node = Node { node_type: NodeType::ExecuteLeaf { executor }, diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index e15e8fd69..ebdf116e3 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -240,7 +240,7 @@ async fn main() { let client_clone = client.clone(); tokio::spawn(async move { - while let Some(_) = rx.recv().await { + while (rx.recv().await).is_some() { let mut enc = client_clone.enc.lock().await; let buf = enc.take(); if let Err(e) = connection_writer.lock().await.write_all(&buf).await { diff --git a/pumpkin/src/net/mod.rs b/pumpkin/src/net/mod.rs index 3d2fb2ac2..d36671766 100644 --- a/pumpkin/src/net/mod.rs +++ b/pumpkin/src/net/mod.rs @@ -501,56 +501,6 @@ impl Client { Ok(()) } - /// Reads the connection until our buffer of len 4096 is full, then decode - /// Close connection when an error occurs or when the Client closed the connection - /// Returns if connection is still open - /* pub async fn poll(&self) -> bool { - loop { - if self.closed.load(std::sync::atomic::Ordering::Relaxed) { - // If we manually close (like a kick) we dont want to keep reading bytes - return false; - } - - let mut dec = self.dec.lock().await; - - match dec.decode() { - Ok(Some(packet)) => { - self.add_packet(packet).await; - return true; - } - Ok(None) => (), //log::debug!("Waiting for more data to complete packet..."), - Err(err) => { - log::warn!("Failed to decode packet for: {}", err.to_string()); - self.close(); - return false; // return to avoid reserving additional bytes - } - } - - dec.reserve(4096); - let mut buf = dec.take_capacity(); - - let bytes_read = self.connection_reader.lock().await.read_buf(&mut buf).await; - match bytes_read { - Ok(cnt) => { - //log::debug!("Read {} bytes", cnt); - if cnt == 0 { - self.close(); - return false; - } - } - Err(error) => { - log::error!("Error while reading incoming packet {}", error); - self.close(); - return false; - } - }; - - // This should always be an O(1) unsplit because we reserved space earlier and - // the call to `read_buf` shouldn't have grown the allocation. - dec.queue_bytes(buf); - } - } */ - /// Disconnects a client from the server with a specified reason. /// /// This function kicks a client identified by its ID from the server. The appropriate disconnect packet is sent based on the client's current connection state. diff --git a/pumpkin/src/plugin/api/context.rs b/pumpkin/src/plugin/api/context.rs index 0e3cd8f0e..aa0e330cf 100644 --- a/pumpkin/src/plugin/api/context.rs +++ b/pumpkin/src/plugin/api/context.rs @@ -69,7 +69,7 @@ pub enum ContextAction { pub fn handle_context( metadata: PluginMetadata<'static>, /* , dispatcher: Arc> */ - server: Arc, + server: &Arc, ) -> Context { let (send, mut recv) = mpsc::channel(1); let server = server.clone(); diff --git a/pumpkin/src/plugin/mod.rs b/pumpkin/src/plugin/mod.rs index d301e48c0..35dda2138 100644 --- a/pumpkin/src/plugin/mod.rs +++ b/pumpkin/src/plugin/mod.rs @@ -103,7 +103,7 @@ impl PluginManager { // The chance that this will panic is non-existent, but just in case let context = handle_context( metadata.clone(), /* , dispatcher */ - self.server.clone().expect("Server not set"), + &self.server.clone().expect("Server not set"), ); let mut plugin_box = plugin_fn(); let res = plugin_box.on_load(&context).await; @@ -137,7 +137,7 @@ impl PluginManager { let context = handle_context( metadata.clone(), /* , dispatcher */ - self.server.clone().expect("Server not set"), + &self.server.clone().expect("Server not set"), ); let res = plugin.on_load(&context).await; res?; @@ -157,7 +157,7 @@ impl PluginManager { if let Some((metadata, plugin, _, loaded)) = plugin { let context = handle_context( metadata.clone(), /* , dispatcher */ - self.server.clone().expect("Server not set"), + &self.server.clone().expect("Server not set"), ); let res = plugin.on_unload(&context).await; res?; @@ -200,7 +200,7 @@ impl PluginManager { if let Some(matching_event) = registered_events.iter().find(|e| e.name == event_name) { let context = handle_context( metadata.clone(), /* , dispatcher.clone() */ - self.server.clone().expect("Server not set"), + &self.server.clone().expect("Server not set"), ); if matching_event.blocking {