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

use azalea::brigadier::prelude::*;
use azalea::chat::ChatPacket;
use azalea::ecs::prelude::*;
use azalea::entity::metadata::Player;
use azalea::Client;
use azalea::GameProfileComponent;
use parking_lot::Mutex;

use crate::State;

pub type Ctx = CommandContext<Mutex<CommandSource>>;

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

impl CommandSource {
    pub fn reply(&self, message: &str) {
        if self.chat.is_whisper() {
            self.bot
                .chat(&format!("/w {} {}", self.chat.username().unwrap(), message));
        } else {
            self.bot.chat(message);
        }
    }

    pub fn entity(&mut self) -> Option<Entity> {
        let username = self.chat.username()?;
        self.bot.entity_by::<With<Player>, &GameProfileComponent>(
            |profile: &&GameProfileComponent| profile.name == username,
        )
    }
}

pub fn register_commands(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
    combat::register(commands);
    debug::register(commands);
    movement::register(commands);
}