diff options
| -rw-r--r-- | examples/craft_dig_straight_down.rs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/examples/craft_dig_straight_down.rs b/examples/craft_dig_straight_down.rs index 54bf0015..47c4fe28 100644 --- a/examples/craft_dig_straight_down.rs +++ b/examples/craft_dig_straight_down.rs @@ -1,15 +1,35 @@ use azalea::{Bot, Event}; -let bot = Bot::offline("bot"); -// or let bot = azalea::Bot::microsoft("access token").await; +struct Context { + pub started: bool +} + +#[tokio::main] +async fn main() { + let bot = Bot::offline("bot"); + // or let bot = azalea::Bot::microsoft("access token").await; + + bot.join("localhost".try_into().unwrap()).await.unwrap(); -bot.join("localhost".try_into().unwrap()).await.unwrap(); + let ctx = Arc::new(Mutex::new(Context { started: false })); -loop { - match bot.next().await { + loop { + tokio::spawn(handle_event(bot.next().await, bot, ctx.clone())); + } +} + + +async fn handle_event(event: &Event, bot: &Bot, ctx: Arc<Context>) { + match event { Event::Message(m) { if m.username == bot.player.username { return }; if m.message = "go" { + // make sure we only start once + let ctx_lock = ctx.lock().unwrap(); + if ctx_lock.started { return }; + ctx_lock.started = true; + drop(ctx_lock); + bot.goto_goal( pathfinder::Goals::NearXZ(5, azalea::BlockXZ(0, 0)) ).await; @@ -34,4 +54,4 @@ loop { }, _ => {} } -} +}
\ No newline at end of file |
