1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

Update examples with new cleaner handle/state

This commit is contained in:
mat 2022-10-23 16:57:28 -05:00
parent 2eade86cf7
commit c9b1b19ff2
5 changed files with 22 additions and 33 deletions

View file

@ -69,15 +69,16 @@ impl Client {
/// # azalea::start(azalea::Options { /// # azalea::start(azalea::Options {
/// # account, /// # account,
/// # address: "localhost", /// # address: "localhost",
/// # state: Arc::new(Mutex::new(State::default())), /// # state: State::default(),
/// # plugins: vec![], /// # plugins: vec![],
/// # handle, /// # handle,
/// # }) /// # })
/// # .await /// # .await
/// # .unwrap(); /// # .unwrap();
/// # } /// # }
/// # #[derive(Default, Clone)]
/// # pub struct State {} /// # pub struct State {}
/// # async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) -> anyhow::Result<()> { /// # async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
/// bot.chat("Hello, world!").await.unwrap(); /// bot.chat("Hello, world!").await.unwrap();
/// # Ok(()) /// # Ok(())
/// # } /// # }

View file

@ -14,7 +14,7 @@ async fn main() {
accounts, accounts,
address: "localhost", address: "localhost",
swarm_state: Arc::new(Mutex::new(State::default())), swarm_state: State::default(),
state: State::default(), state: State::default(),
swarm_plugins: vec![Arc::new(pathfinder::Plugin::default())], swarm_plugins: vec![Arc::new(pathfinder::Plugin::default())],
@ -33,20 +33,12 @@ struct State {}
#[derive(Default, Clone)] #[derive(Default, Clone)]
struct SwarmState {} struct SwarmState {}
async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) -> anyhow::Result<()> { async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
match event {
_ => {}
}
Ok(()) Ok(())
} }
async fn swarm_handle( async fn swarm_handle(swarm: Swarm, event: Event, state: SwarmState) -> anyhow::Result<()> {
swarm: Swarm, match event {
event: Arc<Event>,
state: Arc<Mutex<SwarmState>>,
) -> anyhow::Result<()> {
match *event {
Event::Login => { Event::Login => {
swarm.goto(azalea::BlockPos::new(0, 70, 0)).await; swarm.goto(azalea::BlockPos::new(0, 70, 0)).await;
// or bots.goto_goal(pathfinder::Goals::Goto(azalea::BlockPos(0, 70, 0))).await; // or bots.goto_goal(pathfinder::Goals::Goto(azalea::BlockPos(0, 70, 0))).await;

View file

@ -2,10 +2,8 @@ mod autoeat;
use azalea::prelude::*; use azalea::prelude::*;
use azalea::{pathfinder, Account, BlockPos, Client, Event, ItemKind, MoveDirection, Plugin, Vec3}; 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 {} struct State {}
#[tokio::main] #[tokio::main]

View file

@ -1,23 +1,21 @@
use azalea::{pathfinder, Account, Accounts, Client, Event}; use azalea::{pathfinder, Account, Client, Event};
use parking_lot::Mutex;
use std::sync::Arc;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let accounts = Accounts::new(); let accounts = Vec::new();
for i in 0..10 { for i in 0..10 {
accounts.add(Account::offline(&format!("bot{}", i))); accounts.push(Account::offline(&format!("bot{}", i)));
} }
azalea::start_swarm(azalea::SwarmOptions { azalea::start_swarm(azalea::SwarmOptions {
accounts, accounts,
address: "localhost", address: "localhost",
swarm_state: Arc::new(Mutex::new(State::default())), swarm_state: State::default(),
state: State::default(), state: State::default(),
swarm_plugins: vec![Arc::new(pathfinder::Plugin::default())], swarm_plugins: vec![Box::new(pathfinder::Plugin::default())],
plugins: vec![], plugins: vec![],
handle: Box::new(handle), 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) { if bot.entity.can_reach(target.bounding_box) {
bot.swing(); 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); bot.hold(azalea::ItemGroup::Food);
tokio::task::spawn(bot.use_held_item()); tokio::task::spawn(bot.use_held_item());
} }

View file

@ -22,7 +22,7 @@
//! azalea::start(azalea::Options { //! azalea::start(azalea::Options {
//! account, //! account,
//! address: "localhost", //! address: "localhost",
//! state: Arc::new(Mutex::new(State::default())), //! state: State::default(),
//! plugins: vec![], //! plugins: vec![],
//! handle, //! handle,
//! }) //! })
@ -33,8 +33,8 @@
//! #[derive(Default, Clone)] //! #[derive(Default, Clone)]
//! pub struct State {} //! pub struct State {}
//! //!
//! async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) -> anyhow::Result<()> { //! async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
//! match *event { //! match event {
//! Event::Chat(m) => { //! Event::Chat(m) => {
//! println!(m.message().to_ansi(None)); //! println!(m.message().to_ansi(None));
//! } //! }
@ -129,13 +129,13 @@ pub enum Error {
/// it gets disconnected from the server. /// it gets disconnected from the server.
/// ///
/// ```rust,no_run /// ```rust,no_run
/// azalea::start(azalea::Options { /// let error = azalea::start(azalea::Options {
/// account, /// account,
/// address: "localhost", /// address: "localhost",
/// state: Arc::new(Mutex::new(State::default())), /// state: State::default(),
/// plugins: vec![&autoeat::Plugin::default()], /// plugins: vec![Box::new(autoeat::Plugin::default())],
/// handle: Box::new(handle), /// handle,
/// }).await.unwrap(); /// }).await;
/// ``` /// ```
pub async fn start< pub async fn start<
S: Send + Sync + Clone + 'static, S: Send + Sync + Clone + 'static,