diff options
| author | mat <github@matdoes.dev> | 2022-06-25 15:04:21 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-06-25 15:04:21 -0500 |
| commit | f9e42fa3d8ae96936fe8f38777128c983f3deea4 (patch) | |
| tree | 81fac53ca3e8b88502053b8680e43defc93b7479 | |
| parent | c46eb556e2e7964f0709572a0151c181752c3182 (diff) | |
| download | azalea-drasl-f9e42fa3d8ae96936fe8f38777128c983f3deea4.tar.xz | |
Update craft_dig_straight_down.rs
| -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 |
