ghast v0.5.6

Core concepts

World and entities

After this page you can read and name any block the server has sent, get its collision shape, and find the players and mobs around the bot.

Reading blocks

Every session keeps the chunks the server sends in a ghast-world store. Behaviours read it through ctx.world(), a &dyn WorldView (docs):

methodreturns
raw_block(x, y, z)Option<u32>: the version's raw block state, None when the column is not loaded
is_column_loaded(cx, cz)whether the server sent that chunk column and has not unloaded it
block_query(x, y, z)dig and place facts (air, hardness, tool group, ray box), None when not loaded
column(cx, cz)the whole stored column, for bulk reads without a call per block

loaded_columns() counts the loaded columns, and build_height() gives (min_y, height) on 26.x, where the height depends on the dimension (the overworld is (-64, 384)). It is None on 1.8.9 to 1.16.5, which have a fixed 0..256 world.

let p = ctx.pose().pos;
let below = BlockPos::new(p.x.floor() as i32, p.y.floor() as i32 - 1, p.z.floor() as i32);
match ctx.world().raw_block(below.x, below.y, below.z) {
    None => println!("column not loaded: unknown"),
    Some(raw) => println!("standing on {}", name(ctx.version(), raw)),
}

A lookup costs about 4 ns per block for a physics-like 3×4×3 box and 10 to 12 ns at random positions over 441 columns (x86-64 WSL2 dev machine, release build, as measured in the ghast-world README).

Loaded is not air

raw_block returns None for a column the bot does not have. Never treat that as air. A bot that does will walk into a hole that is really a floor, or dig into a wall it thinks is open. The whole stack keeps the distinction: the ext helpers fail with Error::NotLoaded, and the path finder treats unknown blocks as impassable and returns a partial path that ends at the loaded edge.

Only the physics maps unloaded to air, because the vanilla client's empty chunk does, and movement must match it (see Accuracy).

What the store keeps besides block states is set when you build the client. Light and block entities (sign text, chest data, ...) are off by default:

let client = Client::new(addr, GameVersion::V26_3, Account::offline("Bot")?)
    .world_config(ghast::world::WorldConfig::default().with_block_entities(true));

Block states and shapes

The raw value depends on the version. On 1.8.9 and 1.12.2 it is id << 4 | meta; on 1.16.5 and 26.x it is the global palette id. Decode it with the matching ghast::data module (v1_8, v1_12, v1_16_5, v26_1, v26_2, v26_3):

fn name(v: GameVersion, raw: u32) -> String {
    match v {
        GameVersion::V26_3 => ghast::data::v26_3::BlockState::from_id(raw as u16)
            .map_or(format!("id {raw}"), |s| s.to_string()),
        _ => format!("raw {raw}"),
    }
}

The block_probe example has the full match over every version. If you use the ext helpers, ghast::ext::Block::new(version, raw).name() does this for you.

On 1.16.5 and 26.x a state has named properties, and it prints in vanilla's string form:

use ghast::data::v26_3::{BlockState, Property};

let stairs = BlockState::parse("oak_stairs[facing=east]").unwrap();
assert_eq!(stairs.get(Property::Half), Some("bottom"));
for b in stairs.voxel_shape().boxes { /* collision boxes, block-local */ }
println!("{stairs}"); // minecraft:oak_stairs[facing=east,half=bottom,...]

Collision shapes come from the same tables: voxel_shape() on 26.x, shape() and collision() on the older versions. Shapes that depend on neighbours (fences, panes, walls, stairs, chests), on an entity or on a block entity are never presented as static: on the older versions Collision::dependency says what else is needed and the boxes are only the isolated shape. The physics resolves them from neighbouring states; the world itself keeps no connection state. Every isolated 1.8 shape matches the vanilla oracle bit for bit.

Entities are opt-in

Without a request, the client drops entity packets before decoding them, and ctx.entities() returns None. Ask for them in interests:

interestgives
Interests::ENTITIESthe tracker: positions, boxes, metadata, equipment, effects
Interests::ENTITY_TRACKINGthe same without the generic metadata store, about 450 bytes less per entity
Interests::PLAYER_LISTthe tab list on top: player names and UUIDs
Interests::ENTITY_EVENTSspawn, removal, hurt, metadata, ... events in ctx.events().entities()

Without tracking, the crosshair is a pure block pick and nothing pushes the bot. Interests can change mid-session, but a tracker started late is empty until the server sends the entities again.

Finding players and mobs

fn interests(&self) -> Interests {
    Interests::ENTITIES | Interests::PLAYER_LIST
}

fn tick(&mut self, ctx: &mut TickContext<'_>) {
    let eyes = ctx.eyes(ctx.pose());
    let Some(ents) = ctx.entities() else { return };
    for e in ents.within([eyes.x, eyes.y, eyes.z], 16.0) {
        let [x, y, z] = e.position();
        match e.class() {
            EntityClass::Player => {
                let who = ents.player_name(e.id()).unwrap_or("?");
                println!("player {who} at {x:.1} {y:.1} {z:.1}");
            }
            EntityClass::Mob => println!("mob {:?} box {:?}", e.entity_type(), e.bounding_box()),
            _ => {}
        }
    }
}

The store (docs) has get(id), iter() (which skips the bot), within(pos, r), players() for the tab list and me() for the bot's own entry. An Entity gives position(), yaw(), pitch(), head_yaw(), velocity() (the last value the server sent), on_ground(), vehicle(), passengers(), equipment(slot), effects(), metadata() and the vanilla bounding_box().

Typed metadata reads are per version, because the indices differ: ghast::entity::v1_8::health(e), ghast::entity::v26_3::health(e), custom_name, flags, and on 26.x entity_type(e) gives the registry entry with its name.

Positions are what the vanilla client holds. On 1.8.9 to 1.16.5 that is the server position with each version's fixed-point rules (1/32 on 1.8, 1/4096 later). On 26.x the tracker runs the client's interpolation, checked against the real 26.1 client: 0 diffs over 8.66 M compared fields. The bot's own entry has no position: the client owns it, so read ctx.pose().

Tracking is cheap: about 25 ns per packet on 1.8 and 40 ns on 26.1 with everything on, and a 26.1 player with its spawn metadata takes 520 bytes (x86-64 WSL, bench in the ghast-entity README).