azalea 0.16.0+mc26.1

A framework for creating Minecraft bots.
Documentation
pub mod combat;
pub mod debug;
pub mod movement;

use azalea::{
    Client, brigadier::prelude::*, 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>>;

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,
            )
    }
}

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