diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-10-07 19:57:42 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-07 19:57:42 -0500 |
| commit | ba4cfaafaec97a3c5b9405fe542035ebe9039edd (patch) | |
| tree | 5fc7340a49f96d84f86ed6adf400ad461c47d1b6 /azalea/examples/craft_dig_straight_down.rs | |
| parent | e0bcab53b8a3721a008e47062c6b5972fa64b8ad (diff) | |
| download | azalea-drasl-ba4cfaafaec97a3c5b9405fe542035ebe9039edd.tar.xz | |
Bot API (#27)
Basically make the `azalea` crate have stuff
Diffstat (limited to 'azalea/examples/craft_dig_straight_down.rs')
| -rw-r--r-- | azalea/examples/craft_dig_straight_down.rs | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/azalea/examples/craft_dig_straight_down.rs b/azalea/examples/craft_dig_straight_down.rs index 53e5cae8..3b7267ef 100644 --- a/azalea/examples/craft_dig_straight_down.rs +++ b/azalea/examples/craft_dig_straight_down.rs @@ -1,45 +1,66 @@ -use azalea::{Bot, Event}; +use azalea::{pathfinder, Account}; +use azalea::{Bot, Client, Event}; +use parking_lot::Mutex; +use std::sync::Arc; -struct Context { - pub started: bool +#[derive(Default)] +struct State { + pub started: bool, } #[tokio::main] async fn main() { - let bot = Bot::offline("bot"); + let account = Account::offline("bot"); // or let bot = azalea::Bot::microsoft("access token").await; - bot.join("localhost".try_into().unwrap()).await.unwrap(); - - let ctx = Arc::new(Mutex::new(Context { started: false })); - - loop { - tokio::spawn(handle_event(bot.next().await, bot, ctx.clone())); - } + azalea::start(azalea::Options { + account, + address: "localhost", + state: Arc::new(Mutex::new(State::default())), + plugins: vec![], + handle, + }) + .await + .unwrap(); } - -async fn handle_event(event: &Event, bot: &Bot, ctx: Arc<Context>) { +async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) { match event { - Event::Message(m) { - if m.username == bot.player.username { return }; + 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 }; + if ctx_lock.started { + return; + }; ctx_lock.started = true; drop(ctx_lock); - bot.goto( - pathfinder::Goals::NearXZ(5, azalea::BlockXZ(0, 0)) - ).await; - let chest = bot.open_container(&bot.world.find_one_block(|b| b.id == "minecraft:chest")).await.unwrap(); - bot.take_amount(&chest, 5, |i| i.id == "#minecraft:planks").await; + bot.goto(pathfinder::Goals::NearXZ(5, azalea::BlockXZ(0, 0))) + .await; + let chest = bot + .open_container(&bot.world.find_one_block(|b| b.id == "minecraft:chest")) + .await + .unwrap(); + bot.take_amount(&chest, 5, |i| i.id == "#minecraft:planks") + .await; chest.close().await; - let crafting_table = bot.open_crafting_table(&bot.world.find_one_block(|b| b.id == "minecraft:crafting_table")).await.unwrap(); - bot.craft(&crafting_table, &bot.recipe_for("minecraft:sticks")).await?; - let pickaxe = bot.craft(&crafting_table, &bot.recipe_for("minecraft:wooden_pickaxe")).await?; + let crafting_table = bot + .open_crafting_table( + &bot.world + .find_one_block(|b| b.id == "minecraft:crafting_table"), + ) + .await + .unwrap(); + bot.craft(&crafting_table, &bot.recipe_for("minecraft:sticks")) + .await?; + let pickaxe = bot + .craft(&crafting_table, &bot.recipe_for("minecraft:wooden_pickaxe")) + .await?; crafting_table.close().await; bot.hold(&pickaxe); @@ -50,7 +71,7 @@ async fn handle_event(event: &Event, bot: &Bot, ctx: Arc<Context>) { } } } - }, + } _ => {} } -}
\ No newline at end of file +} |
