aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/bot.rs
blob: 1570fa5e599ab0127aaaace2cf5fe2254ed9ca90 (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
use crate::{Client, Event};
use async_trait::async_trait;
use parking_lot::Mutex;
use std::sync::Arc;

#[derive(Default)]
pub struct Plugin {
    pub state: Arc<Mutex<State>>,
}

#[derive(Default)]
pub struct State {
    jumping_once: bool,
}

pub trait BotTrait {
    fn jump(&self);
}

impl BotTrait for azalea_client::Client {
    /// Try to jump next tick.
    fn jump(&self) {
        let player_lock = self.player.lock();
        let mut dimension_lock = self.dimension.lock();

        let mut player_entity = player_lock
            .entity_mut(&mut dimension_lock)
            .expect("Player must exist");

        player_entity.jumping = true;
    }
}

#[async_trait]
impl crate::Plugin for Plugin {
    async fn handle(self: Arc<Self>, mut bot: Client, event: Arc<Event>) {
        if let Event::Tick = *event {
            let mut state = self.state.lock();
            if state.jumping_once {
                if bot.jumping() {
                    state.jumping_once = false;
                } else {
                    bot.set_jumping(true);
                }
            }
        }
    }
}