Skip to main content

Crate ghast_ext

Crate ghast_ext 

Source
Expand description

Sequential, mineflayer-style helpers for ghast bots.

ghast’s bot API is a per-tick state machine (ghast_client::Behavior::tick): exact and cheap, but every multi-step job (“turn, then dig, then wait for the block”) becomes an explicit state enum. ghast-ext lets you write the same job as straight-line async code, without giving up the tick model:

use ghast_client::{Account, BlockPos, Client, Face, GameVersion, Interests};
use ghast_ext::{Event, Ext};

let mut bot = Ext::new(|bot| async move {
    let below = BlockPos::new(0, 63, 2);
    bot.look_at_block(below, None).await?;
    bot.dig(below).await?;                       // tool choice + vanilla dig time
    bot.equip("cobblestone").await?;
    bot.place(BlockPos::new(0, 62, 2), Face::Up).await?;
    let ores = bot.find_blocks(|b: ghast_ext::Block| b.is_ore(), 32.0, 10).await?;
    bot.wait_for(|ev| matches!(ev, Event::Chat(m) if m.plain().contains("hi"))).await?;
    bot.chat("hello");
    Ok(())
})
.interests(Interests::CHAT | Interests::ENTITIES);

let client = Client::new("localhost:25565".parse()?, GameVersion::V1_8_9, Account::offline("Bot")?);
client.run(&mut bot).await?;

§How it works

Ext is a ghast_client::Behavior with a tiny single-threaded executor inside. Every tick it polls the tasks that are ready. A task reaches the game only through Bot::with: it hands over a closure, the executor runs it with the tick’s ghast_client::TickContext and resumes the task in the same tick. So:

  • every action is written inside ghast_client::Behavior::tick, before the tick’s single movement packet, in call order, exactly as hand-written behaviours do;
  • the core’s guarantees hold unchanged: dig / place are refused unless the sent crosshair is on the block (the helpers aim first and act on a later tick), clicks are refused while turning and while another click is in flight (the helpers wait and retry), rotation is whole mouse counts;
  • nothing runs between ticks, and while the server is silent the behaviour (and with it every task) is on hold;
  • no unsafe: the context never leaves the tick.

Helpers fail with Error when the world changes under them: a teleport / setback or a death interrupts what is running (Ext::interrupt_on_teleport), and the end of the session drops (cancels) every task. Dropping a helper’s future releases the buttons and keys it held.

One task should own the look and the movement keys at a time: two tasks aiming in the same tick override each other (the last set_aim wins) and a turning look blocks clicks.

Re-exports§

pub use ghast_pathfinder as pathfinder;

Structs§

Block
A block state of the session’s version, named with ghast-data.
Bot
A handle to the bot for async tasks: cheap to clone, Send + Sync.
Container
An open container window (Bot::open_container).
DigOptions
Options of Bot::dig_with.
EntityInfo
A snapshot of a tracked entity.
Events
A subscription to the bot’s events (Bot::events): events from the tick after it was made, buffered until read. Dropping it unsubscribes.
Ext
A Behavior that runs async tasks inside the ticks: write a bot as sequential code.
JoinHandle
A spawned task (Bot::spawn): await it for its result, or JoinHandle::abort it. Dropping the handle does not stop the task.
NextEvent
The future of Events::recv.
Ride
What the bot rides (Bot::vehicle).
Timeout
The future of Bot::timeout.
WaitTicks
The future of Bot::wait_ticks.
With
The future of Bot::with.

Enums§

BlockMatch
What Bot::find_blocks looks for: a name, several names, or a predicate.
Error
Why a helper gave up. Nothing was left half-done: buttons are released and a window the helper opened is closed again.
Event
Something that happened, as delivered to Bot::events subscribers (in the order the tick saw them).
ItemMatch
What an item helper looks for: a registry name, an item id of the session’s version, or a predicate on the stack.
WhenDone
What Ext does when the main task returns.

Functions§

item_id
The item id of a registry name in version.
item_name
The registry name of an item id in version ("" when unknown).
yaw_pitch
Yaw / pitch (degrees, vanilla convention) that look from eyes at p.