summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: bd9a39fe18bd29d4c892b8bd93aa0caae17e3761 (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
95
96
97
98
99
100
101
102
103
use azalea::{
    account::yggdrasil::Backend,
    ecs::prelude::{With, Without},
    entity::{Dead, LocalEntity, Position, metadata::AbstractMonster},
    error::MissingComponentError,
    prelude::*,
};
use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    /// Player name
    #[arg(short, long)]
    username: String,

    /// Drasl backend to authenticate with (should have /auth, /session and optionally /player routes)
    #[arg(short, long)]
    backend: String,

    /// Minecraft server to connect to
    #[arg(short, long)]
    server: String,

    /// If not present, password will be queried at runtime as needed. Drasl Minecraft Token can be used instead of password.
    #[arg(short, long)]
    password: Option<String>,

    /// Disable requesting signing certificates
    #[arg(short, long, default_value_t = false)]
    no_certs: bool,

    /// Enable attacking closest entity
    #[arg(short, long, default_value_t = false)]
    autoclick: bool,
}

#[derive(Default, Clone, Component)]
struct State {
    autoclick: bool,
}

#[tokio::main]
async fn main() -> AppExit {
    let args = Args::parse();

    let backend = Backend::new_drasl(&args.backend, !args.no_certs);
    let account = match args.password {
        None => Account::yggdrasil(args.username.clone(), backend).await,
        Some(x) => {
            Account::yggdrasil_with_password(args.username.clone(), x.clone(), backend).await
        }
    };
    let account = match account {
        Ok(x) => x,
        Err(e) => {
            eprintln!("Error logging in: {e}");
            return AppExit::error();
        }
    };

    ClientBuilder::new()
        .set_handler(handle)
        .set_state(State {
            autoclick: args.autoclick,
        })
        .start(account, args.server.as_str())
        .await
}

fn autoclick(bot: Client) -> Result<(), MissingComponentError> {
    let bot_position = bot.eye_position()?;
    let nearest_entity = bot.nearest_entity_by::<&Position, (
        With<AbstractMonster>,
        Without<LocalEntity>,
        Without<Dead>,
    )>(|position: &Position| {
        let distance = bot_position.distance_to(**position);
        distance < 4.
    })?;

    if let Some(nearest_entity) = nearest_entity {
        nearest_entity.attack();
    }

    Ok(())
}

async fn handle(bot: Client, event: Event, state: State) -> Result<(), ()> {
    match event {
        Event::Chat(m) => {
            println!("{}", m.message().to_ansi());
        }
        Event::Tick => {
            if state.autoclick && !bot.has_attack_cooldown() {
                autoclick(bot).ok();
            }
        }
        _ => {}
    }

    Ok(())
}