From 1b602d0be793394ac2b0d009b77c6a877a4efb7d Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 15:32:04 -0500 Subject: fix errors and warnings --- azalea-client/src/client.rs | 104 ++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 52 deletions(-) (limited to 'azalea-client') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index b904fa92..8b456276 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -11,13 +11,13 @@ use azalea_protocol::{ serverbound_accept_teleportation_packet::ServerboundAcceptTeleportationPacket, serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, serverbound_keep_alive_packet::ServerboundKeepAlivePacket, - serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot, GamePacket, + serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot, + ClientboundGamePacket, }, handshake::client_intention_packet::ClientIntentionPacket, login::{ serverbound_hello_packet::ServerboundHelloPacket, serverbound_key_packet::{NonceOrSaltSignature, ServerboundKeyPacket}, - LoginPacket, }, ConnectionProtocol, PROTOCOL_VERSION, }, @@ -203,12 +203,12 @@ impl Client { } async fn handle( - packet: &GamePacket, + packet: &ClientboundGamePacket, client: &Client, tx: &UnboundedSender, ) -> Result<(), HandleError> { match packet { - GamePacket::ClientboundLoginPacket(p) => { + ClientboundGamePacket::ClientboundLoginPacket(p) => { println!("Got login packet {:?}", p); { @@ -307,40 +307,40 @@ impl Client { tx.send(Event::Login).unwrap(); } - GamePacket::ClientboundUpdateViewDistancePacket(p) => { + ClientboundGamePacket::ClientboundUpdateViewDistancePacket(p) => { println!("Got view distance packet {:?}", p); } - GamePacket::ClientboundCustomPayloadPacket(p) => { + ClientboundGamePacket::ClientboundCustomPayloadPacket(p) => { println!("Got custom payload packet {:?}", p); } - GamePacket::ClientboundChangeDifficultyPacket(p) => { + ClientboundGamePacket::ClientboundChangeDifficultyPacket(p) => { println!("Got difficulty packet {:?}", p); } - GamePacket::ClientboundDeclareCommandsPacket(_p) => { + ClientboundGamePacket::ClientboundDeclareCommandsPacket(_p) => { println!("Got declare commands packet"); } - GamePacket::ClientboundPlayerAbilitiesPacket(p) => { + ClientboundGamePacket::ClientboundPlayerAbilitiesPacket(p) => { println!("Got player abilities packet {:?}", p); } - GamePacket::ClientboundSetCarriedItemPacket(p) => { + ClientboundGamePacket::ClientboundSetCarriedItemPacket(p) => { println!("Got set carried item packet {:?}", p); } - GamePacket::ClientboundUpdateTagsPacket(_p) => { + ClientboundGamePacket::ClientboundUpdateTagsPacket(_p) => { println!("Got update tags packet"); } - GamePacket::ClientboundDisconnectPacket(p) => { + ClientboundGamePacket::ClientboundDisconnectPacket(p) => { println!("Got disconnect packet {:?}", p); } - GamePacket::ClientboundUpdateRecipesPacket(_p) => { + ClientboundGamePacket::ClientboundUpdateRecipesPacket(_p) => { println!("Got update recipes packet"); } - GamePacket::ClientboundEntityEventPacket(_p) => { + ClientboundGamePacket::ClientboundEntityEventPacket(_p) => { // println!("Got entity event packet {:?}", p); } - GamePacket::ClientboundRecipePacket(_p) => { + ClientboundGamePacket::ClientboundRecipePacket(_p) => { println!("Got recipe packet"); } - GamePacket::ClientboundPlayerPositionPacket(p) => { + ClientboundGamePacket::ClientboundPlayerPositionPacket(p) => { // TODO: reply with teleport confirm println!("Got player position packet {:?}", p); @@ -432,10 +432,10 @@ impl Client { ) .await; } - GamePacket::ClientboundPlayerInfoPacket(p) => { + ClientboundGamePacket::ClientboundPlayerInfoPacket(p) => { println!("Got player info packet {:?}", p); } - GamePacket::ClientboundSetChunkCacheCenterPacket(p) => { + ClientboundGamePacket::ClientboundSetChunkCacheCenterPacket(p) => { println!("Got chunk cache center packet {:?}", p); client .dimension @@ -444,7 +444,7 @@ impl Client { .unwrap() .update_view_center(&ChunkPos::new(p.x, p.z)); } - GamePacket::ClientboundLevelChunkWithLightPacket(p) => { + ClientboundGamePacket::ClientboundLevelChunkWithLightPacket(p) => { println!("Got chunk with light packet {} {}", p.x, p.z); let pos = ChunkPos::new(p.x, p.z); // let chunk = Chunk::read_with_world_height(&mut p.chunk_data); @@ -457,10 +457,10 @@ impl Client { .replace_with_packet_data(&pos, &mut p.chunk_data.data.as_slice()) .unwrap(); } - GamePacket::ClientboundLightUpdatePacket(p) => { + ClientboundGamePacket::ClientboundLightUpdatePacket(p) => { println!("Got light update packet {:?}", p); } - GamePacket::ClientboundAddEntityPacket(p) => { + ClientboundGamePacket::ClientboundAddEntityPacket(p) => { println!("Got add entity packet {:?}", p); let entity = Entity::from(p); client @@ -470,19 +470,19 @@ impl Client { .expect("Dimension doesn't exist! We should've gotten a login packet by now.") .add_entity(entity); } - GamePacket::ClientboundSetEntityDataPacket(_p) => { + ClientboundGamePacket::ClientboundSetEntityDataPacket(_p) => { // println!("Got set entity data packet {:?}", p); } - GamePacket::ClientboundUpdateAttributesPacket(_p) => { + ClientboundGamePacket::ClientboundUpdateAttributesPacket(_p) => { // println!("Got update attributes packet {:?}", p); } - GamePacket::ClientboundEntityVelocityPacket(_p) => { + ClientboundGamePacket::ClientboundEntityVelocityPacket(_p) => { // println!("Got entity velocity packet {:?}", p); } - GamePacket::ClientboundSetEntityLinkPacket(p) => { + ClientboundGamePacket::ClientboundSetEntityLinkPacket(p) => { println!("Got set entity link packet {:?}", p); } - GamePacket::ClientboundAddPlayerPacket(p) => { + ClientboundGamePacket::ClientboundAddPlayerPacket(p) => { println!("Got add player packet {:?}", p); let entity = Entity::from(p); client @@ -492,25 +492,25 @@ impl Client { .expect("Dimension doesn't exist! We should've gotten a login packet by now.") .add_entity(entity); } - GamePacket::ClientboundInitializeBorderPacket(p) => { + ClientboundGamePacket::ClientboundInitializeBorderPacket(p) => { println!("Got initialize border packet {:?}", p); } - GamePacket::ClientboundSetTimePacket(p) => { + ClientboundGamePacket::ClientboundSetTimePacket(p) => { println!("Got set time packet {:?}", p); } - GamePacket::ClientboundSetDefaultSpawnPositionPacket(p) => { + ClientboundGamePacket::ClientboundSetDefaultSpawnPositionPacket(p) => { println!("Got set default spawn position packet {:?}", p); } - GamePacket::ClientboundContainerSetContentPacket(p) => { + ClientboundGamePacket::ClientboundContainerSetContentPacket(p) => { println!("Got container set content packet {:?}", p); } - GamePacket::ClientboundSetHealthPacket(p) => { + ClientboundGamePacket::ClientboundSetHealthPacket(p) => { println!("Got set health packet {:?}", p); } - GamePacket::ClientboundSetExperiencePacket(p) => { + ClientboundGamePacket::ClientboundSetExperiencePacket(p) => { println!("Got set experience packet {:?}", p); } - GamePacket::ClientboundTeleportEntityPacket(p) => { + ClientboundGamePacket::ClientboundTeleportEntityPacket(p) => { let mut dimension_lock = client.dimension.lock()?; let dimension = dimension_lock.as_mut().unwrap(); @@ -523,28 +523,28 @@ impl Client { }, )?; } - GamePacket::ClientboundUpdateAdvancementsPacket(p) => { + ClientboundGamePacket::ClientboundUpdateAdvancementsPacket(p) => { println!("Got update advancements packet {:?}", p); } - GamePacket::ClientboundRotateHeadPacket(_p) => { + ClientboundGamePacket::ClientboundRotateHeadPacket(_p) => { // println!("Got rotate head packet {:?}", p); } - GamePacket::ClientboundMoveEntityPosPacket(p) => { + ClientboundGamePacket::ClientboundMoveEntityPosPacket(p) => { let mut dimension_lock = client.dimension.lock()?; let dimension = dimension_lock.as_mut().unwrap(); dimension.move_entity_with_delta(p.entity_id, &p.delta)?; } - GamePacket::ClientboundMoveEntityPosrotPacket(p) => { + ClientboundGamePacket::ClientboundMoveEntityPosrotPacket(p) => { let mut dimension_lock = client.dimension.lock()?; let dimension = dimension_lock.as_mut().unwrap(); dimension.move_entity_with_delta(p.entity_id, &p.delta)?; } - GamePacket::ClientboundMoveEntityRotPacket(p) => { + ClientboundGamePacket::ClientboundMoveEntityRotPacket(p) => { println!("Got move entity rot packet {:?}", p); } - GamePacket::ClientboundKeepAlivePacket(p) => { + ClientboundGamePacket::ClientboundKeepAlivePacket(p) => { println!("Got keep alive packet {:?}", p); client .conn @@ -553,48 +553,48 @@ impl Client { .write(ServerboundKeepAlivePacket { id: p.id }.get()) .await; } - GamePacket::ClientboundRemoveEntitiesPacket(p) => { + ClientboundGamePacket::ClientboundRemoveEntitiesPacket(p) => { println!("Got remove entities packet {:?}", p); } - GamePacket::ClientboundPlayerChatPacket(p) => { + ClientboundGamePacket::ClientboundPlayerChatPacket(p) => { println!("Got player chat packet {:?}", p); tx.send(Event::Chat(ChatPacket::Player(Box::new(p.clone())))) .unwrap(); } - GamePacket::ClientboundSystemChatPacket(p) => { + ClientboundGamePacket::ClientboundSystemChatPacket(p) => { println!("Got system chat packet {:?}", p); tx.send(Event::Chat(ChatPacket::System(p.clone()))).unwrap(); } - GamePacket::ClientboundSoundPacket(p) => { + ClientboundGamePacket::ClientboundSoundPacket(p) => { println!("Got sound packet {:?}", p); } - GamePacket::ClientboundLevelEventPacket(p) => { + ClientboundGamePacket::ClientboundLevelEventPacket(p) => { println!("Got level event packet {:?}", p); } - GamePacket::ClientboundBlockUpdatePacket(p) => { + ClientboundGamePacket::ClientboundBlockUpdatePacket(p) => { println!("Got block update packet {:?}", p); // TODO: update world } - GamePacket::ClientboundAnimatePacket(p) => { + ClientboundGamePacket::ClientboundAnimatePacket(p) => { println!("Got animate packet {:?}", p); } - GamePacket::ClientboundSectionBlocksUpdatePacket(p) => { + ClientboundGamePacket::ClientboundSectionBlocksUpdatePacket(p) => { println!("Got section blocks update packet {:?}", p); // TODO: update world } - GamePacket::ClientboundGameEventPacket(p) => { + ClientboundGamePacket::ClientboundGameEventPacket(p) => { println!("Got game event packet {:?}", p); } - GamePacket::ClientboundLevelParticlesPacket(p) => { + ClientboundGamePacket::ClientboundLevelParticlesPacket(p) => { println!("Got level particles packet {:?}", p); } - GamePacket::ClientboundServerDataPacket(p) => { + ClientboundGamePacket::ClientboundServerDataPacket(p) => { println!("Got server data packet {:?}", p); } - GamePacket::ClientboundSetEquipmentPacket(p) => { + ClientboundGamePacket::ClientboundSetEquipmentPacket(p) => { println!("Got set equipment packet {:?}", p); } - GamePacket::ClientboundUpdateMobEffectPacket(p) => { + ClientboundGamePacket::ClientboundUpdateMobEffectPacket(p) => { println!("Got update mob effect packet {:?}", p); } _ => panic!("Unexpected packet {:?}", packet), -- cgit v1.2.3 From f22a386c78ed6cae638d35888a5376cfa48c1a90 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 15:38:33 -0500 Subject: fix errors in az-client --- azalea-client/src/client.rs | 12 ++++++------ azalea-client/src/ping.rs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'azalea-client') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 8b456276..f5dfdf98 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -18,6 +18,7 @@ use azalea_protocol::{ login::{ serverbound_hello_packet::ServerboundHelloPacket, serverbound_key_packet::{NonceOrSaltSignature, ServerboundKeyPacket}, + ClientboundLoginPacket, }, ConnectionProtocol, PROTOCOL_VERSION, }, @@ -110,7 +111,7 @@ impl Client { let packet_result = conn.read().await; match packet_result { Ok(packet) => match packet { - LoginPacket::ClientboundHelloPacket(p) => { + ClientboundLoginPacket::ClientboundHelloPacket(p) => { println!("Got encryption request"); let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap(); @@ -128,21 +129,20 @@ impl Client { .await; conn.set_encryption_key(e.secret_key); } - LoginPacket::ClientboundLoginCompressionPacket(p) => { + ClientboundLoginPacket::ClientboundLoginCompressionPacket(p) => { println!("Got compression request {:?}", p.compression_threshold); conn.set_compression_threshold(p.compression_threshold); } - LoginPacket::ClientboundGameProfilePacket(p) => { + ClientboundLoginPacket::ClientboundGameProfilePacket(p) => { println!("Got profile {:?}", p.game_profile); break (conn.game(), p.game_profile); } - LoginPacket::ClientboundLoginDisconnectPacket(p) => { + ClientboundLoginPacket::ClientboundLoginDisconnectPacket(p) => { println!("Got disconnect {:?}", p); } - LoginPacket::ClientboundCustomQueryPacket(p) => { + ClientboundLoginPacket::ClientboundCustomQueryPacket(p) => { println!("Got custom query {:?}", p); } - _ => panic!("Unexpected packet {:?}", packet), }, Err(e) => { panic!("Error: {:?}", e); diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs index 8ecff7ca..c4ef2915 100755 --- a/azalea-client/src/ping.rs +++ b/azalea-client/src/ping.rs @@ -5,7 +5,8 @@ use azalea_protocol::{ handshake::client_intention_packet::ClientIntentionPacket, status::{ clientbound_status_response_packet::ClientboundStatusResponsePacket, - serverbound_status_request_packet::ServerboundStatusRequestPacket, StatusPacket, + serverbound_status_request_packet::ServerboundStatusRequestPacket, + ClientboundStatusPacket, }, ConnectionProtocol, PROTOCOL_VERSION, }, @@ -38,7 +39,6 @@ pub async fn ping_server( let packet = conn.read().await.unwrap(); match packet { - StatusPacket::ClientboundStatusResponsePacket(p) => Ok(p), - _ => Err("Invalid packet type".to_string()), + ClientboundStatusPacket::ClientboundStatusResponsePacket(p) => Ok(p), } } -- cgit v1.2.3 From 0c2ce00bae1d0a4711ad45f5558286dccb04b0ae Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 16:29:06 -0500 Subject: make Connection a single struct with generics This isn't that good of a solution but I couldn't come up with a better one and this was pretty simple to implement --- azalea-client/src/client.rs | 8 +-- azalea-client/src/ping.rs | 4 +- azalea-protocol/src/connect.rs | 150 +++++++++++++++++------------------------ 3 files changed, 69 insertions(+), 93 deletions(-) (limited to 'azalea-client') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index f5dfdf98..28154bce 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -3,7 +3,7 @@ use azalea_auth::game_profile::GameProfile; use azalea_core::{ChunkPos, EntityPos, PositionDelta, PositionDeltaTrait, ResourceLocation}; use azalea_entity::Entity; use azalea_protocol::{ - connect::{GameConnection, HandshakeConnection}, + connect::Connection, packets::{ game::{ clientbound_player_chat_packet::ClientboundPlayerChatPacket, @@ -12,7 +12,7 @@ use azalea_protocol::{ serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, serverbound_keep_alive_packet::ServerboundKeepAlivePacket, serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot, - ClientboundGamePacket, + ClientboundGamePacket, ServerboundGamePacket, }, handshake::client_intention_packet::ClientIntentionPacket, login::{ @@ -61,7 +61,7 @@ pub enum ChatPacket { #[derive(Clone)] pub struct Client { game_profile: GameProfile, - pub conn: Arc>, + pub conn: Arc>>, pub player: Arc>, pub dimension: Arc>>, // game_loop @@ -81,7 +81,7 @@ impl Client { ) -> Result<(Self, UnboundedReceiver), String> { let resolved_address = resolver::resolve_address(address).await?; - let mut conn = HandshakeConnection::new(&resolved_address).await?; + let mut conn = Connection::new(&resolved_address).await?; // handshake conn.write( diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs index c4ef2915..e4acfc34 100755 --- a/azalea-client/src/ping.rs +++ b/azalea-client/src/ping.rs @@ -1,6 +1,6 @@ ///! Ping Minecraft servers. use azalea_protocol::{ - connect::HandshakeConnection, + connect::Connection, packets::{ handshake::client_intention_packet::ClientIntentionPacket, status::{ @@ -18,7 +18,7 @@ pub async fn ping_server( ) -> Result { let resolved_address = resolver::resolve_address(address).await?; - let mut conn = HandshakeConnection::new(&resolved_address).await?; + let mut conn = Connection::new(&resolved_address).await?; // send the client intention packet and switch to the status state conn.write( diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index bf730fc4..a438ccfb 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -4,40 +4,58 @@ use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket}; use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket}; use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket}; use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket}; +use crate::packets::ProtocolPacket; use crate::read::read_packet; use crate::write::write_packet; use crate::ServerIpAddress; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; +use std::fmt::Debug; +use std::marker::PhantomData; use tokio::net::TcpStream; -pub struct HandshakeConnection { - /// The buffered writer - pub stream: TcpStream, -} +pub struct Handshake; +pub struct Game; +pub struct Status; +pub struct Login; -pub struct GameConnection { +pub struct Connection { /// The buffered writer pub stream: TcpStream, pub compression_threshold: Option, pub enc_cipher: Option, pub dec_cipher: Option, + _reading: PhantomData, + _writing: PhantomData, } -pub struct StatusConnection { - /// The buffered writer - pub stream: TcpStream, -} +impl Connection +where + R: ProtocolPacket + Debug, + W: ProtocolPacket + Debug, +{ + pub async fn read(&mut self) -> Result { + read_packet::( + &mut self.stream, + self.compression_threshold, + &mut self.dec_cipher, + ) + .await + } -pub struct LoginConnection { - /// The buffered writer - pub stream: TcpStream, - pub compression_threshold: Option, - pub enc_cipher: Option, - pub dec_cipher: Option, + /// Write a packet to the server + pub async fn write(&mut self, packet: W) { + write_packet( + packet, + &mut self.stream, + self.compression_threshold, + &mut self.enc_cipher, + ) + .await; + } } -impl HandshakeConnection { - pub async fn new(address: &ServerIpAddress) -> Result { +impl Connection { + pub async fn new(address: &ServerIpAddress) -> Result { let ip = address.ip; let port = address.port; @@ -50,88 +68,44 @@ impl HandshakeConnection { .set_nodelay(true) .expect("Error enabling tcp_nodelay"); - Ok(HandshakeConnection { stream }) - } - - pub fn login(self) -> LoginConnection { - LoginConnection { - stream: self.stream, + Ok(Connection { + stream, compression_threshold: None, enc_cipher: None, dec_cipher: None, - } + _reading: PhantomData, + _writing: PhantomData, + }) } - pub fn status(self) -> StatusConnection { - StatusConnection { + pub fn login(self) -> Connection { + Connection { stream: self.stream, + compression_threshold: self.compression_threshold, + enc_cipher: self.enc_cipher, + dec_cipher: self.dec_cipher, + _reading: PhantomData, + _writing: PhantomData, } } - pub async fn read(&mut self) -> Result { - read_packet::(&mut self.stream, None, &mut None).await - } - - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundHandshakePacket) { - write_packet(packet, &mut self.stream, None, &mut None).await; - } -} - -impl GameConnection { - pub async fn read(&mut self) -> Result { - read_packet::( - &mut self.stream, - self.compression_threshold, - &mut self.dec_cipher, - ) - .await - } - - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundGamePacket) { - write_packet( - packet, - &mut self.stream, - self.compression_threshold, - &mut self.enc_cipher, - ) - .await; - } -} - -impl StatusConnection { - pub async fn read(&mut self) -> Result { - read_packet::(&mut self.stream, None, &mut None).await - } - - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundStatusPacket) { - write_packet(packet, &mut self.stream, None, &mut None).await; + pub fn status(self) -> Connection { + Connection { + stream: self.stream, + compression_threshold: self.compression_threshold, + enc_cipher: self.enc_cipher, + dec_cipher: self.dec_cipher, + _reading: PhantomData, + _writing: PhantomData, + } } } -impl LoginConnection { - pub async fn read(&mut self) -> Result { - read_packet::( - &mut self.stream, - self.compression_threshold, - &mut self.dec_cipher, - ) - .await - } +impl Connection {} - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundLoginPacket) { - write_packet( - packet, - &mut self.stream, - self.compression_threshold, - &mut self.enc_cipher, - ) - .await; - } +impl Connection {} +impl Connection { pub fn set_compression_threshold(&mut self, threshold: i32) { // if you pass a threshold of 0 or less, compression is disabled if threshold > 0 { @@ -148,12 +122,14 @@ impl LoginConnection { self.dec_cipher = Some(dec_cipher); } - pub fn game(self) -> GameConnection { - GameConnection { + pub fn game(self) -> Connection { + Connection { stream: self.stream, compression_threshold: self.compression_threshold, enc_cipher: self.enc_cipher, dec_cipher: self.dec_cipher, + _reading: PhantomData, + _writing: PhantomData, } } } -- cgit v1.2.3