Skip to main content

Ext

Struct Ext 

Source
pub struct Ext { /* private fields */ }
Expand description

A Behavior that runs async tasks inside the ticks: write a bot as sequential code.

Each session (each Client::run), the factory is called with a fresh Bot handle on the first tick and its future becomes the main task. Every tick the executor polls the tasks that are ready and runs the closures they sent (Bot::with) with the tick’s context, until no task can make progress in this tick. When the session ends all tasks are dropped (cancelled).

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

let mut bot = Ext::new(|bot| async move {
    bot.chat("hello");
    let msg = bot.wait_for_chat(|m| m.plain().contains("bye")).await?;
    println!("{}", msg.plain());
    Ok(())
})
.interests(Interests::CHAT);
let client = Client::new("localhost:25565".parse()?, GameVersion::V1_8_9, Account::offline("Bot")?);
client.run(&mut bot).await?;

Implementations§

Source§

impl Ext

Source

pub fn new<F, Fut>(main: F) -> Self
where F: FnMut(Bot) -> Fut + Send + 'static, Fut: Future<Output = Result<(), Error>> + Send + 'static,

A behaviour whose main task is main(bot), called once per session.

let ext = ghast_ext::Ext::new(|bot| async move {
    bot.wait_ticks(100).await;
    Ok(())
});
Source

pub fn name(self, name: impl Into<String>) -> Self

Name for logs (default "ext").

let ext = ext.name("miner");
Source

pub fn interests(self, interests: Interests) -> Self

Packet groups to decode (default Interests::CHAT). Entity helpers need Interests::ENTITIES (player names also Interests::PLAYER_LIST), crate::Event::BlockUpdate needs Interests::BLOCK_UPDATES.

use ghast_client::Interests;
let ext = ext.interests(Interests::CHAT | Interests::ENTITIES | Interests::PLAYER_LIST);
Source

pub fn when_done(self, when: WhenDone) -> Self

What happens when the main task returns (default WhenDone::Disconnect).

let ext = ext.when_done(ghast_ext::WhenDone::Stay);
Source

pub fn auto_respawn(self, on: bool) -> Self

Respawn at once after a death (default off: helpers fail with Error::Died and the task decides).

let ext = ext.auto_respawn(true);
Source

pub fn interrupt_on_teleport(self, on: bool) -> Self

Whether running helpers (look, dig, place, walk, …) fail with Error::Teleported when the server teleports the player (default on: after a setback the plan they made is stale).

let ext = ext.interrupt_on_teleport(false);
Source

pub fn bot(&self) -> Bot

A handle to the bot (the same one the tasks get), e.g. for a task of another executor that sends chat lines.

let bot = ext.bot();
std::thread::spawn(move || bot.act(|ctx| ctx.chat("from another thread")));
Source

pub fn result(&self) -> Option<&Result<(), Error>>

The main task’s result in the last session (None while running or if it never finished).

if let Some(Err(e)) = ext.result() { eprintln!("bot failed: {e}"); }

Trait Implementations§

Source§

impl Behavior for Ext

Source§

fn name(&self) -> &str

Name for logs.
Source§

fn interests(&self) -> Interests

Optional packet groups this behaviour needs. Read every tick; a change updates the packet filter. Everything not asked for is dropped before it is decoded.
Source§

