aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/pvp.rs
blob: 9405cb6fe58eec7142d72954c23c69770a057cc1 (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
use std::sync::Arc;

use azalea::{pathfinder, Account, Accounts, Client, Event};
use parking_lot::Mutex;

#[tokio::main]
async fn main() {
    let accounts = Accounts::new();

    for i in 0..10 {
        accounts.add(Account::offline(format!("bot{}", i)));
    }

    azalea::start_swarm(azalea::SwarmOptions {
        accounts,
        address: "localhost",

        swarm_state: Arc::new(Mutex::new(State::default())),
        state: State::default(),

        swarm_plugins: vec![Arc::new(pathfinder::Plugin::default())],
        plugins: vec![],

        handle: Box::new(handle),
        swarm_handle: Box::new(handle),
    })
    .await
    .unwrap();
}

struct State {}
struct SwarmState {}

async fn handle(bots: Client, event: Arc<Event>, state: Arc<Mutex<State>>) {
    match *event {
        Event::Tick => {
            // choose an arbitrary player within render distance to target
            if let Some(target) = bots
                .dimension()
                .find_one_entity(|e| e.id == "minecraft:player")
            {
                for bot in bots {
                    bot.tick_goto_goal(pathfinder::Goals::Reach(target.bounding_box));
                    // if target.bounding_box.distance(bot.eyes) < bot.reach_distance() {
                    if bot.entity.can_reach(target.bounding_box) {
                        bot.swing();
                    }
                    if !h.using_held_item() && bot.state.lock().hunger <= 17 {
                        bot.hold(azalea::ItemGroup::Food);
                        tokio::task::spawn(bot.use_held_item());
                    }
                }
            }
        }
        _ => {}
    }
}