aboutsummaryrefslogtreecommitdiff
path: root/azalea/src
diff options
context:
space:
mode:
Diffstat (limited to 'azalea/src')
-rw-r--r--azalea/src/lib.rs31
-rw-r--r--azalea/src/prelude.rs7
-rw-r--r--azalea/src/swarm/chat.rs2
-rw-r--r--azalea/src/swarm/mod.rs65
4 files changed, 85 insertions, 20 deletions
diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs
index b7707b92..a34ea179 100644
--- a/azalea/src/lib.rs
+++ b/azalea/src/lib.rs
@@ -4,7 +4,7 @@
mod bot;
pub mod pathfinder;
pub mod prelude;
-mod swarm;
+pub mod swarm;
pub use azalea_block as blocks;
pub use azalea_client::*;
@@ -23,7 +23,6 @@ use protocol::{
resolver::{self, ResolverError},
ServerAddress,
};
-pub use swarm::*;
use thiserror::Error;
use tokio::sync::mpsc;
@@ -43,10 +42,19 @@ pub enum StartError {
/// making Azalea bots.
///
/// ```no_run
-/// azalea::ClientBuilder::new()
+/// # use azalea::prelude::*;
+/// # #[tokio::main]
+/// # async fn main() {
+/// ClientBuilder::new()
/// .set_handler(handle)
/// .start(Account::offline("bot"), "localhost")
/// .await;
+/// # }
+/// # #[derive(Component, Clone, Default)]
+/// # pub struct State;
+/// # async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> {
+/// # Ok(())
+/// # }
/// ```
pub struct ClientBuilder<S, Fut>
where
@@ -79,9 +87,20 @@ where
/// Set the function that's called every time a bot receives an [`Event`].
/// This is the way to handle normal per-bot events.
///
- /// You can only have one client handler, calling this again will replace
- /// the old client handler function (you can have a client handler and swarm
- /// handler separately though).
+ /// You must have exactly one client handler, calling this again will
+ /// replace the old client handler function.
+ ///
+ /// ```
+ /// # use azalea::prelude::*;
+ /// # let client_builder = azalea::ClientBuilder::new();
+ /// client_builder.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 set_handler(mut self, handler: HandleFn<Fut, S>) -> Self {
self.handler = Some(handler);
diff --git a/azalea/src/prelude.rs b/azalea/src/prelude.rs
index a26f72d1..a9ae6093 100644
--- a/azalea/src/prelude.rs
+++ b/azalea/src/prelude.rs
@@ -1,9 +1,6 @@
//! The Azalea prelude. Things that are necessary for a bare-bones bot are
//! re-exported here.
-pub use crate::bot::BotClientExt;
-pub use crate::pathfinder::PathfinderClientExt;
-pub use crate::{ClientBuilder, SwarmBuilder};
+pub use crate::{bot::BotClientExt, pathfinder::PathfinderClientExt, ClientBuilder};
pub use azalea_client::{Account, Client, Event};
-pub use azalea_ecs::component::Component;
-pub use azalea_ecs::system::Resource;
+pub use azalea_ecs::{component::Component, system::Resource};
diff --git a/azalea/src/swarm/chat.rs b/azalea/src/swarm/chat.rs
index a55e9bf6..8a00c34d 100644
--- a/azalea/src/swarm/chat.rs
+++ b/azalea/src/swarm/chat.rs
@@ -23,7 +23,7 @@ use azalea_ecs::{
};
use std::collections::VecDeque;
-use crate::{Swarm, SwarmEvent};
+use super::{Swarm, SwarmEvent};
#[derive(Clone)]
pub struct SwarmChatPlugin;
diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs
index 64312579..33402566 100644
--- a/azalea/src/swarm/mod.rs
+++ b/azalea/src/swarm/mod.rs
@@ -2,6 +2,7 @@
mod chat;
mod events;
+pub mod prelude;
use crate::{bot::DefaultBotPlugins, HandleFn};
use azalea_client::{init_ecs_app, start_ecs, Account, ChatPacket, Client, Event, JoinError};
@@ -136,9 +137,30 @@ where
/// Set the function that's called every time a bot receives an [`Event`].
/// This is the way to handle normal per-bot events.
///
- /// You can only have one client handler, calling this again will replace
- /// the old client handler function (you can have a client handler and swarm
- /// handler separately though).
+ /// You must have exactly one client handler and one swarm handler, calling
+ /// this again will replace the old client handler function.
+ ///
+ /// ```
+ /// # use azalea::{prelude::*, swarm::prelude::*};
+ /// # let swarm_builder = SwarmBuilder::new().set_swarm_handler(swarm_handle);
+ /// swarm_builder.set_handler(handle);
+ ///
+ /// #[derive(Component, Default, Clone)]
+ /// struct State {}
+ /// async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> {
+ /// Ok(())
+ /// }
+ ///
+ /// # #[derive(Resource, Default, Clone)]
+ /// # struct SwarmState {}
+ /// # async fn swarm_handle(
+ /// # mut swarm: Swarm,
+ /// # event: SwarmEvent,
+ /// # state: SwarmState,
+ /// # ) -> anyhow::Result<()> {
+ /// # Ok(())
+ /// # }
+ /// ```
#[must_use]
pub fn set_handler(mut self, handler: HandleFn<Fut, S>) -> Self {
self.handler = Some(handler);
@@ -147,9 +169,31 @@ where
/// Set the function that's called every time the swarm receives a
/// [`SwarmEvent`]. This is the way to handle global swarm events.
///
- /// You can only have one swarm handler, calling this again will replace
- /// the old swarm handler function (you can have a client handler and swarm
- /// handler separately though).
+ /// You must have exactly one client handler and one swarm handler, calling
+ /// this again will replace the old swarm handler function.
+ ///
+ /// ```
+ /// # use azalea::{prelude::*, swarm::prelude::*};
+ /// # let swarm_builder = SwarmBuilder::new().set_handler(handle);
+ /// swarm_builder.set_swarm_handler(swarm_handle);
+ ///
+ /// # #[derive(Component, Default, Clone)]
+ /// # struct State {}
+ ///
+ /// # async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> {
+ /// # Ok(())
+ /// # }
+ ///
+ /// #[derive(Resource, Default, Clone)]
+ /// struct SwarmState {}
+ /// async fn swarm_handle(
+ /// mut swarm: Swarm,
+ /// event: SwarmEvent,
+ /// state: SwarmState,
+ /// ) -> anyhow::Result<()> {
+ /// Ok(())
+ /// }
+ /// ```
#[must_use]
pub fn set_swarm_handler(mut self, handler: SwarmHandleFn<SwarmFut, SS>) -> Self {
self.swarm_handler = Some(handler);
@@ -337,7 +381,7 @@ pub enum SwarmStartError {
///
/// # Examples
/// ```rust,no_run
-/// use azalea::{prelude::*, Swarm, SwarmEvent};
+/// use azalea::{prelude::*, swarm::prelude::*};
/// use std::time::Duration;
///
/// #[derive(Default, Clone, Component)]
@@ -376,7 +420,7 @@ pub enum SwarmStartError {
/// }
///
/// async fn swarm_handle(
-/// mut swarm: Swarm<State>,
+/// mut swarm: Swarm,
/// event: SwarmEvent,
/// _state: SwarmState,
/// ) -> anyhow::Result<()> {
@@ -483,10 +527,15 @@ impl IntoIterator for Swarm {
/// Iterate over the bots in this swarm.
///
/// ```rust,no_run
+ /// # use azalea::{prelude::*, swarm::prelude::*};
+ /// #[derive(Component, Clone)]
+ /// # pub struct State;
+ /// # fn example(swarm: Swarm) {
/// for bot in swarm {
/// let state = bot.component::<State>();
/// // ...
/// }
+ /// # }
/// ```
fn into_iter(self) -> Self::IntoIter {
self.bots