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
impl Ext
Sourcepub fn new<F, Fut>(main: F) -> Self
pub fn new<F, Fut>(main: F) -> Self
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(())
});Sourcepub fn name(self, name: impl Into<String>) -> Self
pub fn name(self, name: impl Into<String>) -> Self
Name for logs (default "ext").
let ext = ext.name("miner");Sourcepub fn interests(self, interests: Interests) -> Self
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);Sourcepub fn when_done(self, when: WhenDone) -> Self
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);Sourcepub fn auto_respawn(self, on: bool) -> Self
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);Sourcepub fn interrupt_on_teleport(self, on: bool) -> Self
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);Trait Implementations§
Source§impl Behavior for Ext
impl Behavior for Ext
Source§fn interests(&self) -> Interests
fn interests(&self) -> Interests
Source§fn start(&mut self, ctx: &mut TickContext<'_>)
fn start(&mut self, ctx: &mut TickContext<'_>)
Behavior::tick (both see the same events).Source§fn tick(&mut self, ctx: &mut TickContext<'_>)
fn tick(&mut self, ctx: &mut TickContext<'_>)
Source§fn running(&self) -> bool
fn running(&self) -> bool
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)
fn stop(&mut self, reason: &str)
Source§fn raw_packets(&self, version: GameVersion) -> Cow<'static, [i32]>
fn raw_packets(&self, version: GameVersion) -> Cow<'static, [i32]>
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<'_>)
fn after_tick(&mut self, view: &TickView<'_>)
Source§fn configuration_dialog(&mut self, dialog: &Dialog) -> Option<DialogAnswer>
fn configuration_dialog(&mut self, dialog: &Dialog) -> Option<DialogAnswer>
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.