aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'azalea/src/lib.rs')
-rw-r--r--[-rwxr-xr-x]azalea/src/lib.rs149
1 files changed, 8 insertions, 141 deletions
diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs
index 89754409..7c9c660b 100755..100644
--- a/azalea/src/lib.rs
+++ b/azalea/src/lib.rs
@@ -75,152 +75,19 @@
//!
//! [`azalea_client`]: https://crates.io/crates/azalea-client
+#![feature(trait_upcasting)]
+#![feature(async_closure)]
+#![allow(incomplete_features)]
+
mod bot;
pub mod pathfinder;
pub mod prelude;
+mod start;
+mod swarm;
pub use azalea_client::*;
pub use azalea_core::{BlockPos, Vec3};
-use azalea_protocol::ServerAddress;
-use std::{future::Future, sync::Arc};
-use thiserror::Error;
+pub use start::{start, Options};
+pub use swarm::*;
pub type HandleFn<Fut, S> = fn(Client, Event, S) -> Fut;
-
-/// The options that are passed to [`azalea::start`].
-///
-/// [`azalea::start`]: fn.start.html
-pub struct Options<S, A, Fut>
-where
- A: TryInto<ServerAddress>,
- Fut: Future<Output = Result<(), anyhow::Error>>,
-{
- /// The address of the server that we're connecting to. This can be a
- /// `&str`, [`ServerAddress`], or anything that implements
- /// `TryInto<ServerAddress>`.
- ///
- /// [`ServerAddress`]: azalea_protocol::ServerAddress
- pub address: A,
- /// The account that's going to join the server.
- pub account: Account,
- /// The plugins that are going to be used. Plugins are external crates that
- /// add extra functionality to Azalea. You should use the [`plugins`] macro
- /// for this field.
- ///
- /// ```rust,no_run
- /// plugins![azalea_pathfinder::Plugin::default()]
- /// ```
- pub plugins: Plugins,
- /// A struct that contains the data that you want your bot to remember
- /// across events.
- ///
- /// # Examples
- ///
- /// ```rust
- /// use parking_lot::Mutex;
- /// use std::sync::Arc;
- ///
- /// #[derive(Default, Clone)]
- /// struct State {
- /// farming: Arc<Mutex<bool>>,
- /// }
- /// ```
- pub state: S,
- /// The function that's called whenever we get an event.
- ///
- /// # Examples
- ///
- /// ```rust
- /// use azalea::prelude::*;
- ///
- /// async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
- /// Ok(())
- /// }
- /// ```
- pub handle: HandleFn<Fut, S>,
-}
-
-#[derive(Error, Debug)]
-pub enum Error {
- #[error("Invalid address")]
- InvalidAddress,
- #[error("Join error: {0}")]
- Join(#[from] azalea_client::JoinError),
-}
-
-/// Join a server and start handling events. This function will run forever until
-/// it gets disconnected from the server.
-///
-/// # Examples
-///
-/// ```rust,no_run
-/// let error = azalea::start(azalea::Options {
-/// account,
-/// address: "localhost",
-/// state: State::default(),
-/// plugins: plugins![azalea_pathfinder::Plugin::default()],
-/// handle,
-/// }).await;
-/// ```
-pub async fn start<
- S: Send + Sync + Clone + 'static,
- A: Send + TryInto<ServerAddress>,
- Fut: Future<Output = Result<(), anyhow::Error>> + Send + 'static,
->(
- options: Options<S, A, Fut>,
-) -> Result<(), Error> {
- let address = match options.address.try_into() {
- Ok(address) => address,
- Err(_) => return Err(Error::InvalidAddress),
- };
-
- let (mut bot, mut rx) = Client::join(&options.account, address).await?;
-
- let mut plugins = options.plugins;
- plugins.add(bot::Plugin::default());
- plugins.add(pathfinder::Plugin::default());
- bot.plugins = Arc::new(plugins);
-
- let state = options.state;
-
- while let Some(event) = rx.recv().await {
- let cloned_plugins = (*bot.plugins).clone();
- for plugin in cloned_plugins.into_iter() {
- tokio::spawn(plugin.handle(event.clone(), bot.clone()));
- }
-
- tokio::spawn(bot::Plugin::handle(
- Box::new(bot.plugins.get::<bot::Plugin>().unwrap().clone()),
- event.clone(),
- bot.clone(),
- ));
- tokio::spawn(pathfinder::Plugin::handle(
- Box::new(bot.plugins.get::<pathfinder::Plugin>().unwrap().clone()),
- event.clone(),
- bot.clone(),
- ));
-
- tokio::spawn((options.handle)(bot.clone(), event.clone(), state.clone()));
- }
-
- Ok(())
-}
-
-/// A helper macro that generates a [`Plugins`] struct from a list of objects
-/// that implement [`Plugin`].
-///
-/// ```rust,no_run
-/// plugins![azalea_pathfinder::Plugin::default()];
-/// ```
-#[macro_export]
-macro_rules! plugins {
- ($($plugin:expr),*) => {
- {
- let mut plugins = azalea::Plugins::new();
- $(
- plugins.add($plugin);
- )*
- plugins
- }
- };
-}