diff options
| author | mat <git@matdoes.dev> | 2024-04-20 03:59:50 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2024-04-20 03:59:50 +0000 |
| commit | 6d9d1a456951ae321089343a91d15bfa9f3087d7 (patch) | |
| tree | e0b4d52930470c4e5a733886103a8eda4882a9dd /azalea-client/src | |
| parent | 353eda21ac8280213edcec3823cc3bd77fe17c44 (diff) | |
| download | azalea-drasl-6d9d1a456951ae321089343a91d15bfa9f3087d7.tar.xz | |
add Client::join_with_proxy and fix tests
Diffstat (limited to 'azalea-client/src')
| -rw-r--r-- | azalea-client/src/client.rs | 80 | ||||
| -rw-r--r-- | azalea-client/src/lib.rs | 3 |
2 files changed, 60 insertions, 23 deletions
diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 8ca0bbef..93852c75 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -138,6 +138,45 @@ pub enum JoinError { Disconnect { reason: FormattedText }, } +pub struct StartClientOpts<'a> { + pub ecs_lock: Arc<Mutex<World>>, + pub account: &'a Account, + pub address: &'a ServerAddress, + pub resolved_address: &'a SocketAddr, + pub proxy: Option<Proxy>, + pub run_schedule_sender: mpsc::UnboundedSender<()>, +} + +impl<'a> StartClientOpts<'a> { + pub fn new( + account: &'a Account, + address: &'a ServerAddress, + resolved_address: &'a SocketAddr, + ) -> StartClientOpts<'a> { + // An event that causes the schedule to run. This is only used internally. + let (run_schedule_sender, run_schedule_receiver) = mpsc::unbounded_channel(); + + let mut app = App::new(); + app.add_plugins(DefaultPlugins); + + let ecs_lock = start_ecs_runner(app, run_schedule_receiver, run_schedule_sender.clone()); + + Self { + ecs_lock, + account, + address, + resolved_address, + proxy: None, + run_schedule_sender, + } + } + + pub fn proxy(mut self, proxy: Proxy) -> Self { + self.proxy = Some(proxy); + self + } +} + impl Client { /// Create a new client from the given [`GameProfile`], ECS Entity, ECS /// World, and schedule runner function. @@ -183,39 +222,36 @@ impl Client { pub async fn join( account: &Account, address: impl TryInto<ServerAddress>, - proxy: Option<Proxy>, ) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> { let address: ServerAddress = address.try_into().map_err(|_| JoinError::InvalidAddress)?; let resolved_address = resolver::resolve_address(&address).await?; - // An event that causes the schedule to run. This is only used internally. - let (run_schedule_sender, run_schedule_receiver) = mpsc::unbounded_channel(); - - let mut app = App::new(); - app.add_plugins(DefaultPlugins); + Self::start_client(StartClientOpts::new(account, &address, &resolved_address)).await + } - let ecs_lock = start_ecs_runner(app, run_schedule_receiver, run_schedule_sender.clone()); + pub async fn join_with_proxy( + account: &Account, + address: impl TryInto<ServerAddress>, + proxy: Proxy, + ) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> { + let address: ServerAddress = address.try_into().map_err(|_| JoinError::InvalidAddress)?; + let resolved_address = resolver::resolve_address(&address).await?; - Self::start_client( - ecs_lock, - account, - &address, - &resolved_address, - proxy, - run_schedule_sender, - ) - .await + Self::start_client(StartClientOpts::new(account, &address, &resolved_address).proxy(proxy)) + .await } /// Create a [`Client`] when you already have the ECS made with /// [`start_ecs_runner`]. You'd usually want to use [`Self::join`] instead. pub async fn start_client( - ecs_lock: Arc<Mutex<World>>, - account: &Account, - address: &ServerAddress, - resolved_address: &SocketAddr, - proxy: Option<Proxy>, - run_schedule_sender: mpsc::UnboundedSender<()>, + StartClientOpts { + ecs_lock, + account, + address, + resolved_address, + proxy, + run_schedule_sender, + }: StartClientOpts<'_>, ) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> { // check if an entity with our uuid already exists in the ecs and if so then // just use that diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index 41399ce9..ec19e3ac 100644 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -32,7 +32,8 @@ pub mod task_pool; pub use account::{Account, AccountOpts}; pub use azalea_protocol::packets::configuration::serverbound_client_information_packet::ClientInformation; pub use client::{ - start_ecs_runner, Client, DefaultPlugins, JoinError, JoinedClientBundle, TickBroadcast, + start_ecs_runner, Client, DefaultPlugins, JoinError, JoinedClientBundle, StartClientOpts, + TickBroadcast, }; pub use events::Event; pub use local_player::{GameProfileComponent, InstanceHolder, TabList}; |