fn start(&mut self, ctx: &mut TickContext<'_>)

Called on the first tick the player is in the world, before that tick’s Behavior::tick (both see the same events).
Source§

fn tick(&mut self, ctx: &mut TickContext<'_>)

Called every tick while the player is in the world and the server is answering.
Source§

fn running(&self) -> bool

Whether the behaviour still wants ticks. Once it returns false the behaviour is done: it is not ticked again (the player keeps standing and answering the server) and Behavior::stop still runs when the session ends. Read after every start / tick.
Source§

fn stop(&mut self, reason: &str)

Called once when the session ends (or the behaviour panicked).
Source§

fn raw_packets(&self, version: GameVersion) -> Cow<'static, [i32]>

Extra clientbound play packet ids (of version) to copy undecoded into TickEvents::raw, including packets the core consumes itself (the core’s handling does not change). Read every tick like Behavior::interests; a change updates the packet filter. Decoding is the behaviour’s job (ghast::protocol::v1_8::play::clientbound / v1_16_5). A behaviour made of modules returns the union of their lists (Cow::Owned([a.ids(version), b.ids(version)].concat()); duplicates are harmless). Most needs have typed events now: TickEvents::effects, TickEvents::hurt, TickEvents::deaths, TickEvents::item_use, TickEvents::entities, TickContext::spawn_position.
Source§

fn after_tick(&mut self, view: &TickView<'_>)

Called after every tick’s movement step (also while the behaviour is on hold or has finished), with a read-only view of what the server now has: the pose sent this tick, the world, entities and inventory. For observers (a viewer, a recorder); it cannot act. Default: nothing. Read more
Source§

fn configuration_dialog(&mut self, dialog: &Dialog) -> Option<DialogAnswer>

26.1+: a dialog the server shows during the configuration state, before the player is in the world (behaviours do not tick there). Called only with Interests::DIALOGS; the client sends the returned answer at once, as vanilla would for that button (see crate::dialog); None leaves the dialog open. A dialog the answer opens is offered next. Play-state dialogs come through TickContext::dialog instead. Default: None.
Source§

impl Debug for Ext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Ext

§

impl !Sync for Ext

§

impl !UnwindSafe for Ext

§

impl Forget for Ext

§

impl Freeze for Ext
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze, *const dyn FnMut(Bot) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> + Send is !null: Freeze, PhantomData<dyn FnMut(Bot) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> + Send>: Freeze, *const ArcInner<Mutex<Shared>> is !null: Freeze, PhantomData<ArcInner<Mutex<Shared>>>: Freeze, PhantomData<Task>: Freeze, *const ArcInner<Mutex<Vec<u64>>> is !null: Freeze, PhantomData<ArcInner<Mutex<Vec<u64>>>>: Freeze, *const ArcInner<Mutex<JoinSlot<Result<(), Error>>>> is !null: Freeze, PhantomData<ArcInner<Mutex<JoinSlot<Result<(), Error>>>>>: Freeze,

§

impl Move for Ext

§

impl Send for Ext
where RawVecInner: Send, BoxRaw<dyn FnMut(Bot) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> + Send>: Send, Arc<Mutex<Shared>>: Send, BoxRaw<dyn Future<Output = ()> + Send>: Send, Waker: Send, BoxRaw<dyn FnOnce(String) + Send>: Send, Arc<Mutex<Vec<u64>>>: Send, Arc<Mutex<JoinSlot<Result<(), Error>>>>: Send,

§

impl Unpin for Ext
where *const u8 is !null: Unpin, Box<dyn FnMut(Bot) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> + Send>: Unpin, Arc<Mutex<Shared>>: Unpin, Box<dyn Future<Output = ()> + Send>: Unpin, Waker: Unpin, Box<dyn FnOnce(String) + Send>: Unpin, Arc<Mutex<Vec<u64>>>: Unpin, Arc<Mutex<JoinSlot<Result<(), Error>>>>: Unpin,

§

impl UnsafeUnpin for Ext
where *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const dyn FnMut(Bot) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> + Send is !null: UnsafeUnpin, PhantomData<dyn FnMut(Bot) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> + Send>: UnsafeUnpin, *const ArcInner<Mutex<Shared>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Shared>>>: UnsafeUnpin, PhantomData<Task>: UnsafeUnpin, *const ArcInner<Mutex<Vec<u64>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Vec<u64>>>>: UnsafeUnpin, *const ArcInner<Mutex<JoinSlot<Result<(), Error>>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<JoinSlot<Result<(), Error>>>>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V