From f993e79a7e6acc7aadd1e6cf9462d7a3e2c3ac3e Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 17 Jun 2022 20:46:16 -0500 Subject: Create azalea-entity --- azalea-client/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'azalea-client/src/lib.rs') diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index 5814687a..db935897 100755 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -1,12 +1,10 @@ //! Significantly abstract azalea-protocol so it's actually useable for bots. mod connect; -mod entity; pub mod ping; mod player; pub use connect::{Account, Client, Event}; -pub use entity::Entity; pub use player::Player; #[cfg(test)] -- cgit v1.2.3 From fc3151f89db1cf018bfebebb8f102e20911e64d3 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 18 Jun 2022 16:54:49 -0500 Subject: account.rs and client.rs --- azalea-client/src/account.rs | 27 +++ azalea-client/src/client.rs | 465 +++++++++++++++++++++++++++++++++++++++++ azalea-client/src/connect.rs | 483 ------------------------------------------- azalea-client/src/lib.rs | 6 +- azalea-world/src/entity.rs | 1 + bot/src/main.rs | 5 +- 6 files changed, 499 insertions(+), 488 deletions(-) create mode 100644 azalea-client/src/account.rs create mode 100644 azalea-client/src/client.rs delete mode 100755 azalea-client/src/connect.rs (limited to 'azalea-client/src/lib.rs') diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs new file mode 100644 index 00000000..00a5d0f6 --- /dev/null +++ b/azalea-client/src/account.rs @@ -0,0 +1,27 @@ +use crate::Client; +use azalea_protocol::{ + packets::game::{ + clientbound_player_chat_packet::ClientboundPlayerChatPacket, + clientbound_system_chat_packet::ClientboundSystemChatPacket, + }, + ServerAddress, +}; +use std::fmt::Debug; + +///! Connect to Minecraft servers. + +/// Something that can join Minecraft servers. +pub struct Account { + pub username: String, +} +impl Account { + pub fn offline(username: &str) -> Self { + Self { + username: username.to_string(), + } + } + + pub async fn join(&self, address: &ServerAddress) -> Result { + Client::join(self, address).await + } +} diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs new file mode 100644 index 00000000..ff8729cb --- /dev/null +++ b/azalea-client/src/client.rs @@ -0,0 +1,465 @@ +use crate::{Account, Player}; +use azalea_core::{resource_location::ResourceLocation, ChunkPos}; +use azalea_entity::Entity; +use azalea_protocol::{ + connect::{GameConnection, HandshakeConnection}, + packets::{ + game::{ + clientbound_player_chat_packet::ClientboundPlayerChatPacket, + clientbound_system_chat_packet::ClientboundSystemChatPacket, + serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, + serverbound_keep_alive_packet::ServerboundKeepAlivePacket, GamePacket, + }, + handshake::client_intention_packet::ClientIntentionPacket, + login::{ + serverbound_hello_packet::ServerboundHelloPacket, + serverbound_key_packet::{NonceOrSaltSignature, ServerboundKeyPacket}, + LoginPacket, + }, + ConnectionProtocol, PROTOCOL_VERSION, + }, + resolver, ServerAddress, +}; +use azalea_world::{ChunkStorage, EntityStorage, World}; +use std::{fmt::Debug, sync::Arc}; +use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; +use tokio::sync::Mutex; + +#[derive(Default)] +pub struct ClientState { + pub player: Player, + pub world: Option, +} + +#[derive(Debug, Clone)] +pub enum Event { + Login, + Chat(ChatPacket), +} + +#[derive(Debug, Clone)] +pub enum ChatPacket { + System(ClientboundSystemChatPacket), + Player(ClientboundPlayerChatPacket), +} + +// impl ChatPacket { +// pub fn message(&self) -> &str { +// match self { +// ChatPacket::System(p) => &p.content, +// ChatPacket::Player(p) => &p.message, +// } +// } +// } + +/// A player that you can control that is currently in a Minecraft server. +pub struct Client { + event_receiver: UnboundedReceiver, + pub conn: Arc>, + pub state: Arc>, + // game_loop +} + +/// Whether we should ignore errors when decoding packets. +const IGNORE_ERRORS: bool = !cfg!(debug_assertions); + +impl Client { + /// Connect to a Minecraft server with an account. + pub async fn join(account: &Account, address: &ServerAddress) -> Result { + let resolved_address = resolver::resolve_address(address).await?; + + let mut conn = HandshakeConnection::new(&resolved_address).await?; + + // handshake + conn.write( + ClientIntentionPacket { + protocol_version: PROTOCOL_VERSION, + hostname: address.host.clone(), + port: address.port, + intention: ConnectionProtocol::Login, + } + .get(), + ) + .await; + let mut conn = conn.login(); + + // login + conn.write( + ServerboundHelloPacket { + username: account.username.clone(), + public_key: None, + } + .get(), + ) + .await; + + let conn = loop { + let packet_result = conn.read().await; + match packet_result { + Ok(packet) => match packet { + LoginPacket::ClientboundHelloPacket(p) => { + println!("Got encryption request"); + let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap(); + + // TODO: authenticate with the server here (authenticateServer) + + conn.write( + ServerboundKeyPacket { + nonce_or_salt_signature: NonceOrSaltSignature::Nonce( + e.encrypted_nonce, + ), + key_bytes: e.encrypted_public_key, + } + .get(), + ) + .await; + conn.set_encryption_key(e.secret_key); + } + LoginPacket::ClientboundLoginCompressionPacket(p) => { + println!("Got compression request {:?}", p.compression_threshold); + conn.set_compression_threshold(p.compression_threshold); + } + LoginPacket::ClientboundGameProfilePacket(p) => { + println!("Got profile {:?}", p.game_profile); + break conn.game(); + } + LoginPacket::ClientboundLoginDisconnectPacket(p) => { + println!("Got disconnect {:?}", p); + } + LoginPacket::ClientboundCustomQueryPacket(p) => { + println!("Got custom query {:?}", p); + } + _ => panic!("Unexpected packet {:?}", packet), + }, + Err(e) => { + panic!("Error: {:?}", e); + } + } + }; + + let conn = Arc::new(Mutex::new(conn)); + + let (tx, rx) = mpsc::unbounded_channel(); + + // we got the GameConnection, so the server is now connected :) + let client = Client { + event_receiver: rx, + conn: conn.clone(), + state: Arc::new(Mutex::new(ClientState::default())), + }; + // let client = Arc::new(Mutex::new(client)); + // let weak_client = Arc::<_>::downgrade(&client); + + // just start up the game loop and we're ready! + // tokio::spawn(Self::game_loop(conn, tx, handler, state)) + + let game_loop_state = client.state.clone(); + + tokio::spawn(Self::game_loop(conn, tx, game_loop_state)); + + Ok(client) + } + + async fn game_loop( + conn: Arc>, + tx: UnboundedSender, + state: Arc>, + ) { + loop { + let r = conn.lock().await.read().await; + match r { + Ok(packet) => Self::handle(&packet, &tx, &state, &conn).await, + Err(e) => { + if IGNORE_ERRORS { + println!("Error: {:?}", e); + if e == "length wider than 21-bit" { + panic!(); + } + } else { + panic!("Error: {:?}", e); + } + } + }; + } + } + + async fn handle( + packet: &GamePacket, + tx: &UnboundedSender, + state: &Arc>, + conn: &Arc>, + ) { + match packet { + GamePacket::ClientboundLoginPacket(p) => { + println!("Got login packet {:?}", p); + + let mut state = state.lock().await; + + // // write p into login.txt + // std::io::Write::write_all( + // &mut std::fs::File::create("login.txt").unwrap(), + // format!("{:#?}", p).as_bytes(), + // ) + // .unwrap(); + + state.player.entity.id = p.player_id; + + // TODO: have registry_holder be a struct because this sucks rn + // best way would be to add serde support to azalea-nbt + + let registry_holder = p + .registry_holder + .as_compound() + .expect("Registry holder is not a compound") + .get("") + .expect("No \"\" tag") + .as_compound() + .expect("\"\" tag is not a compound"); + let dimension_types = registry_holder + .get("minecraft:dimension_type") + .expect("No dimension_type tag") + .as_compound() + .expect("dimension_type is not a compound") + .get("value") + .expect("No dimension_type value") + .as_list() + .expect("dimension_type value is not a list"); + let dimension_type = dimension_types + .iter() + .find(|t| { + t.as_compound() + .expect("dimension_type value is not a compound") + .get("name") + .expect("No name tag") + .as_string() + .expect("name is not a string") + == p.dimension_type.to_string() + }) + .expect(&format!("No dimension_type with name {}", p.dimension_type)) + .as_compound() + .unwrap() + .get("element") + .expect("No element tag") + .as_compound() + .expect("element is not a compound"); + let height = (*dimension_type + .get("height") + .expect("No height tag") + .as_int() + .expect("height tag is not an int")) + .try_into() + .expect("height is not a u32"); + let min_y = (*dimension_type + .get("min_y") + .expect("No min_y tag") + .as_int() + .expect("min_y tag is not an int")) + .try_into() + .expect("min_y is not an i32"); + + state.world = Some(World { + height, + min_y, + storage: ChunkStorage::new(16), + entities: EntityStorage::new(), + }); + + conn.lock() + .await + .write( + ServerboundCustomPayloadPacket { + identifier: ResourceLocation::new("brand").unwrap(), + // they don't have to know :) + data: "vanilla".into(), + } + .get(), + ) + .await; + + tx.send(Event::Login).unwrap(); + } + GamePacket::ClientboundUpdateViewDistancePacket(p) => { + println!("Got view distance packet {:?}", p); + } + GamePacket::ClientboundCustomPayloadPacket(p) => { + println!("Got custom payload packet {:?}", p); + } + GamePacket::ClientboundChangeDifficultyPacket(p) => { + println!("Got difficulty packet {:?}", p); + } + GamePacket::ClientboundDeclareCommandsPacket(_p) => { + println!("Got declare commands packet"); + } + GamePacket::ClientboundPlayerAbilitiesPacket(p) => { + println!("Got player abilities packet {:?}", p); + } + GamePacket::ClientboundSetCarriedItemPacket(p) => { + println!("Got set carried item packet {:?}", p); + } + GamePacket::ClientboundUpdateTagsPacket(_p) => { + println!("Got update tags packet"); + } + GamePacket::ClientboundDisconnectPacket(p) => { + println!("Got disconnect packet {:?}", p); + } + GamePacket::ClientboundUpdateRecipesPacket(_p) => { + println!("Got update recipes packet"); + } + GamePacket::ClientboundEntityEventPacket(p) => { + // println!("Got entity event packet {:?}", p); + } + GamePacket::ClientboundRecipePacket(_p) => { + println!("Got recipe packet"); + } + GamePacket::ClientboundPlayerPositionPacket(p) => { + // TODO: reply with teleport confirm + println!("Got player position packet {:?}", p); + } + GamePacket::ClientboundPlayerInfoPacket(p) => { + println!("Got player info packet {:?}", p); + } + GamePacket::ClientboundSetChunkCacheCenterPacket(p) => { + println!("Got chunk cache center packet {:?}", p); + state + .lock() + .await + .world + .as_mut() + .unwrap() + .update_view_center(&ChunkPos::new(p.x, p.z)); + } + GamePacket::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); + // println("chunk {:?}") + state + .lock() + .await + .world + .as_mut() + .expect("World doesn't exist! We should've gotten a login packet by now.") + .replace_with_packet_data(&pos, &mut p.chunk_data.data.as_slice()) + .unwrap(); + } + GamePacket::ClientboundLightUpdatePacket(p) => { + println!("Got light update packet {:?}", p); + } + GamePacket::ClientboundAddEntityPacket(p) => { + println!("Got add entity packet {:?}", p); + let entity = Entity::from(p); + state + .lock() + .await + .world + .as_mut() + .expect("World doesn't exist! We should've gotten a login packet by now.") + .entities + .insert(entity); + } + GamePacket::ClientboundSetEntityDataPacket(p) => { + // println!("Got set entity data packet {:?}", p); + } + GamePacket::ClientboundUpdateAttributesPacket(p) => { + // println!("Got update attributes packet {:?}", p); + } + GamePacket::ClientboundEntityVelocityPacket(p) => { + // println!("Got entity velocity packet {:?}", p); + } + GamePacket::ClientboundSetEntityLinkPacket(p) => { + println!("Got set entity link packet {:?}", p); + } + GamePacket::ClientboundAddPlayerPacket(p) => { + println!("Got add player packet {:?}", p); + } + GamePacket::ClientboundInitializeBorderPacket(p) => { + println!("Got initialize border packet {:?}", p); + } + GamePacket::ClientboundSetTimePacket(p) => { + println!("Got set time packet {:?}", p); + } + GamePacket::ClientboundSetDefaultSpawnPositionPacket(p) => { + println!("Got set default spawn position packet {:?}", p); + } + GamePacket::ClientboundContainerSetContentPacket(p) => { + println!("Got container set content packet {:?}", p); + } + GamePacket::ClientboundSetHealthPacket(p) => { + println!("Got set health packet {:?}", p); + } + GamePacket::ClientboundSetExperiencePacket(p) => { + println!("Got set experience packet {:?}", p); + } + GamePacket::ClientboundTeleportEntityPacket(p) => { + // println!("Got teleport entity packet {:?}", p); + } + GamePacket::ClientboundUpdateAdvancementsPacket(p) => { + println!("Got update advancements packet {:?}", p); + } + GamePacket::ClientboundRotateHeadPacket(p) => { + // println!("Got rotate head packet {:?}", p); + } + GamePacket::ClientboundMoveEntityPosPacket(p) => { + // println!("Got move entity pos packet {:?}", p); + } + GamePacket::ClientboundMoveEntityPosRotPacket(p) => { + // println!("Got move entity pos rot packet {:?}", p); + } + GamePacket::ClientboundMoveEntityRotPacket(p) => { + println!("Got move entity rot packet {:?}", p); + } + GamePacket::ClientboundKeepAlivePacket(p) => { + println!("Got keep alive packet {:?}", p); + conn.lock() + .await + .write(ServerboundKeepAlivePacket { id: p.id }.get()) + .await; + } + GamePacket::ClientboundRemoveEntitiesPacket(p) => { + println!("Got remove entities packet {:?}", p); + } + GamePacket::ClientboundPlayerChatPacket(p) => { + println!("Got player chat packet {:?}", p); + tx.send(Event::Chat(ChatPacket::Player(p.clone()))).unwrap(); + } + GamePacket::ClientboundSystemChatPacket(p) => { + println!("Got system chat packet {:?}", p); + tx.send(Event::Chat(ChatPacket::System(p.clone()))).unwrap(); + } + GamePacket::ClientboundSoundPacket(p) => { + println!("Got sound packet {:?}", p); + } + GamePacket::ClientboundLevelEventPacket(p) => { + println!("Got level event packet {:?}", p); + } + GamePacket::ClientboundBlockUpdatePacket(p) => { + println!("Got block update packet {:?}", p); + // TODO: update world + } + GamePacket::ClientboundAnimatePacket(p) => { + println!("Got animate packet {:?}", p); + } + GamePacket::ClientboundSectionBlocksUpdatePacket(p) => { + println!("Got section blocks update packet {:?}", p); + // TODO: update world + } + GamePacket::ClientboundGameEventPacket(p) => { + println!("Got game event packet {:?}", p); + } + GamePacket::ClientboundLevelParticlesPacket(p) => { + println!("Got level particles packet {:?}", p); + } + GamePacket::ClientboundServerDataPacket(p) => { + println!("Got server data packet {:?}", p); + } + GamePacket::ClientboundSetEquipmentPacket(p) => { + println!("Got set equipment packet {:?}", p); + } + _ => panic!("Unexpected packet {:?}", packet), + } + } + + pub async fn next(&mut self) -> Option { + self.event_receiver.recv().await + } +} diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs deleted file mode 100755 index dd3162eb..00000000 --- a/azalea-client/src/connect.rs +++ /dev/null @@ -1,483 +0,0 @@ -use crate::Player; -use azalea_core::{resource_location::ResourceLocation, ChunkPos, EntityPos}; -use azalea_entity::Entity; -use azalea_protocol::{ - connect::{GameConnection, HandshakeConnection}, - packets::{ - game::{ - clientbound_player_chat_packet::ClientboundPlayerChatPacket, - clientbound_system_chat_packet::ClientboundSystemChatPacket, - serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, - serverbound_keep_alive_packet::ServerboundKeepAlivePacket, GamePacket, - }, - handshake::client_intention_packet::ClientIntentionPacket, - login::{ - serverbound_hello_packet::ServerboundHelloPacket, - serverbound_key_packet::{NonceOrSaltSignature, ServerboundKeyPacket}, - LoginPacket, - }, - ConnectionProtocol, PROTOCOL_VERSION, - }, - resolver, ServerAddress, -}; -use azalea_world::{ChunkStorage, EntityStorage, World}; -use std::{fmt::Debug, sync::Arc}; -use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; -use tokio::sync::Mutex; - -///! Connect to Minecraft servers. - -/// Something that can join Minecraft servers. -pub struct Account { - username: String, -} - -#[derive(Default)] -pub struct ClientState { - pub player: Player, - pub world: Option, -} - -/// A player that you can control that is currently in a Minecraft server. -pub struct Client { - event_receiver: UnboundedReceiver, - pub conn: Arc>, - pub state: Arc>, - // game_loop -} - -#[derive(Debug, Clone)] -pub enum ChatPacket { - System(ClientboundSystemChatPacket), - Player(ClientboundPlayerChatPacket), -} - -// impl ChatPacket { -// pub fn message(&self) -> &str { -// match self { -// ChatPacket::System(p) => &p.content, -// ChatPacket::Player(p) => &p.message, -// } -// } -// } - -#[derive(Debug, Clone)] -pub enum Event { - Login, - Chat(ChatPacket), -} - -/// Whether we should ignore errors when decoding packets. -const IGNORE_ERRORS: bool = false; - -impl Client { - async fn join(account: &Account, address: &ServerAddress) -> Result { - let resolved_address = resolver::resolve_address(address).await?; - - let mut conn = HandshakeConnection::new(&resolved_address).await?; - - // handshake - conn.write( - ClientIntentionPacket { - protocol_version: PROTOCOL_VERSION, - hostname: address.host.clone(), - port: address.port, - intention: ConnectionProtocol::Login, - } - .get(), - ) - .await; - let mut conn = conn.login(); - - // login - conn.write( - ServerboundHelloPacket { - username: account.username.clone(), - public_key: None, - } - .get(), - ) - .await; - - let conn = loop { - let packet_result = conn.read().await; - match packet_result { - Ok(packet) => match packet { - LoginPacket::ClientboundHelloPacket(p) => { - println!("Got encryption request"); - let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap(); - - // TODO: authenticate with the server here (authenticateServer) - - conn.write( - ServerboundKeyPacket { - nonce_or_salt_signature: NonceOrSaltSignature::Nonce( - e.encrypted_nonce, - ), - key_bytes: e.encrypted_public_key, - } - .get(), - ) - .await; - conn.set_encryption_key(e.secret_key); - } - LoginPacket::ClientboundLoginCompressionPacket(p) => { - println!("Got compression request {:?}", p.compression_threshold); - conn.set_compression_threshold(p.compression_threshold); - } - LoginPacket::ClientboundGameProfilePacket(p) => { - println!("Got profile {:?}", p.game_profile); - break conn.game(); - } - LoginPacket::ClientboundLoginDisconnectPacket(p) => { - println!("Got disconnect {:?}", p); - } - LoginPacket::ClientboundCustomQueryPacket(p) => { - println!("Got custom query {:?}", p); - } - _ => panic!("Unexpected packet {:?}", packet), - }, - Err(e) => { - panic!("Error: {:?}", e); - } - } - }; - - let conn = Arc::new(Mutex::new(conn)); - - let (tx, rx) = mpsc::unbounded_channel(); - - // we got the GameConnection, so the server is now connected :) - let client = Client { - event_receiver: rx, - conn: conn.clone(), - state: Arc::new(Mutex::new(ClientState::default())), - }; - // let client = Arc::new(Mutex::new(client)); - // let weak_client = Arc::<_>::downgrade(&client); - - // just start up the game loop and we're ready! - // tokio::spawn(Self::game_loop(conn, tx, handler, state)) - - let game_loop_state = client.state.clone(); - - tokio::spawn(Self::game_loop(conn, tx, game_loop_state)); - - Ok(client) - } - - async fn game_loop( - conn: Arc>, - tx: UnboundedSender, - state: Arc>, - ) { - loop { - let r = conn.lock().await.read().await; - match r { - Ok(packet) => Self::handle(&packet, &tx, &state, &conn).await, - Err(e) => { - if IGNORE_ERRORS { - println!("Error: {:?}", e); - if e == "length wider than 21-bit" { - panic!(); - } - } else { - panic!("Error: {:?}", e); - } - } - }; - } - } - - async fn handle( - packet: &GamePacket, - tx: &UnboundedSender, - state: &Arc>, - conn: &Arc>, - ) { - match packet { - GamePacket::ClientboundLoginPacket(p) => { - println!("Got login packet {:?}", p); - - let mut state = state.lock().await; - - // // write p into login.txt - // std::io::Write::write_all( - // &mut std::fs::File::create("login.txt").unwrap(), - // format!("{:#?}", p).as_bytes(), - // ) - // .unwrap(); - - state.player.entity.id = p.player_id; - - // TODO: have registry_holder be a struct because this sucks rn - // best way would be to add serde support to azalea-nbt - - let registry_holder = p - .registry_holder - .as_compound() - .expect("Registry holder is not a compound") - .get("") - .expect("No \"\" tag") - .as_compound() - .expect("\"\" tag is not a compound"); - let dimension_types = registry_holder - .get("minecraft:dimension_type") - .expect("No dimension_type tag") - .as_compound() - .expect("dimension_type is not a compound") - .get("value") - .expect("No dimension_type value") - .as_list() - .expect("dimension_type value is not a list"); - let dimension_type = dimension_types - .iter() - .find(|t| { - t.as_compound() - .expect("dimension_type value is not a compound") - .get("name") - .expect("No name tag") - .as_string() - .expect("name is not a string") - == p.dimension_type.to_string() - }) - .expect(&format!("No dimension_type with name {}", p.dimension_type)) - .as_compound() - .unwrap() - .get("element") - .expect("No element tag") - .as_compound() - .expect("element is not a compound"); - let height = (*dimension_type - .get("height") - .expect("No height tag") - .as_int() - .expect("height tag is not an int")) - .try_into() - .expect("height is not a u32"); - let min_y = (*dimension_type - .get("min_y") - .expect("No min_y tag") - .as_int() - .expect("min_y tag is not an int")) - .try_into() - .expect("min_y is not an i32"); - - state.world = Some(World { - height, - min_y, - storage: ChunkStorage::new(16), - entities: EntityStorage::new(), - }); - - conn.lock() - .await - .write( - ServerboundCustomPayloadPacket { - identifier: ResourceLocation::new("brand").unwrap(), - // they don't have to know :) - data: "vanilla".into(), - } - .get(), - ) - .await; - - tx.send(Event::Login).unwrap(); - } - GamePacket::ClientboundUpdateViewDistancePacket(p) => { - println!("Got view distance packet {:?}", p); - } - GamePacket::ClientboundCustomPayloadPacket(p) => { - println!("Got custom payload packet {:?}", p); - } - GamePacket::ClientboundChangeDifficultyPacket(p) => { - println!("Got difficulty packet {:?}", p); - } - GamePacket::ClientboundDeclareCommandsPacket(_p) => { - println!("Got declare commands packet"); - } - GamePacket::ClientboundPlayerAbilitiesPacket(p) => { - println!("Got player abilities packet {:?}", p); - } - GamePacket::ClientboundSetCarriedItemPacket(p) => { - println!("Got set carried item packet {:?}", p); - } - GamePacket::ClientboundUpdateTagsPacket(_p) => { - println!("Got update tags packet"); - } - GamePacket::ClientboundDisconnectPacket(p) => { - println!("Got disconnect packet {:?}", p); - } - GamePacket::ClientboundUpdateRecipesPacket(_p) => { - println!("Got update recipes packet"); - } - GamePacket::ClientboundEntityEventPacket(p) => { - // println!("Got entity event packet {:?}", p); - } - GamePacket::ClientboundRecipePacket(_p) => { - println!("Got recipe packet"); - } - GamePacket::ClientboundPlayerPositionPacket(p) => { - // TODO: reply with teleport confirm - println!("Got player position packet {:?}", p); - } - GamePacket::ClientboundPlayerInfoPacket(p) => { - println!("Got player info packet {:?}", p); - } - GamePacket::ClientboundSetChunkCacheCenterPacket(p) => { - println!("Got chunk cache center packet {:?}", p); - state - .lock() - .await - .world - .as_mut() - .unwrap() - .update_view_center(&ChunkPos::new(p.x, p.z)); - } - GamePacket::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); - // println("chunk {:?}") - state - .lock() - .await - .world - .as_mut() - .expect("World doesn't exist! We should've gotten a login packet by now.") - .replace_with_packet_data(&pos, &mut p.chunk_data.data.as_slice()) - .unwrap(); - } - GamePacket::ClientboundLightUpdatePacket(p) => { - println!("Got light update packet {:?}", p); - } - GamePacket::ClientboundAddEntityPacket(p) => { - println!("Got add entity packet {:?}", p); - let entity = Entity::from(p); - state - .lock() - .await - .world - .as_mut() - .expect("World doesn't exist! We should've gotten a login packet by now.") - .entities - .insert(entity); - } - GamePacket::ClientboundSetEntityDataPacket(p) => { - // println!("Got set entity data packet {:?}", p); - } - GamePacket::ClientboundUpdateAttributesPacket(p) => { - // println!("Got update attributes packet {:?}", p); - } - GamePacket::ClientboundEntityVelocityPacket(p) => { - // println!("Got entity velocity packet {:?}", p); - } - GamePacket::ClientboundSetEntityLinkPacket(p) => { - println!("Got set entity link packet {:?}", p); - } - GamePacket::ClientboundAddPlayerPacket(p) => { - println!("Got add player packet {:?}", p); - } - GamePacket::ClientboundInitializeBorderPacket(p) => { - println!("Got initialize border packet {:?}", p); - } - GamePacket::ClientboundSetTimePacket(p) => { - println!("Got set time packet {:?}", p); - } - GamePacket::ClientboundSetDefaultSpawnPositionPacket(p) => { - println!("Got set default spawn position packet {:?}", p); - } - GamePacket::ClientboundContainerSetContentPacket(p) => { - println!("Got container set content packet {:?}", p); - } - GamePacket::ClientboundSetHealthPacket(p) => { - println!("Got set health packet {:?}", p); - } - GamePacket::ClientboundSetExperiencePacket(p) => { - println!("Got set experience packet {:?}", p); - } - GamePacket::ClientboundTeleportEntityPacket(p) => { - // println!("Got teleport entity packet {:?}", p); - } - GamePacket::ClientboundUpdateAdvancementsPacket(p) => { - println!("Got update advancements packet {:?}", p); - } - GamePacket::ClientboundRotateHeadPacket(p) => { - // println!("Got rotate head packet {:?}", p); - } - GamePacket::ClientboundMoveEntityPosPacket(p) => { - // println!("Got move entity pos packet {:?}", p); - } - GamePacket::ClientboundMoveEntityPosRotPacket(p) => { - // println!("Got move entity pos rot packet {:?}", p); - } - GamePacket::ClientboundMoveEntityRotPacket(p) => { - println!("Got move entity rot packet {:?}", p); - } - GamePacket::ClientboundKeepAlivePacket(p) => { - println!("Got keep alive packet {:?}", p); - conn.lock() - .await - .write(ServerboundKeepAlivePacket { id: p.id }.get()) - .await; - } - GamePacket::ClientboundRemoveEntitiesPacket(p) => { - println!("Got remove entities packet {:?}", p); - } - GamePacket::ClientboundPlayerChatPacket(p) => { - println!("Got player chat packet {:?}", p); - tx.send(Event::Chat(ChatPacket::Player(p.clone()))).unwrap(); - } - GamePacket::ClientboundSystemChatPacket(p) => { - println!("Got system chat packet {:?}", p); - tx.send(Event::Chat(ChatPacket::System(p.clone()))).unwrap(); - } - GamePacket::ClientboundSoundPacket(p) => { - println!("Got sound packet {:?}", p); - } - GamePacket::ClientboundLevelEventPacket(p) => { - println!("Got level event packet {:?}", p); - } - GamePacket::ClientboundBlockUpdatePacket(p) => { - println!("Got block update packet {:?}", p); - // TODO: update world - } - GamePacket::ClientboundAnimatePacket(p) => { - println!("Got animate packet {:?}", p); - } - GamePacket::ClientboundSectionBlocksUpdatePacket(p) => { - println!("Got section blocks update packet {:?}", p); - // TODO: update world - } - GamePacket::ClientboundGameEventPacket(p) => { - println!("Got game event packet {:?}", p); - } - GamePacket::ClientboundLevelParticlesPacket(p) => { - println!("Got level particles packet {:?}", p); - } - GamePacket::ClientboundServerDataPacket(p) => { - println!("Got server data packet {:?}", p); - } - GamePacket::ClientboundSetEquipmentPacket(p) => { - println!("Got set equipment packet {:?}", p); - } - _ => panic!("Unexpected packet {:?}", packet), - } - } - - pub async fn next(&mut self) -> Option { - self.event_receiver.recv().await - } -} - -impl Account { - pub fn offline(username: &str) -> Self { - Self { - username: username.to_string(), - } - } - - pub async fn join(&self, address: &ServerAddress) -> Result { - Client::join(self, address).await - } -} diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index db935897..867f05a1 100755 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -1,10 +1,12 @@ //! Significantly abstract azalea-protocol so it's actually useable for bots. -mod connect; +mod account; +mod client; pub mod ping; mod player; -pub use connect::{Account, Client, Event}; +pub use account::Account; +pub use client::{Client, Event}; pub use player::Player; #[cfg(test)] diff --git a/azalea-world/src/entity.rs b/azalea-world/src/entity.rs index a25b1f40..49e1ae73 100644 --- a/azalea-world/src/entity.rs +++ b/azalea-world/src/entity.rs @@ -4,6 +4,7 @@ use azalea_core::ChunkPos; use azalea_entity::Entity; use nohash_hasher::IntMap; +#[derive(Debug)] pub struct EntityStorage { by_id: IntMap, // TODO: this doesn't work yet (should be updated in the set_pos method in azalea-entity) diff --git a/bot/src/main.rs b/bot/src/main.rs index 0b9da787..bfcba7f5 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,5 +1,4 @@ use azalea_client::{Account, Event}; -use azalea_core::BlockPos; #[tokio::main] async fn main() { @@ -20,10 +19,10 @@ async fn main() { match e { // TODO: have a "loaded" or "ready" event that fires when all chunks are loaded Event::Login => {} - Event::Chat(p) => { + Event::Chat(_p) => { let state = client.state.lock().await; let world = state.world.as_ref().unwrap(); - println!("{:?}", state.world.entities.get_player(player)); + println!("{:?}", world.entities); // world.get_block_state(state.player.entity.pos); // println!("{}", p.message.to_ansi(None)); // if p.message.to_ansi(None) == " ok" { -- cgit v1.2.3 From c7b0c51274b5d8548c8a2f829b75dfbec4038be2 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 21 Jun 2022 23:19:31 -0500 Subject: Add move_to --- azalea-client/src/lib.rs | 1 + azalea-client/src/movement.rs | 24 ++++++++++++++++++++++++ bot/src/main.rs | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 azalea-client/src/movement.rs (limited to 'azalea-client/src/lib.rs') diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index 867f05a1..c3c37460 100755 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -2,6 +2,7 @@ mod account; mod client; +mod movement; pub mod ping; mod player; diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs new file mode 100644 index 00000000..9f5cd27c --- /dev/null +++ b/azalea-client/src/movement.rs @@ -0,0 +1,24 @@ +use azalea_core::EntityPos; +use crate::Client; +use azalea_protocol::packets::game::serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot; + +impl Client { + /// Set the client's position to the given coordinates. + pub async fn move_to(&mut self, pos: &EntityPos) { + self.conn + .lock() + .await + .write( + ServerboundMovePlayerPacketPosRot { + x: pos.x, + y: pos.y, + z: pos.z, + x_rot: 0.0, + y_rot: 0.0, + on_ground: false, + } + .get(), + ) + .await; + } +} diff --git a/bot/src/main.rs b/bot/src/main.rs index 6ff4cc0b..546a9244 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -6,7 +6,7 @@ async fn main() -> Result<(), Box> { println!("Hello, world!"); // let address = "95.111.249.143:10000"; - let address = "localhost:59021"; + let address = "localhost:57172"; // let response = azalea_client::ping::ping_server(&address.try_into().unwrap()) // .await // .unwrap(); -- cgit v1.2.3