From 8e3ba097b48543a85f2cf487d5db90add3f28bac Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 18 Dec 2021 10:04:10 -0600 Subject: [PATCH] start adding clientbound_login_packet --- Cargo.lock | 1 + azalea-client/src/connect.rs | 18 +++++- azalea-core/Cargo.toml | 1 + azalea-core/src/game_type.rs | 60 +++++++++++++++++++ azalea-core/src/lib.rs | 1 + ...nPacket.rs => clientbound_login_packet.rs} | 18 +++--- azalea-protocol/src/packets/game/mod.rs | 12 ++-- .../login/clientbound_custom_query_packet.rs | 1 - azalea-protocol/src/packets/login/mod.rs | 7 +-- 9 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 azalea-core/src/game_type.rs rename azalea-protocol/src/packets/game/{ClientboundLoginPacket.rs => clientbound_login_packet.rs} (76%) diff --git a/Cargo.lock b/Cargo.lock index c91c963f..c4ecf771 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,7 @@ dependencies = [ name = "azalea-core" version = "0.1.0" dependencies = [ + "azalea-chat", "uuid", ] diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index 07c2e0d0..94c800f6 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -56,7 +56,23 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { }; // game - panic!("ok i haven't implemented game yet"); + loop { + let packet_result = conn.read().await; + match packet_result { + Ok(packet) => match packet { + GamePacket::ClientboundKeepAlivePacket(p) => { + println!("Got keep alive packet {:?}", p.keep_alive_id); + } + GamePacket::ClientboundChatMessagePacket(p) => { + println!("Got chat message packet {:?}", p.message); + } + _ => panic!("unhandled packet"), + }, + Err(e) => { + println!("Error: {:?}", e); + } + } + } Ok(()) } diff --git a/azalea-core/Cargo.toml b/azalea-core/Cargo.toml index 1aa59fe9..b0139999 100644 --- a/azalea-core/Cargo.toml +++ b/azalea-core/Cargo.toml @@ -6,4 +6,5 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +azalea-chat = {path = "../azalea-chat"} uuid = "^0.8.2" diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs new file mode 100644 index 00000000..b0ca6a2d --- /dev/null +++ b/azalea-core/src/game_type.rs @@ -0,0 +1,60 @@ +use azalea_chat; + +#[derive(Hash, Clone, Debug)] +pub enum GameType { + SURVIVAL, + CREATIVE, + ADVENTURE, + SPECTATOR, +} + +impl GameType { + fn to_id(&self) -> u8 { + match self { + GameType::SURVIVAL => 0, + GameType::CREATIVE => 1, + GameType::ADVENTURE => 2, + GameType::SPECTATOR => 3, + } + } + + fn from_id(id: u8) -> GameType { + match id { + 0 => GameType::SURVIVAL, + 1 => GameType::CREATIVE, + 2 => GameType::ADVENTURE, + 3 => GameType::SPECTATOR, + _ => panic!("Unknown game type id: {}", id), + } + } + + fn short_name(&self) -> &'static str { + // TODO: these should be translated TranslatableComponent("selectWorld.gameMode." + string2) + match self { + GameType::SURVIVAL => "Survival", + GameType::CREATIVE => "Creative", + GameType::ADVENTURE => "Adventure", + GameType::SPECTATOR => "Spectator", + } + } + + fn long_name(&self) -> &'static str { + // TODO: These should be translated TranslatableComponent("gameMode." + string2); + match self { + GameType::SURVIVAL => "Survival Mode", + GameType::CREATIVE => "Creative Mode", + GameType::ADVENTURE => "Adventure Mode", + GameType::SPECTATOR => "Spectator Mode", + } + } + + fn from_name(name: &str) -> GameType { + match name { + "survival" => GameType::SURVIVAL, + "creative" => GameType::CREATIVE, + "adventure" => GameType::ADVENTURE, + "spectator" => GameType::SPECTATOR, + _ => panic!("Unknown game type name: {}", name), + } + } +} diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index dbe08afb..887d1686 100644 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -1,5 +1,6 @@ //! Random miscellaneous things like UUIDs that don't deserve their own crate. +pub mod game_type; pub mod resource_location; pub mod serializable_uuid; diff --git a/azalea-protocol/src/packets/game/ClientboundLoginPacket.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs similarity index 76% rename from azalea-protocol/src/packets/game/ClientboundLoginPacket.rs rename to azalea-protocol/src/packets/game/clientbound_login_packet.rs index ec869faa..fc701d9d 100644 --- a/azalea-protocol/src/packets/game/ClientboundLoginPacket.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -1,12 +1,11 @@ use super::GamePacket; use crate::mc_buf::{Readable, Writable}; -use azalea_core::resource_location::ResourceLocation; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; use std::hash::Hash; -use tokio::io::BufReader; #[derive(Hash, Clone, Debug)] pub struct ClientboundLoginPacket { - // private final int playerId; + // private final int playerId; // private final boolean hardcore; // private final GameType gameType; // @Nullable @@ -23,7 +22,12 @@ pub struct ClientboundLoginPacket { // private final boolean showDeathScreen; // private final boolean isDebug; // private final boolean isFlat; - + pub player_id: i32, + pub hardcore: bool, + pub game_type: GameType, + pub previous_game_type: Option, + pub levels: Vec, + pub registry_holder: azalea_core::registry::RegistryAccess, } impl ClientboundLoginPacket { @@ -32,9 +36,9 @@ impl ClientboundLoginPacket { } pub fn write(&self, buf: &mut Vec) { - buf.write_varint(self.transaction_id as i32).unwrap(); - buf.write_utf(self.identifier.to_string().as_str()).unwrap(); - buf.write_bytes(&self.data).unwrap(); + buf.write_int(self.player_id); + buf.write_bool(self.hardcore); + // buf.write_byte(self.game_type. } pub async fn read( diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 0391a6b1..73f66c33 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,9 +1,9 @@ -use async_trait::async_trait; -use tokio::io::BufReader; - -use crate::connect::PacketFlow; +pub mod clientbound_login_packet; use super::ProtocolPacket; +use crate::connect::PacketFlow; +use async_trait::async_trait; +use tokio::io::BufReader; #[derive(Clone, Debug)] pub enum GamePacket @@ -13,7 +13,9 @@ where #[async_trait] impl ProtocolPacket for GamePacket { fn id(&self) -> u32 { - 0x00 + match self { + GamePacket::ClientboundLoginPacket(_packet) => 0x00, + } } fn write(&self, _buf: &mut Vec) {} diff --git a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs index ed9820ef..2bc1fc1e 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -2,7 +2,6 @@ use super::LoginPacket; use crate::mc_buf::{Readable, Writable}; use azalea_core::resource_location::ResourceLocation; use std::hash::Hash; -use tokio::io::BufReader; #[derive(Hash, Clone, Debug)] pub struct ClientboundCustomQueryPacket { diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index 377a285a..65d94bed 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -4,12 +4,9 @@ pub mod clientbound_hello_packet; pub mod clientbound_login_compression_packet; pub mod serverbound_hello_packet; -use async_trait::async_trait; -use tokio::io::BufReader; - -use crate::connect::PacketFlow; - use super::ProtocolPacket; +use crate::connect::PacketFlow; +use async_trait::async_trait; #[derive(Clone, Debug)] pub enum LoginPacket