From 8e3ba097b48543a85f2cf487d5db90add3f28bac Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 18 Dec 2021 10:04:10 -0600 Subject: 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 + .../src/packets/game/ClientboundLoginPacket.rs | 53 ------------------- .../src/packets/game/clientbound_login_packet.rs | 57 ++++++++++++++++++++ azalea-protocol/src/packets/game/mod.rs | 12 +++-- .../login/clientbound_custom_query_packet.rs | 1 - azalea-protocol/src/packets/login/mod.rs | 7 +-- 10 files changed, 146 insertions(+), 65 deletions(-) create mode 100644 azalea-core/src/game_type.rs delete mode 100644 azalea-protocol/src/packets/game/ClientboundLoginPacket.rs create mode 100644 azalea-protocol/src/packets/game/clientbound_login_packet.rs 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/ClientboundLoginPacket.rs deleted file mode 100644 index ec869faa..00000000 --- a/azalea-protocol/src/packets/game/ClientboundLoginPacket.rs +++ /dev/null @@ -1,53 +0,0 @@ -use super::GamePacket; -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 ClientboundLoginPacket { - // private final int playerId; - // private final boolean hardcore; - // private final GameType gameType; - // @Nullable - // private final GameType previousGameType; - // private final Set> levels; - // private final RegistryAccess.RegistryHolder registryHolder; - // private final DimensionType dimensionType; - // private final ResourceKey dimension; - // private final long seed; - // private final int maxPlayers; - // private final int chunkRadius; - // private final int simulationDistance; - // private final boolean reducedDebugInfo; - // private final boolean showDeathScreen; - // private final boolean isDebug; - // private final boolean isFlat; - -} - -impl ClientboundLoginPacket { - pub fn get(self) -> GamePacket { - GamePacket::ClientboundLoginPacket(self) - } - - 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(); - } - - pub async fn read( - buf: &mut T, - ) -> Result { - let transaction_id = buf.read_varint().await? as u32; - let identifier = ResourceLocation::new(&buf.read_utf().await?)?; - let data = buf.read_bytes(1048576).await?; - Ok(ClientboundLoginPacket { - transaction_id, - identifier, - data, - } - .get()) - } -} diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs new file mode 100644 index 00000000..fc701d9d --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -0,0 +1,57 @@ +use super::GamePacket; +use crate::mc_buf::{Readable, Writable}; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use std::hash::Hash; + +#[derive(Hash, Clone, Debug)] +pub struct ClientboundLoginPacket { + // private final int playerId; + // private final boolean hardcore; + // private final GameType gameType; + // @Nullable + // private final GameType previousGameType; + // private final Set> levels; + // private final RegistryAccess.RegistryHolder registryHolder; + // private final DimensionType dimensionType; + // private final ResourceKey dimension; + // private final long seed; + // private final int maxPlayers; + // private final int chunkRadius; + // private final int simulationDistance; + // private final boolean reducedDebugInfo; + // 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 { + pub fn get(self) -> GamePacket { + GamePacket::ClientboundLoginPacket(self) + } + + pub fn write(&self, buf: &mut Vec) { + buf.write_int(self.player_id); + buf.write_bool(self.hardcore); + // buf.write_byte(self.game_type. + } + + pub async fn read( + buf: &mut T, + ) -> Result { + let transaction_id = buf.read_varint().await? as u32; + let identifier = ResourceLocation::new(&buf.read_utf().await?)?; + let data = buf.read_bytes(1048576).await?; + Ok(ClientboundLoginPacket { + transaction_id, + identifier, + data, + } + .get()) + } +} 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 -- cgit v1.2.3