azalea/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#![doc = include_str!("../README.md")]
#![allow(incomplete_features)]
#![feature(type_changing_struct_update)]
#![feature(let_chains)]
#![feature(never_type)]
pub mod accept_resource_packs;
pub mod auto_respawn;
pub mod auto_tool;
mod bot;
pub mod container;
pub mod nearest_entity;
pub mod pathfinder;
pub mod prelude;
pub mod swarm;
use std::net::SocketAddr;
use app::Plugins;
pub use azalea_auth as auth;
pub use azalea_block as blocks;
pub use azalea_brigadier as brigadier;
pub use azalea_buf as buf;
pub use azalea_chat::FormattedText;
pub use azalea_client::*;
pub use azalea_core as core;
// these are re-exported on this level because they're very common
pub use azalea_core::{
position::{BlockPos, Vec3},
resource_location::ResourceLocation,
};
pub use azalea_entity as entity;
pub use azalea_physics as physics;
pub use azalea_protocol as protocol;
pub use azalea_registry as registry;
pub use azalea_world as world;
pub use bevy_app as app;
pub use bevy_ecs as ecs;
pub use bot::*;
use ecs::component::Component;
use futures::{future::BoxFuture, Future};
use protocol::connect::Proxy;
use protocol::{resolver::ResolverError, ServerAddress};
use swarm::SwarmBuilder;
use thiserror::Error;
pub type BoxHandleFn<S> =
Box<dyn Fn(Client, azalea_client::Event, S) -> BoxFuture<'static, Result<(), anyhow::Error>>>;
pub type HandleFn<S, Fut> = fn(Client, azalea_client::Event, S) -> Fut;
#[derive(Error, Debug)]
pub enum StartError {
#[error("Invalid address")]
InvalidAddress,
#[error(transparent)]
ResolveAddress(#[from] ResolverError),
}
/// A builder for creating new [`Client`]s. This is the recommended way of
/// making Azalea bots.
///
/// ```no_run
/// # 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>
where
S: Default + Send + Sync + Clone + Component + 'static,
{
/// Internally, ClientBuilder is just a wrapper over SwarmBuilder since it's
/// technically just a subset of it so we can avoid duplicating code this
/// way.
swarm: SwarmBuilder<S, swarm::NoSwarmState>,
}
impl ClientBuilder<NoState> {
/// Start building a client that can join the world.
#[must_use]
pub fn new() -> ClientBuilder<NoState> {
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.
///
/// Note that you can also disable `LogPlugin` by disabling the `log`
/// feature.
///
/// You **must** add [`DefaultPlugins`] and [`DefaultBotPlugins`] to this.
///
/// ```
/// # use azalea::prelude::*;
/// use azalea::{app::PluginGroup, DefaultBotPlugins, DefaultPlugins};
/// use bevy_log::LogPlugin;
///
/// let client_builder = ClientBuilder::new_without_plugins()
/// .add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
/// .add_plugins(DefaultBotPlugins);
/// # 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 new_without_plugins() -> ClientBuilder<NoState> {
Self {
swarm: SwarmBuilder::new_without_plugins(),
}
}
/// Set the function that's called every time a bot receives an [`Event`].
/// This is the way to handle normal per-bot events.
///
/// Currently you can have up to one client handler.
///
/// ```
/// # 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<S, Fut>(self, handler: HandleFn<S, Fut>) -> ClientBuilder<S>
where
S: Default + Send + Sync + Clone + Component + 'static,
Fut: Future<Output = Result<(), anyhow::Error>> + Send + 'static,
{
ClientBuilder {
swarm: self.swarm.set_handler(handler),
}
}
}
impl<S> ClientBuilder<S>
where
S: Default + Send + Sync + Clone + Component + 'static,
{
/// Set the client state instead of initializing defaults.
#[must_use]
pub fn set_state(mut self, state: S) -> Self {
self.swarm.states = vec![state];
self
}
/// Add a group of plugins to the client.
#[must_use]
pub fn add_plugins<M>(mut self, plugins: impl Plugins<M>) -> Self {
self.swarm = self.swarm.add_plugins(plugins);
self
}
/// Build this `ClientBuilder` into an actual [`Client`] and join the given
/// server. If the client can't join, it'll keep retrying forever until it
/// can.
///
/// The `address` argument can be a `&str`, [`ServerAddress`], or anything
/// that implements `TryInto<ServerAddress>`.
///
/// # Errors
///
/// This will error if the given address is invalid or couldn't be resolved
/// to a Minecraft server.
///
/// [`ServerAddress`]: azalea_protocol::ServerAddress
pub async fn start(
mut self,
account: Account,
address: impl TryInto<ServerAddress>,
) -> Result<!, StartError> {
self.swarm.accounts = vec![(account, JoinOpts::default())];
if self.swarm.states.is_empty() {
self.swarm.states = vec![S::default()];
}
self.swarm.start(address).await
}
/// Do the same as [`Self::start`], but allow passing in custom join
/// options.
pub async fn start_with_opts(
mut self,
account: Account,
address: impl TryInto<ServerAddress>,
opts: JoinOpts,
) -> Result<!, StartError> {
self.swarm.accounts = vec![(account, opts.clone())];
if self.swarm.states.is_empty() {
self.swarm.states = vec![S::default()];
}
self.swarm.start_with_default_opts(address, opts).await
}
}
impl Default for ClientBuilder<NoState> {
fn default() -> Self {
Self::new()
}
}
/// A marker that can be used in place of a State in [`ClientBuilder`] or
/// [`SwarmBuilder`]. You probably don't need to use this manually since the
/// compiler will infer it for you.
///
/// [`SwarmBuilder`]: swarm::SwarmBuilder
#[derive(Component, Clone, Default)]
pub struct NoState;
/// Optional settings when adding an account to a swarm or client.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct JoinOpts {
/// The Socks5 proxy that this bot will use.
pub proxy: Option<Proxy>,
/// Override the server address that this specific bot will send in the
/// handshake packet.
pub custom_address: Option<ServerAddress>,
/// Override the socket address that this specific bot will use to connect
/// to the server.
pub custom_resolved_address: Option<SocketAddr>,
}
impl JoinOpts {
pub fn new() -> Self {
Self::default()
}
pub fn update(&mut self, other: &Self) {
if let Some(proxy) = other.proxy.clone() {
self.proxy = Some(proxy);
}
if let Some(custom_address) = other.custom_address.clone() {
self.custom_address = Some(custom_address);
}
if let Some(custom_resolved_address) = other.custom_resolved_address {
self.custom_resolved_address = Some(custom_resolved_address);
}
}
/// Set the proxy that this bot will use.
#[must_use]
pub fn proxy(mut self, proxy: Proxy) -> Self {
self.proxy = Some(proxy);
self
}
/// Set the custom address that this bot will send in the handshake packet.
#[must_use]
pub fn custom_address(mut self, custom_address: ServerAddress) -> Self {
self.custom_address = Some(custom_address);
self
}
/// Set the custom resolved address that this bot will use to connect to the
/// server.
#[must_use]
pub fn custom_resolved_address(mut self, custom_resolved_address: SocketAddr) -> Self {
self.custom_resolved_address = Some(custom_resolved_address);
self
}
}