From c9b1b19ff235975a80191d512392460c0eabfad4 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 23 Oct 2022 16:57:28 -0500 Subject: [PATCH] Update examples with new cleaner handle/state --- azalea-client/src/chat.rs | 5 +++-- azalea/examples/mine_a_chunk.rs | 16 ++++------------ azalea/examples/potatobot/main.rs | 4 +--- azalea/examples/pvp.rs | 14 ++++++-------- azalea/src/lib.rs | 16 ++++++++-------- 5 files changed, 22 insertions(+), 33 deletions(-) diff --git a/azalea-client/src/chat.rs b/azalea-client/src/chat.rs index fecd76ae..335261e6 100644 --- a/azalea-client/src/chat.rs +++ b/azalea-client/src/chat.rs @@ -69,15 +69,16 @@ impl Client { /// # azalea::start(azalea::Options { /// # account, /// # address: "localhost", - /// # state: Arc::new(Mutex::new(State::default())), + /// # state: State::default(), /// # plugins: vec![], /// # handle, /// # }) /// # .await /// # .unwrap(); /// # } + /// # #[derive(Default, Clone)] /// # pub struct State {} - /// # async fn handle(bot: Client, event: Arc, state: Arc>) -> anyhow::Result<()> { + /// # async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> { /// bot.chat("Hello, world!").await.unwrap(); /// # Ok(()) /// # } diff --git a/azalea/examples/mine_a_chunk.rs b/azalea/examples/mine_a_chunk.rs index 5f1dabe1..3bb712fa 100644 --- a/azalea/examples/mine_a_chunk.rs +++ b/azalea/examples/mine_a_chunk.rs @@ -14,7 +14,7 @@ async fn main() { accounts, address: "localhost", - swarm_state: Arc::new(Mutex::new(State::default())), + swarm_state: State::default(), state: State::default(), swarm_plugins: vec![Arc::new(pathfinder::Plugin::default())], @@ -33,20 +33,12 @@ struct State {} #[derive(Default, Clone)] struct SwarmState {} -async fn handle(bot: Client, event: Arc, state: Arc>) -> anyhow::Result<()> { - match event { - _ => {} - } - +async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> { Ok(()) } -async fn swarm_handle( - swarm: Swarm, - event: Arc, - state: Arc>, -) -> anyhow::Result<()> { - match *event { +async fn swarm_handle(swarm: Swarm, event: Event, state: SwarmState) -> anyhow::Result<()> { + match event { Event::Login => { swarm.goto(azalea::BlockPos::new(0, 70, 0)).await; // or bots.goto_goal(pathfinder::Goals::Goto(azalea::BlockPos(0, 70, 0))).await; diff --git a/azalea/examples/potatobot/main.rs b/azalea/examples/potatobot/main.rs index 5398c68a..1aa28ecc 100644 --- a/azalea/examples/potatobot/main.rs +++ b/azalea/examples/potatobot/main.rs @@ -2,10 +2,8 @@ mod autoeat; use azalea::prelude::*; use azalea::{pathfinder, Account, BlockPos, Client, Event, ItemKind, MoveDirection, Plugin, Vec3}; -use parking_lot::Mutex; -use std::sync::Arc; -#[derive(Default)] +#[derive(Default, Clone)] struct State {} #[tokio::main] diff --git a/azalea/examples/pvp.rs b/azalea/examples/pvp.rs index 8c133576..ec2a08f0 100644 --- a/azalea/examples/pvp.rs +++ b/azalea/examples/pvp.rs @@ -1,23 +1,21 @@ -use azalea::{pathfinder, Account, Accounts, Client, Event}; -use parking_lot::Mutex; -use std::sync::Arc; +use azalea::{pathfinder, Account, Client, Event}; #[tokio::main] async fn main() { - let accounts = Accounts::new(); + let accounts = Vec::new(); for i in 0..10 { - accounts.add(Account::offline(&format!("bot{}", i))); + accounts.push(Account::offline(&format!("bot{}", i))); } azalea::start_swarm(azalea::SwarmOptions { accounts, address: "localhost", - swarm_state: Arc::new(Mutex::new(State::default())), + swarm_state: State::default(), state: State::default(), - swarm_plugins: vec![Arc::new(pathfinder::Plugin::default())], + swarm_plugins: vec![Box::new(pathfinder::Plugin::default())], plugins: vec![], handle: Box::new(handle), @@ -48,7 +46,7 @@ async fn swarm_handle(swarm: Swarm, event: Event, state: State) { if bot.entity.can_reach(target.bounding_box) { bot.swing(); } - if !h.using_held_item() && bot.state.lock().hunger <= 17 { + if !bot.using_held_item() && bot.state.lock().hunger <= 17 { bot.hold(azalea::ItemGroup::Food); tokio::task::spawn(bot.use_held_item()); } diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 6a000e6d..148dfca7 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -22,7 +22,7 @@ //! azalea::start(azalea::Options { //! account, //! address: "localhost", -//! state: Arc::new(Mutex::new(State::default())), +//! state: State::default(), //! plugins: vec![], //! handle, //! }) @@ -33,8 +33,8 @@ //! #[derive(Default, Clone)] //! pub struct State {} //! -//! async fn handle(bot: Client, event: Arc, state: Arc>) -> anyhow::Result<()> { -//! match *event { +//! async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> { +//! match event { //! Event::Chat(m) => { //! println!(m.message().to_ansi(None)); //! } @@ -129,13 +129,13 @@ pub enum Error { /// it gets disconnected from the server. /// /// ```rust,no_run -/// azalea::start(azalea::Options { +/// let error = azalea::start(azalea::Options { /// account, /// address: "localhost", -/// state: Arc::new(Mutex::new(State::default())), -/// plugins: vec![&autoeat::Plugin::default()], -/// handle: Box::new(handle), -/// }).await.unwrap(); +/// state: State::default(), +/// plugins: vec![Box::new(autoeat::Plugin::default())], +/// handle, +/// }).await; /// ``` pub async fn start< S: Send + Sync + Clone + 'static,