1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00

fix some doc tests

This commit is contained in:
mat 2023-03-14 19:05:04 -05:00
commit 12c1558c17
4 changed files with 29 additions and 33 deletions

View file

@ -107,10 +107,11 @@ pub type ClientInformation = ServerboundClientInformationPacket;
/// tab list. /// tab list.
/// ///
/// ``` /// ```
/// # use azalea_client::TabList;
/// # fn example(client: &azalea_client::Client) { /// # fn example(client: &azalea_client::Client) {
/// let tab_list = client.component::<TabList>(); /// let tab_list = client.component::<TabList>();
/// println!("Online players:"); /// println!("Online players:");
/// for (uuid, player_info) in tab_list { /// for (uuid, player_info) in tab_list.iter() {
/// println!("- {} ({}ms)", player_info.profile.name, player_info.latency); /// println!("- {} ({}ms)", player_info.profile.name, player_info.latency);
/// } /// }
/// # } /// # }
@ -636,7 +637,9 @@ pub async fn tick_run_schedule_loop(run_schedule_sender: mpsc::UnboundedSender<(
/// ///
/// This is useful for running code every schedule from async user code. /// This is useful for running code every schedule from async user code.
/// ///
/// ```no_run /// ```
/// use azalea_client::TickBroadcast;
/// # async fn example(client: azalea_client::Client) {
/// let mut receiver = { /// let mut receiver = {
/// let ecs = client.ecs.lock(); /// let ecs = client.ecs.lock();
/// let tick_broadcast = ecs.resource::<TickBroadcast>(); /// let tick_broadcast = ecs.resource::<TickBroadcast>();
@ -645,6 +648,7 @@ pub async fn tick_run_schedule_loop(run_schedule_sender: mpsc::UnboundedSender<(
/// while receiver.recv().await.is_ok() { /// while receiver.recv().await.is_ok() {
/// // do something /// // do something
/// } /// }
/// # }
/// ``` /// ```
#[derive(Resource, Deref)] #[derive(Resource, Deref)]
pub struct TickBroadcast(broadcast::Sender<()>); pub struct TickBroadcast(broadcast::Sender<()>);

View file

@ -38,6 +38,21 @@ impl Plugin for InventoryPlugin {
pub struct WaitingForInventoryOpen; pub struct WaitingForInventoryOpen;
impl Client { impl Client {
/// Open a container in the world, like a chest.
///
/// ```
/// # async fn example(mut bot: azalea_client::Client) {
/// let target_pos = bot
/// .world()
/// .read()
/// .find_block(bot.position(), &azalea_registry::Block::Chest.into());
/// let Some(target_pos) = target_pos else {
/// bot.chat("no chest found");
/// return;
/// };
/// let container = bot.open_container(target_pos).await;
/// # }
/// ```
pub async fn open_container(&mut self, pos: BlockPos) -> Option<ContainerHandle> { pub async fn open_container(&mut self, pos: BlockPos) -> Option<ContainerHandle> {
self.ecs self.ecs
.lock() .lock()

View file

@ -30,6 +30,7 @@ pub mod task_pool;
pub use account::Account; pub use account::Account;
pub use client::{ pub use client::{
init_ecs_app, start_ecs, Client, ClientInformation, JoinError, JoinedClientBundle, TabList, init_ecs_app, start_ecs, Client, ClientInformation, JoinError, JoinedClientBundle, TabList,
TickBroadcast,
}; };
pub use events::Event; pub use events::Event;
pub use local_player::{GameProfileComponent, LocalPlayer}; pub use local_player::{GameProfileComponent, LocalPlayer};

View file

@ -1,7 +1,6 @@
//! Take wool from a chest and put one random piece of wool in every inventory //! Take the items in a container and put one of each in a checkerboard pattern
//! slot
use azalea::{entity::Position, prelude::*}; use azalea::{pathfinder::BlockPosGoal, prelude::*};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
@ -22,10 +21,10 @@ struct State {
pub started: Arc<Mutex<bool>>, pub started: Arc<Mutex<bool>>,
} }
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> { async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> {
match event { match event {
Event::Chat(m) => { Event::Chat(m) => {
if m.username() == Some(bot.profile.name) { if m.username() == Some(bot.profile.name.clone()) {
return Ok(()); return Ok(());
}; };
if m.content() != "go" { if m.content() != "go" {
@ -47,36 +46,13 @@ async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
bot.chat("No chest found"); bot.chat("No chest found");
return Ok(()); return Ok(());
}; };
bot.goto(chest_block.into()); bot.goto(BlockPosGoal::from(chest_block));
let Some(chest) = bot.open_container(&chest_block).await else { let Some(chest) = bot.open_container(chest_block).await else {
println!("Couldn't open chest"); println!("Couldn't open chest");
return Ok(()); return Ok(());
}; };
bot.take_amount_from_container(&chest, 5, |i| i.id == "#minecraft:planks")
.await;
chest.close().await;
let crafting_table = bot // move everything into our inventory first
.open_crafting_table(
&bot.world
.find_one_block(|b| b.id == "minecraft:crafting_table"),
)
.await
.unwrap();
bot.craft(&crafting_table, &bot.recipe_for("minecraft:sticks"))
.await?;
let pickaxe = bot
.craft(&crafting_table, &bot.recipe_for("minecraft:wooden_pickaxe"))
.await?;
crafting_table.close().await;
bot.hold(&pickaxe);
loop {
if let Err(e) = bot.dig(bot.position().down(1.).into()).await {
println!("{:?}", e);
break;
}
}
} }
_ => {} _ => {}
} }