diff options
| -rw-r--r-- | src/main.rs | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 6dfc127..bd9a39f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,10 @@ -use azalea::{account::yggdrasil::Backend, prelude::*}; +use azalea::{ + account::yggdrasil::Backend, + ecs::prelude::{With, Without}, + entity::{Dead, LocalEntity, Position, metadata::AbstractMonster}, + error::MissingComponentError, + prelude::*, +}; use clap::Parser; #[derive(Parser, Debug)] @@ -23,6 +29,15 @@ struct Args { /// 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] @@ -46,18 +61,41 @@ async fn main() -> AppExit { ClientBuilder::new() .set_handler(handle) + .set_state(State { + autoclick: args.autoclick, + }) .start(account, args.server.as_str()) .await } -#[derive(Default, Clone, Component)] -pub struct State {} +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. + })?; -async fn handle(_bot: Client, event: Event, _state: State) -> Result<(), ()> { + 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(); + } + } _ => {} } |
