aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/steal.rs
blob: 7a7ee4bbff8e254b633f134576a7f0b76c34bd48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Steal all the diamonds from all the nearby chests.

use azalea::{prelude::*, BlockPos};
use azalea_inventory::operations::QuickMoveClick;
use azalea_inventory::ItemSlot;
use parking_lot::Mutex;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let account = Account::offline("bot");
    // or let bot = Account::microsoft("email").await.unwrap();

    ClientBuilder::new()
        .set_handler(handle)
        .start(account, "localhost")
        .await
        .unwrap();
}

#[derive(Default, Clone, Component)]
struct State {
    pub checked_chests: Arc<Mutex<Vec<BlockPos>>>,
}

async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> {
    match event {
        Event::Chat(m) => {
            if m.username() == Some(bot.profile.name.clone()) {
                return Ok(());
            };
            if m.content() != "go" {
                return Ok(());
            }
            {
                state.checked_chests.lock().clear();
            }

            let chest_block = bot
                .world()
                .read()
                .find_block(bot.position(), &azalea::Block::Chest.into());
            // TODO: update this when find_blocks is implemented
            let Some(chest_block) = chest_block else {
                bot.chat("No chest found");
                return Ok(());
            };
            // bot.goto(BlockPosGoal::from(chest_block));
            let Some(chest) = bot.open_container(chest_block).await else {
                println!("Couldn't open chest");
                return Ok(());
            };

            println!("Getting contents");
            for (index, slot) in chest
                .contents()
                .expect("we just opened the chest")
                .iter()
                .enumerate()
            {
                println!("Checking slot {index}: {slot:?}");
                if let ItemSlot::Present(item) = slot {
                    if item.kind == azalea::Item::Diamond {
                        println!("clicking slot ^");
                        chest.click(QuickMoveClick::Left { slot: index as u16 });
                    }
                }
            }

            println!("Done");
        }
        _ => {}
    }

    Ok(())
}