Skip to main content

Behavior

Trait Behavior 

Source
pub trait Behavior {
    // Required method
    fn tick(&mut self, ctx: &mut TickContext<'_>);

    // Provided methods
    fn name(&self) -> &str { ... }
    fn interests(&self) -> Interests { ... }
    fn raw_packets(&self, version: GameVersion) -> Cow<'static, [i32]> { ... }
    fn start(&mut self, ctx: &mut TickContext<'_>) { ... }
    fn running(&self) -> bool { ... }
    fn after_tick(&mut self, view: &TickView<'_>) { ... }
    fn stop(&mut self, reason: &str) { ... }
}
Expand description

A bot behaviour: the code that decides what the player does each tick.

Behaviours never see packets or versions: they read a TickContext and call its actions. A panic in a behaviour stops that behaviour (its Behavior::stop is called with "panic") but not the connection: the player keeps standing and answering the server.

Required Methods§

Source

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

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

Provided Methods§

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 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).

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 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 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.

use ghast_client::{Behavior, TickContext, TickView};

/// Logs where the server has the player, once a second.
struct Tracker;

impl Behavior for Tracker {
    fn tick(&mut self, _ctx: &mut TickContext<'_>) {}

    fn after_tick(&mut self, view: &TickView<'_>) {
        if view.tick() % 20 == 0 {
            let p = view.sent();
            println!("{} {:?} in {}", view.tick(), p.pos, view.dimension());
        }
    }
}
Source

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

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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<B: Behavior + ?Sized> Behavior for Box<B>

A boxed behaviour is a behaviour (a runner can pick one from its config at run time).

Source§

fn name(&self) -> &str

Source§

fn interests(&self) -> Interests

Source§

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

Source§

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

Source§

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

Source§

fn running(&self) -> bool

Source§

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

Source§

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

Implementors§