Skip to main content

testbot/
commands.rs

1pub mod combat;
2pub mod debug;
3pub mod movement;
4
5use azalea::{
6    Client, brigadier::prelude::*, client_chat::ChatPacket, entity::metadata::Player,
7    player::GameProfileComponent,
8};
9use bevy_ecs::query::With;
10use parking_lot::Mutex;
11
12use crate::State;
13
14pub type Ctx = CommandContext<Mutex<CommandSource>, eyre::Result<i32>>;
15pub type Dispatcher = CommandDispatcher<Mutex<CommandSource>, eyre::Result<i32>>;
16
17pub struct CommandSource {
18    pub bot: Client,
19    pub state: State,
20    pub chat: ChatPacket,
21}
22
23impl CommandSource {
24    pub fn reply(&self, message: impl Into<String>) {
25        let message = message.into();
26        if self.chat.is_whisper() {
27            // /msg instead of /w for compat with custom servers
28            self.bot
29                .chat(format!("/msg {} {message}", self.chat.sender().unwrap()));
30        } else {
31            self.bot.chat(message);
32        }
33    }
34
35    pub fn entity(&self) -> Option<azalea::EntityRef> {
36        let username = self.chat.sender()?;
37        self.bot
38            .any_entity_by::<&GameProfileComponent, With<Player>>(
39                |profile: &GameProfileComponent| profile.name == username,
40            )
41            .ok()
42            .flatten()
43    }
44}
45
46pub fn register_commands(commands: &mut Dispatcher) {
47    combat::register(commands);
48    debug::register(commands);
49    movement::register(commands);
50}