Crate azalea

source ·
Expand description

Azalea is a framework for creating Minecraft bots.

Internally, it’s just a wrapper over azalea_client, adding useful functions for making bots. Because of this, lots of the documentation will refer to azalea_client. You can just replace these with azalea in your code, since everything from azalea_client is re-exported in azalea.

Installation

First, install Rust nightly with rustup install nightly and rustup default nightly.

Then, add one of the following lines to your Cargo.toml:

Latest bleeding-edge version: azalea = { git="https://github.com/mat-1/azalea" }
Latest “stable” release: azalea = "0.6.0"

Optimization

For faster compile times, make a .cargo/config.toml file in your project and copy this file into it. You may have to install the LLD linker.

For faster performance in debug mode, add the following code to your Cargo.toml:

[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3

Examples

//! A bot that logs chat messages sent in the server to the console.

use azalea::prelude::*;
use parking_lot::Mutex;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let account = Account::offline("bot");
    // or Account::microsoft("example@example.com").await.unwrap();

    loop {
        let e = azalea::ClientBuilder::new()
            .set_handler(handle)
            .start(account, "localhost")
            .await;
        eprintln!("{:?}", e);
    }
}

#[derive(Default, Clone, Component)]
pub struct State {}

async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
    match event {
        Event::Chat(m) => {
            println!("{}", m.message().to_ansi());
        }
        _ => {}
    }

    Ok(())
}

Plugins

Azalea uses Bevy ECS internally to store information about the world and clients. Bevy plugins are more powerful than async handler functions, but more difficult to use. See pathfinder as an example of how to make a plugin. You can then enable a plugin by adding .add_plugin(ExamplePlugin) in your client/swarm builder.

Also note that just because something is an entity in the ECS doesn’t mean that it’s a Minecraft entity. You can filter for that by having With<MinecraftEntityId> as a filter.

See the Bevy Cheatbook to learn more about Bevy ECS (and the ECS paradigm in general).

Re-exports

pub use azalea_block as blocks;
pub use azalea_protocol as protocol;

Modules

Re-export important parts of bevy_ecs and bevy_app and make them more compatible with Azalea.
Ping Minecraft servers.
The Azalea prelude. Things that are necessary for a bare-bones bot are re-exported here.
Borrowed from bevy_core.

Structs

Something that can join Minecraft servers.
The coordinates of a block in the world. For entities (if the coordinate with decimals), use Vec3 instead.
Client has the things that a user interacting with the library will want. Things that a player in the world will want to know are in LocalPlayer.
A builder for creating new Clients. This is the recommended way of making Azalea bots.
This plugin group will add all the default plugins necessary for swarms to work.
A component only present in players that contains the [GameProfile] (which you can use to get a player’s name).
This is a component for our local player entities that are probably in a world. If you have access to a Client, you probably don’t need to care about this since Client gives you access to everything here.
A player in the tab list.
An event sent when the client starts sprinting. This does not get sent for non-local entities.
An event sent when the client starts walking. This does not get sent for non-local entities.
A swarm is a way to conveniently control many bots at once, while also being able to control bots at an individual level when desired.
Create a new Swarm.
Used to represent an exact position in the world where an entity could be. For blocks, BlockPos is used instead.
A world where the chunks are stored as weak pointers. This is used for shared worlds.

Enums

A chat packet, either a system message or a chat message.
Something that happened in-game, such as a tick passing or chat message being sent.
An error that happened while joining the server.
The directions that we can sprint in. It’s a subset of WalkDirection.
An event about something that doesn’t have to do with a single bot.

Type Definitions