aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/steal.rs
blob: 44d2eb571951e2147f23e88f4cc18617551b892c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Steal all the diamonds from all the nearby chests.

use std::sync::Arc;

use azalea::{BlockPos, pathfinder::goals::RadiusGoal, prelude::*};
use azalea_inventory::ItemStack;
use azalea_registry::builtin::{BlockKind, ItemKind};
use parking_lot::Mutex;

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

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

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

async fn handle(bot: Client, event: Event, state: State) -> eyre::Result<()> {
    if let Event::Chat(m) = event {
        if m.sender() == Some(bot.username()) {
            return Ok(());
        };
        if m.content() != "go" {
            return Ok(());
        }

        steal(bot, state).await?;
    }

    Ok(())
}

async fn steal(bot: Client, state: State) -> eyre::Result<()> {
    {
        let mut is_stealing = state.is_stealing.lock();
        if *is_stealing {
            bot.chat("Already stealing");
            return Ok(());
        }
        *is_stealing = true;
    }

    state.checked_chests.lock().clear();

    loop {
        let chest_block = bot
            .world()?
            .read()
            .find_blocks(bot.position()?, &BlockKind::Chest.into())
            .find(
                // find the closest chest that hasn't been checked
                |block_pos| !state.checked_chests.lock().contains(block_pos),
            );
        let Some(chest_block) = chest_block else {
            break;
        };

        state.checked_chests.lock().push(chest_block);

        bot.goto(RadiusGoal::new(chest_block.center(), 3.)).await;

        let Some(chest) = bot.open_container_at(chest_block).await? else {
            println!("Couldn't open chest at {chest_block:?}");
            continue;
        };

        println!("Getting contents of chest at {chest_block:?}");
        for (index, slot) in chest.contents().unwrap_or_default().iter().enumerate() {
            println!("Checking slot {index}: {slot:?}");
            let ItemStack::Present(item) = slot else {
                continue;
            };
            if item.kind == ItemKind::Diamond {
                println!("clicking slot ^");
                chest.left_click(index);
            }
        }
    }

    bot.chat("Done");

    *state.is_stealing.lock() = false;

    Ok(())
}