testbot/
commands.rs

1pub mod combat;
2pub mod debug;
3pub mod movement;
4
5use azalea::{
6    Client, brigadier::prelude::*, 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>>;
15
16pub struct CommandSource {
17    pub bot: Client,
18    pub state: State,
19    pub chat: ChatPacket,
20}
21
22impl CommandSource {
23    pub fn reply(&self, message: impl Into<String>) {
24        let message = message.into();
25        if self.chat.is_whisper() {
26            self.bot
27                .chat(format!("/w {} {message}", self.chat.sender().unwrap()));
28        } else {
29            self.bot.chat(message);
30        }
31    }
32
33    pub fn entity(&self) -> Option<azalea::EntityRef> {
34        let username = self.chat.sender()?;
35        self.bot
36            .any_entity_by::<&GameProfileComponent, With<Player>>(
37                |profile: &GameProfileComponent| profile.name == username,
38            )
39    }
40}
41
42pub fn register_commands(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
43    combat::register(commands);
44    debug::register(commands);
45    movement::register(commands);
46}