Skip to main content

ghast_ext/
lib.rs

1//! Sequential, mineflayer-style helpers for ghast bots.
2//!
3//! ghast's bot API is a per-tick state machine ([`ghast_client::Behavior::tick`]): exact and
4//! cheap, but every multi-step job ("turn, then dig, then wait for the block") becomes an
5//! explicit state enum. ghast-ext lets you write the same job as straight-line async code,
6//! without giving up the tick model:
7//!
8//! ```no_run
9//! use ghast_client::{Account, BlockPos, Client, Face, GameVersion, Interests};
10//! use ghast_ext::{Event, Ext};
11//!
12//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
13//! let mut bot = Ext::new(|bot| async move {
14//!     let below = BlockPos::new(0, 63, 2);
15//!     bot.look_at_block(below, None).await?;
16//!     bot.dig(below).await?;                       // tool choice + vanilla dig time
17//!     bot.equip("cobblestone").await?;
18//!     bot.place(BlockPos::new(0, 62, 2), Face::Up).await?;
19//!     let ores = bot.find_blocks(|b: ghast_ext::Block| b.is_ore(), 32.0, 10).await?;
20//!     bot.wait_for(|ev| matches!(ev, Event::Chat(m) if m.plain().contains("hi"))).await?;
21//!     bot.chat("hello");
22//!     Ok(())
23//! })
24//! .interests(Interests::CHAT | Interests::ENTITIES);
25//!
26//! let client = Client::new("localhost:25565".parse()?, GameVersion::V1_8_9, Account::offline("Bot")?);
27//! client.run(&mut bot).await?;
28//! # Ok(()) }
29//! ```
30//!
31//! # How it works
32//!
33//! [`Ext`] is a [`ghast_client::Behavior`] with a tiny single-threaded executor inside.
34//! Every tick it polls the tasks that are ready. A task reaches the game only through
35//! [`Bot::with`]: it hands over a closure, the executor runs it with the tick's
36//! [`ghast_client::TickContext`] and resumes the task in the same tick. So:
37//!
38//! - every action is written inside [`ghast_client::Behavior::tick`], before the tick's
39//!   single movement packet, in call order, exactly as hand-written behaviours do;
40//! - the core's guarantees hold unchanged: dig / place are refused unless the **sent**
41//!   crosshair is on the block (the helpers aim first and act on a later tick), clicks are
42//!   refused while turning and while another click is in flight (the helpers wait and
43//!   retry), rotation is whole mouse counts;
44//! - nothing runs between ticks, and while the server is silent the behaviour (and with it
45//!   every task) is on hold;
46//! - no `unsafe`: the context never leaves the tick.
47//!
48//! Helpers fail with [`Error`] when the world changes under them: a teleport / setback or a
49//! death interrupts what is running ([`Ext::interrupt_on_teleport`]), and the end of the
50//! session drops (cancels) every task. Dropping a helper's future releases the buttons and
51//! keys it held.
52//!
53//! One task should own the look and the movement keys at a time: two tasks aiming in the
54//! same tick override each other (the last `set_aim` wins) and a turning look blocks clicks.
55
56#![forbid(unsafe_code)]
57#![warn(missing_docs, rust_2018_idioms, unreachable_pub, unnameable_types)]
58// Without a version feature the crate builds (every lookup answers "unknown") but the
59// per-version arms are gone.
60#![cfg_attr(
61    not(any(
62        feature = "v1_8",
63        feature = "v1_12_2",
64        feature = "v1_16_5",
65        feature = "v26_1",
66        feature = "v26_2",
67        feature = "v26_3"
68    )),
69    allow(unused_variables, dead_code, unreachable_code)
70)]
71
72mod actions;
73mod error;
74mod events;
75mod executor;
76mod find;
77mod fishing;
78#[cfg(feature = "pathfinder")]
79mod goto;
80mod items;
81mod look;
82mod nav;
83mod ride;
84mod screens;
85
86pub use actions::DigOptions;
87pub use error::Error;
88pub use events::{Event, Events, NextEvent};
89pub use executor::{Bot, Ext, JoinHandle, Timeout, WaitTicks, WhenDone, With};
90pub use find::{Block, BlockMatch, EntityInfo};
91/// The path finder ([`Bot::goto`]), re-exported (feature `pathfinder`).
92#[cfg(feature = "pathfinder")]
93pub use ghast_pathfinder as pathfinder;
94pub use items::{Container, ItemMatch, item_id, item_name};
95pub use look::yaw_pitch;
96pub use ride::Ride;