aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/testbot/commands.rs
blob: 599c693d6a79bc7ead8a88aece4e74a9f5c44ccb (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
pub mod combat;
pub mod debug;
pub mod movement;

use azalea::{
    Client, brigadier::prelude::*, client_chat::ChatPacket, entity::metadata::Player,
    player::GameProfileComponent,
};
use bevy_ecs::query::With;
use parking_lot::Mutex;

use crate::State;

pub type Ctx = CommandContext<Mutex<CommandSource>, eyre::Result<i32>>;
pub type Dispatcher = CommandDispatcher<Mutex<CommandSource>, eyre::Result<i32>>;

pub struct CommandSource {
    pub bot: Client,
    pub state: State,
    pub chat: ChatPacket,
}

impl CommandSource {
    pub fn reply(&self, message: impl Into<String>) {
        let message = message.into();
        if self.chat.is_whisper() {
            // /msg instead of /w for compat with custom servers
            self.bot
                .chat(format!("/msg {} {message}", self.chat.sender().unwrap()));
        } else {
            self.bot.chat(message);
        }
    }

    pub fn entity(&self) -> Option<azalea::EntityRef> {
        let username = self.chat.sender()?;
        self.bot
            .any_entity_by::<&GameProfileComponent, With<Player>>(
                |profile: &GameProfileComponent| profile.name == username,
            )
            .ok()
            .flatten()
    }
}

pub fn register_commands(commands: &mut Dispatcher) {
    combat::register(commands);
    debug::register(commands);
    movement::register(commands);
}