Struct SwarmBuilder

Source
pub struct SwarmBuilder<S, SS, R, SR>
where S: Send + Sync + Clone + Component + 'static, SS: Default + Send + Sync + Clone + Resource + 'static, Self: Send,
{ /* private fields */ }
Expand description

Create a new Swarm.

The generics of this struct stand for the following:

  • S: State
  • SS: Swarm State
  • R: Return type of the handler
  • SR: Return type of the swarm handler

You shouldn’t have to manually set them though, they’ll be inferred for you.

Implementations§

Source§

impl SwarmBuilder<NoState, NoSwarmState, (), ()>

Source

pub fn new() -> Self

Start creating the swarm.

Source

pub fn new_without_plugins() -> Self

Self::new but without adding the plugins by default.

This is useful if you want to disable a default plugin. This also exists for ClientBuilder, see ClientBuilder::new_without_plugins.

You must add DefaultPlugins, DefaultBotPlugins, and DefaultSwarmPlugins to this.

use azalea::app::PluginGroup;

let swarm_builder = SwarmBuilder::new_without_plugins()
    .add_plugins(azalea::DefaultPlugins.build().disable::<azalea::chat_signing::ChatSigningPlugin>())
    .add_plugins(azalea::DefaultBotPlugins)
    .add_plugins(azalea::swarm::DefaultSwarmPlugins);
Source§

impl<SS, SR> SwarmBuilder<NoState, SS, (), SR>
where SS: Default + Send + Sync + Clone + Resource + 'static,

Source

pub fn set_handler<S, Fut, R>( self, handler: HandleFn<S, Fut>, ) -> SwarmBuilder<S, SS, R, SR>
where Fut: Future<Output = R> + Send + 'static, S: Send + Sync + Clone + Component + Default + 'static,

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 handler.

Note that if you’re creating clients directly from the ECS using StartJoinServerEvent and the client wasn’t already in the ECS, then the handler function won’t be called for that client. This also applies to SwarmBuilder::set_swarm_handler. This shouldn’t be a concern for most bots, though.

swarm_builder.set_handler(handle);

#[derive(Component, Default, Clone)]
struct State {}
async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> {
    Ok(())
}
Source§

impl<S, R> SwarmBuilder<S, NoSwarmState, R, ()>
where S: Send + Sync + Clone + Component + 'static,

Source

pub fn set_swarm_handler<SS, Fut, SR>( self, handler: SwarmHandleFn<SS, Fut>, ) -> SwarmBuilder<S, SS, R, SR>
where SS: Default + Send + Sync + Clone + Resource + 'static, Fut: Future<Output = SR> + Send + 'static,

Set the function that’s called every time the swarm receives a SwarmEvent. This is the way to handle global swarm events.

Currently you can have up to one swarm handler.

Note that if you’re creating clients directly from the ECS using StartJoinServerEvent and the client wasn’t already in the ECS, then this handler function won’t be called for that client. This also applies to SwarmBuilder::set_handler. This shouldn’t be a concern for most bots, though.

swarm_builder.set_swarm_handler(swarm_handle);



#[derive(Resource, Default, Clone)]
struct SwarmState {}
async fn swarm_handle(
    mut swarm: Swarm,
    event: SwarmEvent,
    state: SwarmState,
) -> anyhow::Result<()> {
    Ok(())
}
Source§

impl<S, SS, R, SR> SwarmBuilder<S, SS, R, SR>
where S: Send + Sync + Clone + Component + 'static, SS: Default + Send + Sync + Clone + Resource + 'static, R: Send + 'static, SR: Send + 'static,

Source

pub fn add_accounts(self, accounts: Vec<Account>) -> Self
where S: Default,

Add a vec of Accounts to the swarm.

Use Self::add_account to only add one account. If you want the clients to have different default states, add them one at a time with Self::add_account_with_state.

By default, every account will join at the same time, you can add a delay with Self::join_delay.

Source

pub fn add_account(self, account: Account) -> Self
where S: Default,

Add a single new Account to the swarm. Use Self::add_accounts to add multiple accounts at a time.

This will make the state for this client be the default, use Self::add_account_with_state to avoid that.

Source

pub fn add_account_with_state(self, account: Account, state: S) -> Self

Add an account with a custom initial state. Use just Self::add_account to use the Default implementation for the state.

Source

pub fn add_account_with_opts(self, account: Account, opts: JoinOpts) -> Self
where S: Default,

Add an account with a custom initial state. Use just Self::add_account to use the Default implementation for the state.

Source

pub fn add_account_with_state_and_opts( self, account: Account, state: S, join_opts: JoinOpts, ) -> Self

Same as Self::add_account_with_state, but allow passing in custom join options.

Source

pub fn set_swarm_state(self, swarm_state: SS) -> Self

Set the swarm state instead of initializing defaults.

Source

pub fn add_plugins<M>(self, plugins: impl Plugins<M>) -> Self

Add one or more plugins to this swarm.

See Self::new_without_plugins to learn how to disable default plugins.

Source

pub fn join_delay(self, delay: Duration) -> Self

Set how long we should wait between each bot joining the server.

By default, every bot will connect at the same time. If you set this field, however, the bots will wait for the previous one to have connected and then they’ll wait the given duration.

Source

pub fn reconnect_after(self, delay: impl Into<Option<Duration>>) -> Self

Configures the auto-reconnection behavior for our bots.

If this is Some, then it’ll set the default reconnection delay for our bots (how long they’ll wait after being kicked before they try rejoining). if it’s None, then auto-reconnecting will be disabled.

If this function isn’t called, then our clients will reconnect after DEFAULT_RECONNECT_DELAY.

Source

pub async fn start( self, address: impl TryInto<ServerAddress>, ) -> Result<!, StartError>

Build this SwarmBuilder into an actual Swarm and join the given server.

The address argument can be a &str, ServerAddress, or anything that implements TryInto<ServerAddress>.

Source

pub async fn start_with_default_opts( self, address: impl TryInto<ServerAddress>, default_join_opts: JoinOpts, ) -> Result<!, StartError>

Do the same as Self::start, but allow passing in default join options for the bots.

Trait Implementations§

Source§

impl Default for SwarmBuilder<NoState, NoSwarmState, (), ()>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S, SS, R, SR> !Freeze for SwarmBuilder<S, SS, R, SR>

§

impl<S, SS, R, SR> !RefUnwindSafe for SwarmBuilder<S, SS, R, SR>

§

impl<S, SS, R, SR> Send for SwarmBuilder<S, SS, R, SR>

§

impl<S, SS, R, SR> !Sync for SwarmBuilder<S, SS, R, SR>

§

impl<S, SS, R, SR> Unpin for SwarmBuilder<S, SS, R, SR>
where SS: Unpin, S: Unpin,

§

impl<S, SS, R, SR> !UnwindSafe for SwarmBuilder<S, SS, R, SR>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromWorld for T
where T: Default,

§

fn from_world(_world: &mut World) -> T

Creates Self using default().

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ConditionalSend for T
where T: Send,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T