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, (), ()>
impl SwarmBuilder<NoState, NoSwarmState, (), ()>
Sourcepub fn new_without_plugins() -> Self
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>
impl<SS, SR> SwarmBuilder<NoState, SS, (), SR>
Sourcepub fn set_handler<S, Fut, R>(
self,
handler: HandleFn<S, Fut>,
) -> SwarmBuilder<S, SS, R, SR>
pub fn set_handler<S, Fut, R>( self, handler: HandleFn<S, Fut>, ) -> SwarmBuilder<S, SS, R, SR>
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, ()>
impl<S, R> SwarmBuilder<S, NoSwarmState, R, ()>
Sourcepub fn set_swarm_handler<SS, Fut, SR>(
self,
handler: SwarmHandleFn<SS, Fut>,
) -> SwarmBuilder<S, SS, R, SR>
pub fn set_swarm_handler<SS, Fut, SR>( self, handler: SwarmHandleFn<SS, Fut>, ) -> SwarmBuilder<S, SS, R, SR>
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>
impl<S, SS, R, SR> SwarmBuilder<S, SS, R, SR>
Sourcepub fn add_accounts(self, accounts: Vec<Account>) -> Selfwhere
S: Default,
pub fn add_accounts(self, accounts: Vec<Account>) -> Selfwhere
S: Default,
Add a vec of Account
s 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
.
Sourcepub fn add_account(self, account: Account) -> Selfwhere
S: Default,
pub fn add_account(self, account: Account) -> Selfwhere
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.
Sourcepub fn add_account_with_state(self, account: Account, state: S) -> Self
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.
Sourcepub fn add_account_with_opts(self, account: Account, opts: JoinOpts) -> Selfwhere
S: Default,
pub fn add_account_with_opts(self, account: Account, opts: JoinOpts) -> Selfwhere
S: Default,
Add an account with a custom initial state. Use just
Self::add_account
to use the Default implementation for the state.
Sourcepub fn add_account_with_state_and_opts(
self,
account: Account,
state: S,
join_opts: JoinOpts,
) -> Self
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.
Sourcepub fn set_swarm_state(self, swarm_state: SS) -> Self
pub fn set_swarm_state(self, swarm_state: SS) -> Self
Set the swarm state instead of initializing defaults.
Sourcepub fn add_plugins<M>(self, plugins: impl Plugins<M>) -> Self
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.
Sourcepub fn join_delay(self, delay: Duration) -> Self
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.
Sourcepub fn reconnect_after(self, delay: impl Into<Option<Duration>>) -> Self
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
.
Sourcepub async fn start(
self,
address: impl TryInto<ServerAddress>,
) -> Result<!, StartError>
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>
.
Sourcepub async fn start_with_default_opts(
self,
address: impl TryInto<ServerAddress>,
default_join_opts: JoinOpts,
) -> Result<!, StartError>
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§
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>
impl<S, SS, R, SR> !UnwindSafe for SwarmBuilder<S, SS, R, SR>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSend for T
§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using default()
.