Skip to main content

Crate ghast_client

Crate ghast_client 

Source
Expand description

Headless Minecraft Java Edition client for the ghast library.

  • Client: connect (directly or through a SOCKS5 proxy, see ghast_auth::connect), handshake, login (offline; online mode with the microsoft feature), compression and encryption, then the play state: keep-alives, teleports, window confirms, health, respawn, chat.
  • A fixed 50 ms tick loop with vanilla’s tick order (see engine for the details): inbound packets → Behavior::tick (actions written immediately) → rotation → movement → exactly one movement packet (26.1 also: player_loaded once the level is in, player_input, and tick_end closing every tick).
  • A version-agnostic API: behaviours see TickContext and the types of common, never packets. The connection is generic over the protocol version (version::ClientVersion), so each supported version is monomorphised.
  • Packets nobody needs are dropped before they are decoded; a behaviour opts into more with Behavior::interests.

Supported versions: 1.8.9 (protocol 47), 1.12.2 (340), 1.16.5 (754), 26.1 – 26.1.2 (775), 26.2 (776) and 26.3 (777), one cargo feature each (v1_8, v1_12_2, v1_16_5, v26_1, v26_2, v26_3; default: all), and 1.12.1 (338), 1.16.2 (751), 1.16.3 (753) with their row’s feature. What a version can do is its version::Capabilities row (GameVersion::caps). Server dialogs (26.1+) are in dialog. Many bots in one process: Swarm.

use ghast_client::{Account, Behavior, Client, GameVersion, Interests, TickContext};

struct Echo;
impl Behavior for Echo {
    fn interests(&self) -> Interests {
        Interests::CHAT
    }
    fn tick(&mut self, ctx: &mut TickContext<'_>) {
        let lines: Vec<String> = ctx.events().chat.iter().map(|m| m.plain()).collect();
        for line in lines {
            println!("chat: {line}");
        }
        if ctx.is_dead() {
            ctx.respawn();
        }
    }
}

let client = Client::new("localhost:25565".parse()?, GameVersion::V1_8_9, Account::offline("Steve")?);
let end = client.run(&mut Echo).await?;
println!("session ended: {end:?}");

Re-exports§

pub use common::ChatMessage;
pub use common::ClientSettings;
pub use common::Controls;
pub use common::Face;
pub use common::GameMode;
pub use common::Health;
pub use common::Pose;
pub use common::Vec3;
pub use common::Vehicle;
pub use common::WorldTime;
pub use hands::BlockHit;
pub use hands::Button;
pub use hands::Buttons;
pub use hands::PressOutcome;
pub use swarm::BotContext;
pub use swarm::BotError;
pub use swarm::BotHandle;
pub use swarm::ShutdownReport;
pub use swarm::Swarm;
pub use swarm::SwarmBuilder;
pub use version::GameVersion;
pub use ghast_entity as entity;
pub use ghast_inventory as inventory;
pub use ghast_world as world;

Modules§

book
Book and quill editing: what the vanilla book screen sends when the player presses Done (the pages) or signs the book (pages and title), and what it changes in the held stack.
common
The version-agnostic layer: what the client core receives from and sends to the server, independent of the protocol version.
connect
Re-exports of the connection setup types from ghast-auth.
cooldown
Item cooldowns (1.9+): the client’s ItemCooldownManager (1.12.2, 1.16.5; 26.x ItemCooldowns). While an item cools down a right click with it does nothing on the client after its packet (interactItem / useItem answer PASS, interactBlock / useItemOn skip the item’s use on the block), so holding the use key with ender pearls throws one every 20 ticks, not every 4.
dialog
Server dialogs (26.1+): the screens a server opens with show_dialog, and the answers a vanilla client sends for them.
fishing
Fishing: the bot’s own fishing bobber as the vanilla client knows it, and the bite.
hands
The mouse buttons in the world: digging, placing, using items and hitting or using entities, as vanilla’s key handling does it (MinecraftClient.tick: doAttack, doUse, handleBlockBreaking; ClientPlayerInteractionManager: attackBlock, updateBlockBreakingProgress, cancelBlockBreaking, interactBlock, interactItem, attackEntity, stopUsingItem). The 1.8.9 rules were checked against recorded lab packet traces; the modern dig timing and the 26.x packets against the oracle and vanilla captures.
hud
What the vanilla in-game HUD shows (Interests::HUD): boss bars, the title and subtitle with their fade times, the action bar (overlay message) and the tab list’s header and footer, kept and counted down per client tick the way each version’s HUD does it; and the server’s answers to command tab-completion requests (TabCompletion, always decoded: they only arrive after a crate::TickContext::tab_complete).
movement
The per-tick movement packet (ClientPlayerEntity.sendMovementPackets, 1.8 and 1.16) and the stand-in mover used until ghast-physics is integrated.
rotation
Rotation in whole mouse counts.
sign
Sign editing: the sign editor the server opens (open_sign_entity) and the lines the client sends back (update_sign), as the vanilla sign screen does it.
swarm
Many bots in one process: Swarm.
teams
The client scoreboard’s teams on every version (Interests::TEAMS): who is on which team, each team’s prefix, suffix, colour and options, as the vanilla client keeps them from the scoreboard team packets (1.8 scoreboard_team, 1.9 – 1.16.5 teams, 26.x set_player_team).
throw
The expected flight of a projectile the player throws or shoots (crate::TickContext::predict_throw): the launch of the item (shootFromRotation and its per-version forms, without the random spread) and then the projectile’s own ticks as the server runs them, both from the version’s oracle-proven physics (ghast_physics::<version>::{launch, projectile}), until the first block hit.
version
Per-version glue between the generated packets of ghast-protocol and the common layer.

Structs§

BlockPos
A block position. x/z must fit 26 signed bits, y 12 signed bits to be encodable.
ClickTicket
A click that was sent: match it with TickEvents::inventory’s clicks on a later tick (window id and action number, as the server’s confirm carries them).
Client
A configured client: where to connect, which version, which account, and how.
Idle
A behaviour that does nothing (the player stands still).
Interests
A set of optional packet groups (see the associated constants).
ProfileKeys
A profile key pair: what the Minecraft services issue for signed chat (valid for about two days; vanilla fetches a new one once ProfileKeys::due_refresh).
RawInbound
A clientbound play packet copied for the behaviour (Behavior::raw_packets).
TeleportEvent
A server teleport / correction (position) with the poses around it.
TickContext
The behaviour’s view of the client during one tick.
TickEvents
What happened since the behaviour’s previous tick.
TickView
A read-only view after the movement step (Behavior::after_tick).

Enums§

Account
The account a client logs in with.
ClickRefused
Why TickContext::click (or opening / closing a window) sent nothing.
ClientError
A session failed.
ForceStepPosition
Grim compatibility (not vanilla): when to send the position on a tick whose move stayed under vanilla’s 0.03-block resend threshold (every version; on 26.x also bounce landings on slime and beds). See crates/ghast-physics/GRIM_NOTES.md.
SessionEnd
How a session that reached the play state ended without an error.

Constants§

TICK
Length of one client tick.

Traits§

Behavior
A bot behaviour: the code that decides what the player does each tick.
WorldView
Block access for behaviours, independent of the protocol version.