From 4fde1ecd61c0f67057df0c55a45ba398da580ae1 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 9 Dec 2025 04:21:15 +0300 Subject: split open_container_at to open_container_at_with_timeout_ticks to have a cleaner api interface --- CHANGELOG.md | 2 +- azalea/src/container.rs | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a17fda99..cfc4f3a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ is breaking anyways, semantic versioning is not followed. - `Client::query`, `map_component`, and `map_get_component` were replaced by `Client::query_self`. - Rename `SendPacketEvent` to `SendGamePacketEvent` and `PingEvent` to `GamePingEvent`. - Swap the order of the type parameters in entity filtering functions so query is first, then filter. -- Add optional `timeout_ticks` field to `Client::open_container_at`. +- Add `Client::open_container_at_with_timeout_ticks`, and `Client::open_container_at` now times out after 5 seconds. - Rename `ResourceLocation` to `Identifier` to match Minecraft's new internal naming. - Rename `azalea_protocol::resolver` to `resolve` and `ResolverError` to `ResolveError`. diff --git a/azalea/src/container.rs b/azalea/src/container.rs index 57d0b369..dc85ba96 100644 --- a/azalea/src/container.rs +++ b/azalea/src/container.rs @@ -31,35 +31,50 @@ pub trait ContainerClientExt { /// /// Use [`Client::open_inventory`] to open your own inventory. /// - /// `timeout_ticks` indicates how long the client will wait before giving - /// up and returning `None`. You may need to adjust it based on latency. - /// Setting the timeout to `None` will result in waiting potentially - /// forever. + /// This function times out after 5 seconds (100 ticks). Use + /// [`Self::open_container_at_with_timeout_ticks`] if you would like to + /// configure this. /// /// ``` - /// # use azalea::prelude::*; + /// # use azalea::{prelude::*, azalea::registry::Block}; /// # async fn example(mut bot: azalea::Client) { /// let target_pos = bot /// .world() /// .read() - /// .find_block(bot.position(), &azalea::registry::Block::Chest.into()); + /// .find_block(bot.position(), &Block::Chest.into()); /// let Some(target_pos) = target_pos else { /// bot.chat("no chest found"); /// return; /// }; - /// let container = bot.open_container_at(target_pos, None).await; + /// let container = bot.open_container_at(target_pos).await; /// # } /// ``` fn open_container_at( &self, pos: BlockPos, + ) -> impl Future> + Send; + + /// Open a container in the world, or time out after a specified amount of + /// ticks. + /// + /// See [`Self::open_container_at`] for more information. That function + /// defaults to a timeout of 5 seconds (100 ticks), which is usually good + /// enough. However to detect failures faster or to account for server + /// lag, you may find it useful to adjust the timeout to a different + /// value. + /// + /// The timeout is measured in game ticks (on the client, not the server), + /// i.e. 1/20th of a second. + fn open_container_at_with_timeout_ticks( + &self, + pos: BlockPos, timeout_ticks: Option, ) -> impl Future> + Send; /// Wait until a container is open, up to the specified number of ticks. /// /// Returns `None` if the container was immediately opened and closed, or if - /// the timeout expires. + /// the timeout expired. /// /// If `timeout_ticks` is None, there will be no timeout. fn wait_for_container_open( @@ -97,7 +112,12 @@ pub trait ContainerClientExt { } impl ContainerClientExt for Client { - async fn open_container_at( + async fn open_container_at(&self, pos: BlockPos) -> Option { + self.open_container_at_with_timeout_ticks(pos, Some(20 * 5)) + .await + } + + async fn open_container_at_with_timeout_ticks( &self, pos: BlockPos, timeout_ticks: Option, -- cgit v1.2.3