From ca39b8b6af27a91431d2a837037881bc882f0dde Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 13 May 2023 19:08:08 -0500 Subject: reexport azalea-chat and add goto docs --- azalea/src/lib.rs | 1 + azalea/src/pathfinder/mod.rs | 7 +++++++ 2 files changed, 8 insertions(+) (limited to 'azalea/src') diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 604961f4..8a00ff71 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -14,6 +14,7 @@ use app::{App, Plugin, PluginGroup}; pub use azalea_auth as auth; pub use azalea_block as blocks; pub use azalea_brigadier as brigadier; +pub use azalea_chat as chat; pub use azalea_client::*; pub use azalea_core::{BlockPos, Vec3}; pub use azalea_protocol as protocol; diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs index 3f978c95..4db18b0e 100644 --- a/azalea/src/pathfinder/mod.rs +++ b/azalea/src/pathfinder/mod.rs @@ -70,6 +70,13 @@ pub trait PathfinderClientExt { } impl PathfinderClientExt for azalea_client::Client { + /// ```no_run + /// # use azalea::prelude::*; + /// # use azalea::{BlockPos, pathfinder::BlockPosGoal}; + /// # fn example(bot: &Client) { + /// bot.goto(BlockPosGoal::from(BlockPos::new(0, 70, 0))); + /// # } + /// ``` fn goto(&self, goal: impl Goal + Send + Sync + 'static) { self.ecs.lock().send_event(GotoEvent { entity: self.entity, -- cgit v1.2.3 From 0cc76dfb67e852868e30d1c8529826c41a18e9d1 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 13 May 2023 19:29:55 -0500 Subject: ChatPacket::is_whisper --- azalea-client/src/chat.rs | 10 ++++++++++ azalea/src/lib.rs | 2 +- azalea/src/pathfinder/mod.rs | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) (limited to 'azalea/src') diff --git a/azalea-client/src/chat.rs b/azalea-client/src/chat.rs index 0ae0250a..90618c80 100755 --- a/azalea-client/src/chat.rs +++ b/azalea-client/src/chat.rs @@ -108,6 +108,16 @@ impl ChatPacket { overlay: false, })) } + + /// Whether this message was sent with /msg (or aliases). It works by + /// checking the translation key, so it won't work on servers that use their + /// own whisper system. + pub fn is_whisper(&self) -> bool { + match self.message() { + FormattedText::Text(_) => false, + FormattedText::Translatable(t) => t.key == "commands.message.display.incoming", + } + } } impl Client { diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 8a00ff71..dcfa43b1 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -14,7 +14,7 @@ use app::{App, Plugin, PluginGroup}; pub use azalea_auth as auth; pub use azalea_block as blocks; pub use azalea_brigadier as brigadier; -pub use azalea_chat as chat; +pub use azalea_chat::FormattedText; pub use azalea_client::*; pub use azalea_core::{BlockPos, Vec3}; pub use azalea_protocol as protocol; diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs index 4db18b0e..b7ed222d 100644 --- a/azalea/src/pathfinder/mod.rs +++ b/azalea/src/pathfinder/mod.rs @@ -70,7 +70,7 @@ pub trait PathfinderClientExt { } impl PathfinderClientExt for azalea_client::Client { - /// ```no_run + /// ``` /// # use azalea::prelude::*; /// # use azalea::{BlockPos, pathfinder::BlockPosGoal}; /// # fn example(bot: &Client) { -- cgit v1.2.3 From 2ba7b83490f5fb6e40a4e94da743bebe23cd7862 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 16 May 2023 23:26:45 -0500 Subject: ClientBuilder::new_without_plugins --- Cargo.lock | 1 + azalea-client/src/client.rs | 49 +++++++++++++++++------------------------- azalea-client/src/interact.rs | 2 +- azalea-client/src/inventory.rs | 4 ++-- azalea-client/src/lib.rs | 2 +- azalea-client/src/respawn.rs | 21 +++++++++--------- azalea/Cargo.toml | 1 + azalea/src/lib.rs | 34 +++++++++++++++++++++++++---- azalea/src/pathfinder/moves.rs | 3 ++- azalea/src/swarm/mod.rs | 41 +++++++++++++++++++++++++++++++---- 10 files changed, 105 insertions(+), 53 deletions(-) (limited to 'azalea/src') diff --git a/Cargo.lock b/Cargo.lock index dcf982f2..2aa7d0eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,6 +177,7 @@ dependencies = [ "azalea-world", "bevy_app", "bevy_ecs", + "bevy_log", "bevy_tasks", "derive_more", "futures", diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index f2e91dfa..739ec434 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -221,7 +221,10 @@ impl Client { // An event that causes the schedule to run. This is only used internally. let (run_schedule_sender, run_schedule_receiver) = mpsc::unbounded_channel(); - let app = init_ecs_app(); + + let mut app = App::new(); + app.add_plugins(DefaultPlugins); + let ecs_lock = start_ecs(app, run_schedule_receiver, run_schedule_sender.clone()); Self::start_client( @@ -583,35 +586,10 @@ impl Plugin for AzaleaPlugin { } } -/// Create the [`App`]. This won't actually run anything yet. +/// Start running the ECS loop! /// -/// Note that you usually only need this if you're creating a client manually, -/// otherwise use [`Client::join`]. -/// -/// Use [`start_ecs`] to actually start running the app and then -/// [`Client::start_client`] to add a client to the ECS and make it join a -/// server. -#[doc(hidden)] -pub fn init_ecs_app() -> App { - // if you get an error right here that means you're doing something with locks - // wrong read the error to see where the issue is - // you might be able to just drop the lock or put it in its own scope to fix - - let mut app = App::new(); - - app.edit_schedule(CoreSchedule::Main, |schedule| { - schedule.set_build_settings(ScheduleBuildSettings { - ambiguity_detection: LogLevel::Warn, - ..Default::default() - }); - }); - - app.add_plugins(DefaultPlugins); - app -} - -/// Start running the ECS loop! You must create your `App` from [`init_ecs_app`] -/// first. +/// You can create your app with `App::new()`, but don't forget to add +/// [`DefaultPlugins`]. #[doc(hidden)] pub fn start_ecs( mut app: App, @@ -698,6 +676,18 @@ impl Plugin for TickBroadcastPlugin { } } +pub struct AmbiguityLoggerPlugin; +impl Plugin for AmbiguityLoggerPlugin { + fn build(&self, app: &mut App) { + app.edit_schedule(CoreSchedule::Main, |schedule| { + schedule.set_build_settings(ScheduleBuildSettings { + ambiguity_detection: LogLevel::Warn, + ..Default::default() + }); + }); + } +} + /// This plugin group will add all the default plugins necessary for Azalea to /// work. pub struct DefaultPlugins; @@ -706,6 +696,7 @@ impl PluginGroup for DefaultPlugins { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() .add(LogPlugin::default()) + .add(AmbiguityLoggerPlugin) .add(TimePlugin::default()) .add(PacketHandlerPlugin) .add(AzaleaPlugin) diff --git a/azalea-client/src/interact.rs b/azalea-client/src/interact.rs index 2da80760..fa05aa49 100644 --- a/azalea-client/src/interact.rs +++ b/azalea-client/src/interact.rs @@ -73,7 +73,7 @@ pub struct CurrentSequenceNumber(u32); #[derive(Component, Clone, Debug, Deref, DerefMut)] pub struct HitResultComponent(BlockHitResult); -fn handle_block_interact_event( +pub fn handle_block_interact_event( mut events: EventReader, mut query: Query<( &LocalPlayer, diff --git a/azalea-client/src/inventory.rs b/azalea-client/src/inventory.rs index d6f909a7..bce629b0 100644 --- a/azalea-client/src/inventory.rs +++ b/azalea-client/src/inventory.rs @@ -628,7 +628,7 @@ fn handle_container_close_event( pub struct ClientSideCloseContainerEvent { pub entity: Entity, } -fn handle_client_side_close_container_event( +pub fn handle_client_side_close_container_event( mut events: EventReader, mut query: Query<&mut InventoryComponent>, ) { @@ -645,7 +645,7 @@ pub struct ContainerClickEvent { pub window_id: u8, pub operation: ClickOperation, } -fn handle_container_click_event( +pub fn handle_container_click_event( mut events: EventReader, mut query: Query<(&mut InventoryComponent, &LocalPlayer)>, ) { diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index 2c08033b..10a50e92 100644 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -30,7 +30,7 @@ pub mod task_pool; pub use account::{Account, AccountOpts}; pub use client::{ - init_ecs_app, start_ecs, Client, ClientInformation, JoinError, JoinedClientBundle, TabList, + start_ecs, Client, ClientInformation, DefaultPlugins, JoinError, JoinedClientBundle, TabList, TickBroadcast, }; pub use events::Event; diff --git a/azalea-client/src/respawn.rs b/azalea-client/src/respawn.rs index 4e42157c..3ac17612 100644 --- a/azalea-client/src/respawn.rs +++ b/azalea-client/src/respawn.rs @@ -4,7 +4,7 @@ use azalea_protocol::packets::game::serverbound_client_command_packet::{ use bevy_app::{App, Plugin}; use bevy_ecs::prelude::*; -use crate::LocalPlayer; +use crate::local_player::{handle_send_packet_event, SendPacketEvent}; /// Tell the server that we're respawning. #[derive(Debug, Clone)] @@ -17,22 +17,21 @@ pub struct RespawnPlugin; impl Plugin for RespawnPlugin { fn build(&self, app: &mut App) { app.add_event::() - .add_system(perform_respawn); + .add_system(perform_respawn.before(handle_send_packet_event)); } } pub fn perform_respawn( mut events: EventReader, - mut query: Query<&mut LocalPlayer>, + mut send_packets: EventWriter, ) { for event in events.iter() { - if let Ok(local_player) = query.get_mut(event.entity) { - local_player.write_packet( - ServerboundClientCommandPacket { - action: serverbound_client_command_packet::Action::PerformRespawn, - } - .get(), - ); - } + send_packets.send(SendPacketEvent { + entity: event.entity, + packet: ServerboundClientCommandPacket { + action: serverbound_client_command_packet::Action::PerformRespawn, + } + .get(), + }); } } diff --git a/azalea/Cargo.toml b/azalea/Cargo.toml index 4a7469d3..f7d050f6 100644 --- a/azalea/Cargo.toml +++ b/azalea/Cargo.toml @@ -39,3 +39,4 @@ priority-queue = "1.3.0" thiserror = "^1.0.37" tokio = "^1.24.2" uuid = "1.2.2" +bevy_log = "0.10.1" diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index dcfa43b1..09e88534 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -20,7 +20,7 @@ pub use azalea_core::{BlockPos, Vec3}; pub use azalea_protocol as protocol; pub use azalea_registry::{Block, EntityKind, Item}; pub use azalea_world::{entity, Instance}; -use bot::DefaultBotPlugins; +pub use bot::DefaultBotPlugins; use ecs::component::Component; use futures::Future; use protocol::{ @@ -81,16 +81,42 @@ where /// Start building a client that can join the world. #[must_use] pub fn new() -> Self { + Self::new_without_plugins() + .add_plugins(DefaultPlugins) + .add_plugins(DefaultBotPlugins) + } + + /// [`Self::new`] but without adding the plugins by default. This is useful + /// if you want to disable a default plugin. + /// + /// You **must** add [`DefaultPlugins`] and [`DefaultBotPlugins`] to this. + /// + /// ``` + /// # use azalea::prelude::*; + /// use azalea::{app::PluginGroup, DefaultBotPlugins, DefaultPlugins}; + /// use bevy_log::LogPlugin; + /// + /// let client = ClientBuilder::new_without_plugins() + /// .add_plugins(DefaultPlugins.build().disable::()) + /// .add_plugins(DefaultBotPlugins); + /// # client.set_handler(handle); + /// # #[derive(Component, Clone, Default)] + /// # pub struct State; + /// # async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> { + /// # Ok(()) + /// # } + /// ``` + #[must_use] + pub fn new_without_plugins() -> Self { Self { // we create the app here so plugins can add onto it. // the schedules won't run until [`Self::start`] is called. - app: init_ecs_app(), - + app: App::new(), handler: None, state: S::default(), } - .add_plugins(DefaultBotPlugins) } + /// Set the function that's called every time a bot receives an [`Event`]. /// This is the way to handle normal per-bot events. /// diff --git a/azalea/src/pathfinder/moves.rs b/azalea/src/pathfinder/moves.rs index 6625581d..0dca3c95 100644 --- a/azalea/src/pathfinder/moves.rs +++ b/azalea/src/pathfinder/moves.rs @@ -15,7 +15,8 @@ fn is_block_passable(pos: &BlockPos, world: &Instance) -> bool { /// whether this block has a solid hitbox (i.e. we can stand on it) fn is_block_solid(pos: &BlockPos, world: &Instance) -> bool { if let Some(block) = world.chunks.get_block_state(pos) { - block.shape() == &collision::block_shape() + // block.shape() == &collision::block_shape() + block.shape() != &collision::empty_shape() } else { false } diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index 2253f5bd..f138950f 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -5,7 +5,9 @@ mod events; pub mod prelude; use crate::{bot::DefaultBotPlugins, HandleFn}; -use azalea_client::{chat::ChatPacket, init_ecs_app, start_ecs, Account, Client, Event, JoinError}; +use azalea_client::{ + chat::ChatPacket, start_ecs, Account, Client, DefaultPlugins, Event, JoinError, +}; use azalea_protocol::{ connect::ConnectionError, resolver::{self, ResolverError}, @@ -83,10 +85,43 @@ where /// Start creating the swarm. #[must_use] pub fn new() -> Self { + Self::new_without_plugins() + .add_plugins(DefaultPlugins) + .add_plugins(DefaultBotPlugins) + .add_plugins(DefaultSwarmPlugins) + } + + /// [`Self::new`] but without adding the plugins by default. This is useful + /// if you want to disable a default plugin. + /// + /// You **must** add [`DefaultPlugins`], [`DefaultBotPlugins`], and + /// [`DefaultSwarmPlugins`] to this. + /// + /// ``` + /// # use azalea::{prelude::*, swarm::prelude::*}; + /// use azalea::{app::PluginGroup, DefaultBotPlugins, DefaultPlugins, swarm::{DefaultSwarmPlugins}}; + /// use bevy_log::LogPlugin; + /// + /// let client = SwarmBuilder::new_without_plugins() + /// .add_plugins(DefaultPlugins.build().disable::()) + /// .add_plugins(DefaultBotPlugins) + /// .add_plugins(DefaultSwarmPlugins); + /// # client.set_handler(handle).set_swarm_handler(swarm_handle); + /// # #[derive(Component, Resource, Clone, Default)] + /// # pub struct State; + /// # async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> { + /// # Ok(()) + /// # } + /// # async fn swarm_handle(swarm: Swarm, event: SwarmEvent, state: State) -> anyhow::Result<()> { + /// # Ok(()) + /// # } + /// ``` + #[must_use] + pub fn new_without_plugins() -> Self { Self { // we create the app here so plugins can add onto it. // the schedules won't run until [`Self::start`] is called. - app: init_ecs_app(), + app: App::new(), accounts: Vec::new(), states: Vec::new(), @@ -95,8 +130,6 @@ where swarm_handler: None, join_delay: None, } - .add_plugins(DefaultSwarmPlugins) - .add_plugins(DefaultBotPlugins) } /// Add a vec of [`Account`]s to the swarm. -- cgit v1.2.3 From e2f9d59c458bb226e88dca50355dbe49ce7f6fc7 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 16 May 2023 23:32:10 -0500 Subject: better variable naming in doc --- azalea/src/lib.rs | 4 ++-- azalea/src/swarm/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'azalea/src') diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 09e88534..fd735585 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -96,10 +96,10 @@ where /// use azalea::{app::PluginGroup, DefaultBotPlugins, DefaultPlugins}; /// use bevy_log::LogPlugin; /// - /// let client = ClientBuilder::new_without_plugins() + /// let client_builder = ClientBuilder::new_without_plugins() /// .add_plugins(DefaultPlugins.build().disable::()) /// .add_plugins(DefaultBotPlugins); - /// # client.set_handler(handle); + /// # client_builder.set_handler(handle); /// # #[derive(Component, Clone, Default)] /// # pub struct State; /// # async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> { diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index f138950f..0bb9f7cd 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -102,11 +102,11 @@ where /// use azalea::{app::PluginGroup, DefaultBotPlugins, DefaultPlugins, swarm::{DefaultSwarmPlugins}}; /// use bevy_log::LogPlugin; /// - /// let client = SwarmBuilder::new_without_plugins() + /// let swarm_builder = SwarmBuilder::new_without_plugins() /// .add_plugins(DefaultPlugins.build().disable::()) /// .add_plugins(DefaultBotPlugins) /// .add_plugins(DefaultSwarmPlugins); - /// # client.set_handler(handle).set_swarm_handler(swarm_handle); + /// # swarm_builder.set_handler(handle).set_swarm_handler(swarm_handle); /// # #[derive(Component, Resource, Clone, Default)] /// # pub struct State; /// # async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> { -- cgit v1.2.3 From eb65b0ad6e03f5ffcf8f0f2328ec91bb7e26259f Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 21 May 2023 17:29:28 -0500 Subject: fix respawn system ambiguity --- azalea/src/auto_respawn.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'azalea/src') diff --git a/azalea/src/auto_respawn.rs b/azalea/src/auto_respawn.rs index c5dd12a4..d2973a2f 100644 --- a/azalea/src/auto_respawn.rs +++ b/azalea/src/auto_respawn.rs @@ -1,6 +1,6 @@ use crate::app::{App, Plugin}; use azalea_client::packet_handling::DeathEvent; -use azalea_client::respawn::PerformRespawnEvent; +use azalea_client::respawn::{perform_respawn, PerformRespawnEvent}; use bevy_ecs::prelude::*; /// A plugin that makes [`DeathEvent`]s send [`PerformRespawnEvent`]s. @@ -8,7 +8,7 @@ use bevy_ecs::prelude::*; pub struct AutoRespawnPlugin; impl Plugin for AutoRespawnPlugin { fn build(&self, app: &mut App) { - app.add_system(auto_respawn); + app.add_system(auto_respawn.before(perform_respawn)); } } -- cgit v1.2.3