diff options
| -rw-r--r-- | bot/src/main.rs | 6 | ||||
| -rw-r--r-- | minecraft-chat/src/base_component.rs | 6 | ||||
| -rw-r--r-- | minecraft-chat/src/component.rs | 13 | ||||
| -rw-r--r-- | minecraft-chat/src/style.rs | 19 | ||||
| -rw-r--r-- | minecraft-chat/src/text_component.rs | 6 | ||||
| -rw-r--r-- | minecraft-client/src/connect.rs | 4 | ||||
| -rw-r--r-- | minecraft-client/src/ping.rs | 8 | ||||
| -rw-r--r-- | minecraft-protocol/src/connect.rs | 9 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/game/mod.rs | 14 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/handshake/client_intention_packet.rs | 8 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/login/clientbound_hello_packet.rs | 3 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/login/serverbound_hello_packet.rs | 5 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/mod.rs | 4 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs | 7 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/status/mod.rs | 2 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs | 3 | ||||
| -rw-r--r-- | minecraft-protocol/src/read.rs | 2 |
17 files changed, 48 insertions, 71 deletions
diff --git a/bot/src/main.rs b/bot/src/main.rs index 2d62208d..ca5c698a 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,9 +1,9 @@ -use minecraft_client::{connect::join_server, ping::ping_server}; +use minecraft_client::connect::join_server; use tokio::runtime::Runtime; async fn bot() { - let address = "localhost:63425"; - let response = join_server(&address.try_into().unwrap()).await.unwrap(); + let address = "localhost:62072"; + let _response = join_server(&address.try_into().unwrap()).await.unwrap(); // println!("{}", response.description.to_ansi(None)); println!("connected"); } diff --git a/minecraft-chat/src/base_component.rs b/minecraft-chat/src/base_component.rs index b07e08e7..fa39a11c 100644 --- a/minecraft-chat/src/base_component.rs +++ b/minecraft-chat/src/base_component.rs @@ -15,3 +15,9 @@ impl BaseComponent { } } } + +impl Default for BaseComponent { + fn default() -> Self { + Self::new() + } +} diff --git a/minecraft-chat/src/component.rs b/minecraft-chat/src/component.rs index 22e803bd..1ed1f836 100644 --- a/minecraft-chat/src/component.rs +++ b/minecraft-chat/src/component.rs @@ -1,7 +1,4 @@ -use serde::{ - de::{self, Error}, - Deserialize, Deserializer, -}; +use serde::{de, Deserialize, Deserializer}; use crate::{ base_component::BaseComponent, @@ -59,7 +56,7 @@ impl Component { /// Convert this component into an ansi string pub fn to_ansi(&self, default_style: Option<&Style>) -> String { // default the default_style to white if it's not set - let default_style: &Style = default_style.unwrap_or_else(|| &DEFAULT_STYLE); + let default_style: &Style = default_style.unwrap_or(&DEFAULT_STYLE); // this contains the final string will all the ansi escape codes let mut built_string = String::new(); @@ -132,10 +129,10 @@ impl<'de> Deserialize<'de> for Component { if json.get("with").is_some() { let with = json.get("with").unwrap().as_array().unwrap(); let mut with_array = Vec::with_capacity(with.len()); - for i in 0..with.len() { + for item in with { // if it's a string component with no styling and no siblings, just add a string to with_array // otherwise add the component to the array - let c = Component::deserialize(&with[i]).map_err(de::Error::custom)?; + let c = Component::deserialize(item).map_err(de::Error::custom)?; if let Component::Text(text_component) = c { if text_component.base.siblings.is_empty() && text_component.base.style.is_empty() @@ -145,7 +142,7 @@ impl<'de> Deserialize<'de> for Component { } } with_array.push(StringOrComponent::Component( - Component::deserialize(&with[i]).map_err(de::Error::custom)?, + Component::deserialize(item).map_err(de::Error::custom)?, )); } component = diff --git a/minecraft-chat/src/style.rs b/minecraft-chat/src/style.rs index 7b333e5f..4e3b24de 100644 --- a/minecraft-chat/src/style.rs +++ b/minecraft-chat/src/style.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, fmt}; use serde_json::Value; @@ -15,9 +15,9 @@ impl TextColor { let n = u32::from_str_radix(&n, 16).unwrap(); return Ok(TextColor::from_rgb(n)); } - let color = NAMED_COLORS.get(&value.to_ascii_uppercase()); - if color.is_some() { - return Ok(color.unwrap().clone()); + let color_option = NAMED_COLORS.get(&value.to_ascii_uppercase()); + if let Some(color) = color_option { + return Ok(color.clone()); } Err(format!("Invalid color {}", value)) } @@ -52,9 +52,6 @@ lazy_static! { }; } -/// The weird S character Minecraft used to use for chat formatting -const PREFIX_CODE: char = '\u{00a7}'; - #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct ChatFormatting<'a> { pub name: &'a str, @@ -178,12 +175,14 @@ impl TextColor { pub fn format(&self) -> String { format!("#{:06X}", self.value) } +} - pub fn to_string(&self) -> String { +impl fmt::Display for TextColor { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(name) = &self.name { - name.clone() + write!(f, "{}", name.clone()) } else { - self.format() + write!(f, "{}", self.format()) } } } diff --git a/minecraft-chat/src/text_component.rs b/minecraft-chat/src/text_component.rs index 25341086..6c43f8b7 100644 --- a/minecraft-chat/src/text_component.rs +++ b/minecraft-chat/src/text_component.rs @@ -1,10 +1,6 @@ use std::fmt; -use crate::{ - base_component::BaseComponent, - component::Component, - style::{ChatFormatting, Style, TextColor}, -}; +use crate::{base_component::BaseComponent, component::Component, style::ChatFormatting}; #[derive(Clone, Debug)] pub struct TextComponent { diff --git a/minecraft-client/src/connect.rs b/minecraft-client/src/connect.rs index e6a23612..4bf937db 100644 --- a/minecraft-client/src/connect.rs +++ b/minecraft-client/src/connect.rs @@ -34,9 +34,9 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { // encryption request let packet = conn.read().await.unwrap(); - let encryption_request_packet = match packet { + let _encryption_request_packet = match packet { LoginPacket::ClientboundHelloPacket(p) => p, - _ => Err(format!("Invalid packet type: {:?}", packet))?, + _ => return Err(format!("Invalid packet type: {:?}", packet)), }; // TODO: client auth diff --git a/minecraft-client/src/ping.rs b/minecraft-client/src/ping.rs index 88c0a24a..ff869d4e 100644 --- a/minecraft-client/src/ping.rs +++ b/minecraft-client/src/ping.rs @@ -37,8 +37,8 @@ pub async fn ping_server( let packet = conn.read().await.unwrap(); - Ok(match packet { - StatusPacket::ClientboundStatusResponsePacket(p) => p, - _ => Err("Invalid packet type".to_string())?, - }) + match packet { + StatusPacket::ClientboundStatusResponsePacket(p) => Ok(*p), + _ => Err("Invalid packet type".to_string()), + } } diff --git a/minecraft-protocol/src/connect.rs b/minecraft-protocol/src/connect.rs index 7c6a5f18..f6dd9fe7 100644 --- a/minecraft-protocol/src/connect.rs +++ b/minecraft-protocol/src/connect.rs @@ -4,15 +4,10 @@ use crate::packets::game::GamePacket; use crate::packets::handshake::HandshakePacket; use crate::packets::login::LoginPacket; use crate::packets::status::StatusPacket; -use crate::packets::{ConnectionProtocol, ProtocolPacket}; use crate::read::read_packet; use crate::write::write_packet; -use crate::{mc_buf, packets::Packet, ServerIpAddress}; -use tokio::io::AsyncWriteExt; -use tokio::{ - io::{AsyncReadExt, BufReader}, - net::TcpStream, -}; +use crate::ServerIpAddress; +use tokio::net::TcpStream; pub enum PacketFlow { ClientToServer, diff --git a/minecraft-protocol/src/packets/game/mod.rs b/minecraft-protocol/src/packets/game/mod.rs index 06d8ae4c..a3ef2541 100644 --- a/minecraft-protocol/src/packets/game/mod.rs +++ b/minecraft-protocol/src/packets/game/mod.rs @@ -13,22 +13,16 @@ where #[async_trait] impl ProtocolPacket for GamePacket { fn id(&self) -> u32 { - match self { - _ => 0x00, - } + 0x00 } - fn write(&self, buf: &mut Vec<u8>) { - match self { - _ => (), - } - } + fn write(&self, _buf: &mut Vec<u8>) {} /// Read a packet by its id, ConnectionProtocol, and flow async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - id: u32, + _id: u32, flow: &PacketFlow, - buf: &mut BufReader<T>, + _buf: &mut BufReader<T>, ) -> Result<GamePacket, String> where Self: Sized, diff --git a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs index 54a08f39..3f7e3b37 100644 --- a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs +++ b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,12 +1,8 @@ use std::hash::Hash; -use async_trait::async_trait; use tokio::io::BufReader; -use crate::{ - mc_buf, - packets::{ConnectionProtocol, Packet}, -}; +use crate::{mc_buf, packets::ConnectionProtocol}; use super::HandshakePacket; @@ -32,7 +28,7 @@ impl ClientIntentionPacket { } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - buf: &mut BufReader<T>, + _buf: &mut BufReader<T>, ) -> Result<HandshakePacket, String> { Err("ClientIntentionPacket::parse not implemented".to_string()) // Ok(ClientIntentionPacket {}.get()) diff --git a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs index 7e69e3ce..2041497a 100644 --- a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs @@ -1,8 +1,7 @@ -use async_trait::async_trait; use std::hash::Hash; use tokio::io::BufReader; -use crate::{mc_buf, packets::Packet}; +use crate::mc_buf; use super::LoginPacket; diff --git a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs index 9415fe82..69448074 100644 --- a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs @@ -1,8 +1,7 @@ -use async_trait::async_trait; use std::hash::Hash; use tokio::io::BufReader; -use crate::{mc_buf, packets::Packet}; +use crate::mc_buf; use super::LoginPacket; @@ -21,7 +20,7 @@ impl ServerboundHelloPacket { } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - buf: &mut BufReader<T>, + _buf: &mut BufReader<T>, ) -> Result<LoginPacket, String> { Err("ServerboundHelloPacket::read not implemented".to_string()) } diff --git a/minecraft-protocol/src/packets/mod.rs b/minecraft-protocol/src/packets/mod.rs index d55bd364..a074b570 100644 --- a/minecraft-protocol/src/packets/mod.rs +++ b/minecraft-protocol/src/packets/mod.rs @@ -4,7 +4,7 @@ pub mod login; pub mod status; use async_trait::async_trait; -use tokio::io::{AsyncRead, BufReader}; +use tokio::io::BufReader; use crate::connect::PacketFlow; @@ -23,7 +23,7 @@ pub enum Packet { Game(game::GamePacket), Handshake(handshake::HandshakePacket), Login(login::LoginPacket), - Status(status::StatusPacket), + Status(Box<status::StatusPacket>), } /// An enum of packets for a certain protocol diff --git a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs index f19ab024..fc5d8a2a 100644 --- a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -1,10 +1,9 @@ -use async_trait::async_trait; use minecraft_chat::component::Component; -use serde::{Deserialize, Deserializer}; +use serde::Deserialize; use serde_json::Value; use tokio::io::BufReader; -use crate::{mc_buf, packets::Packet}; +use crate::mc_buf; use super::StatusPacket; @@ -38,7 +37,7 @@ pub struct ClientboundStatusResponsePacket { impl ClientboundStatusResponsePacket { pub fn get(self) -> StatusPacket { - StatusPacket::ClientboundStatusResponsePacket(self) + StatusPacket::ClientboundStatusResponsePacket(Box::new(self)) } pub fn write(&self, _buf: &mut Vec<u8>) {} diff --git a/minecraft-protocol/src/packets/status/mod.rs b/minecraft-protocol/src/packets/status/mod.rs index 2696cc07..ac6a34e1 100644 --- a/minecraft-protocol/src/packets/status/mod.rs +++ b/minecraft-protocol/src/packets/status/mod.rs @@ -17,7 +17,7 @@ where serverbound_status_request_packet::ServerboundStatusRequestPacket, ), ClientboundStatusResponsePacket( - clientbound_status_response_packet::ClientboundStatusResponsePacket, + Box<clientbound_status_response_packet::ClientboundStatusResponsePacket>, ), } diff --git a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs index 8b29391a..6a58da1f 100644 --- a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -1,9 +1,6 @@ -use async_trait::async_trait; use std::hash::Hash; use tokio::io::BufReader; -use crate::packets::{Packet, ProtocolPacket}; - use super::StatusPacket; #[derive(Hash, Clone, Debug)] diff --git a/minecraft-protocol/src/read.rs b/minecraft-protocol/src/read.rs index 7f4eeaac..f0c82641 100644 --- a/minecraft-protocol/src/read.rs +++ b/minecraft-protocol/src/read.rs @@ -22,7 +22,7 @@ pub async fn read_packet<P: ProtocolPacket>( // if we recognize the packet id, parse it - let packet = P::read(packet_id.try_into().unwrap(), &flow, &mut buf).await?; + let packet = P::read(packet_id.try_into().unwrap(), flow, &mut buf).await?; Ok(packet) } |
