Skip to main content

steal/
steal.rs

1//! Steal all the diamonds from all the nearby chests.
2
3use std::sync::Arc;
4
5use azalea::{BlockPos, pathfinder::goals::RadiusGoal, prelude::*};
6use azalea_registry::{
7    builtin::{BlockKind, ItemKind},
8    item::ItemStack,
9};
10use parking_lot::Mutex;
11
12#[tokio::main]
13async fn main() -> AppExit {
14    let account = Account::offline("bot");
15    // or let bot = Account::microsoft("email").await.unwrap();
16
17    ClientBuilder::new()
18        .set_handler(handle)
19        .start(account, "localhost")
20        .await
21}
22
23#[derive(Clone, Component, Default)]
24struct State {
25    pub is_stealing: Arc<Mutex<bool>>,
26    pub checked_chests: Arc<Mutex<Vec<BlockPos>>>,
27}
28
29async fn handle(bot: Client, event: Event, state: State) -> eyre::Result<()> {
30    if let Event::Chat(m) = event {
31        if m.sender() == Some(bot.username()) {
32            return Ok(());
33        };
34        if m.content() != "go" {
35            return Ok(());
36        }
37
38        steal(bot, state).await?;
39    }
40
41    Ok(())
42}
43
44async fn steal(bot: Client, state: State) -> eyre::Result<()> {
45    {
46        let mut is_stealing = state.is_stealing.lock();
47        if *is_stealing {
48            bot.chat("Already stealing");
49            return Ok(());
50        }
51        *is_stealing = true;
52    }
53
54    state.checked_chests.lock().clear();
55
56    loop {
57        let chest_block = bot
58            .world()?
59            .read()
60            .find_blocks(bot.position()?, &BlockKind::Chest.into())
61            .find(
62                // find the closest chest that hasn't been checked
63                |block_pos| !state.checked_chests.lock().contains(block_pos),
64            );
65        let Some(chest_block) = chest_block else {
66            break;
67        };
68
69        state.checked_chests.lock().push(chest_block);
70
71        bot.goto(RadiusGoal::new(chest_block.center(), 3.)).await;
72
73        let Some(chest) = bot.open_container_at(chest_block).await? else {
74            println!("Couldn't open chest at {chest_block:?}");
75            continue;
76        };
77
78        println!("Getting contents of chest at {chest_block:?}");
79        for (index, slot) in chest.contents().unwrap_or_default().iter().enumerate() {
80            println!("Checking slot {index}: {slot:?}");
81            let ItemStack::Present(item) = slot else {
82                continue;
83            };
84            if item.kind == ItemKind::Diamond {
85                println!("clicking slot ^");
86                chest.left_click(index);
87            }
88        }
89    }
90
91    bot.chat("Done");
92
93    *state.is_stealing.lock() = false;
94
95    Ok(())
96}