From c0829e6d0eedef6e4954e4d9aa9c9c1dd30dbae5 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 11 Jan 2023 04:56:11 +0000 Subject: [PATCH] simple example for azalea-client --- azalea-client/examples/echo.rs | 25 +++++++++++++++++++++++++ azalea-client/src/client.rs | 6 ++++++ azalea-client/src/lib.rs | 11 +---------- azalea-client/src/packet_handling.rs | 3 +++ azalea/src/pathfinder/mod.rs | 1 - 5 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 azalea-client/examples/echo.rs diff --git a/azalea-client/examples/echo.rs b/azalea-client/examples/echo.rs new file mode 100644 index 00000000..2f5baccd --- /dev/null +++ b/azalea-client/examples/echo.rs @@ -0,0 +1,25 @@ +//! A simple bot that repeats chat messages sent by other players. + +use azalea_client::{Account, Client, Event}; + +#[tokio::main] +async fn main() { + let account = Account::offline("bot"); + // or let account = Account::microsoft("email").await; + + let (client, mut rx) = Client::join(&account, "localhost").await.unwrap(); + + while let Some(event) = rx.recv().await { + match &event { + Event::Chat(m) => { + if let (Some(sender), content) = m.split_sender_and_content() { + if sender == client.profile.name { + continue; // ignore our own messages + } + client.chat(&content).await; + }; + } + _ => {} + } + } +} diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 7dda2097..0f49b99f 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -79,6 +79,12 @@ pub enum Event { /// Client has the things that a user interacting with the library will want. /// Things that a player in the world will want to know are in [`LocalPlayer`]. pub struct Client { + /// The [`GameProfile`] for our client. This contains your username, UUID, + /// and skin data. + /// + /// This is immutable; the server cannot change it. To get the username and + /// skin the server chose for you, get your player from + /// [`Self::players`]. pub profile: GameProfile, /// The entity for this client in the ECS. pub entity: Entity, diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index 5e4d9098..2656844e 100644 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -22,16 +22,7 @@ mod player; mod plugins; pub use account::Account; -pub use client::{ChatPacket, ClientInformation, Event, JoinError}; +pub use client::{ChatPacket, Client, ClientInformation, Event, JoinError}; pub use movement::{SprintDirection, WalkDirection}; pub use player::PlayerInfo; pub use plugins::{Plugin, PluginState, PluginStates, Plugins}; - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/azalea-client/src/packet_handling.rs b/azalea-client/src/packet_handling.rs index 959a27b3..f3224144 100644 --- a/azalea-client/src/packet_handling.rs +++ b/azalea-client/src/packet_handling.rs @@ -54,12 +54,14 @@ pub struct PacketReceiver { fn handle_packets( mut commands: Commands, + query: Query<(Entity, &PacketReceiver), Changed>, local_player_query: Query<&LocalPlayer>, entity_kind_query: Query<&EntityKind>, mut mut_local_player_query: Query<&mut LocalPlayer>, mut mut_health_query: Query<&mut Health>, mut mut_position_query: Query<&mut Position>, + combat_kill_query: Query<(&MinecraftEntityId, Option<&Dead>)>, mut position_query: Query<( &mut LocalPlayer, @@ -67,6 +69,7 @@ fn handle_packets( &mut Position, &mut LastSentPosition, )>, + mut world_container: ResMut, mut entity_infos: ResMut, ) { diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs index a1619c41..c884f910 100644 --- a/azalea/src/pathfinder/mod.rs +++ b/azalea/src/pathfinder/mod.rs @@ -5,7 +5,6 @@ use crate::{prelude::*, SprintDirection, WalkDirection}; use crate::{Client, Event}; use async_trait::async_trait; use azalea_core::{BlockPos, CardinalDirection}; -use azalea_world::entity::EntityData; use log::{debug, error}; use mtdstarlite::Edge; pub use mtdstarlite::MTDStarLite;