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 {
/// # 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<Event>, state: Arc<Mutex<State>>) -> anyhow::Result<()> {
/// # async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
/// bot.chat("Hello, world!").await.unwrap();
/// # Ok(())
/// # }

View file

@ -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<Event>, state: Arc<Mutex<State>>) -> anyhow::Result<()> {
match event {
_ => {}
}
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
Ok(())
}
async fn swarm_handle(
swarm: Swarm,
event: Arc<Event>,
state: Arc<Mutex<SwarmState>>,
) -> 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;

View file

@ -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]

View file

@ -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());
}

View file

@ -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<Event>, state: Arc<Mutex<State>>) -> 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,