Getting started
First bot
In this page you build a bot from an empty Cargo project. It joins a local server in offline mode, says hello in chat, turns a quarter turn every two seconds, jumps once a second, prints the chat it receives and leaves after a minute.
A server to join
Any Minecraft server for one of the supported versions works. The simplest is a vanilla or
Paper server on your own machine with online-mode=false in server.properties, so the bot can
log in with an offline name and no Microsoft account. Note the version the server runs; the bot
must name the same one. Online-mode servers need the microsoft feature and are covered in
Accounts and proxies.
The project
Replace src/main.rs with the program below. The rest of the page goes through it part by part.
use ;
async
Start the server, then cargo run.
Identity
Account::offline("HelloBot") is the player the bot logs in as. The name is checked like
vanilla checks it (1 to 16 characters of A-Z, a-z, 0-9 and _), so it returns a Result.
The UUID is derived from the name, as in vanilla's offline mode.
Connecting and choosing the version
Client::new takes three things: the address, the game version and the account. The address
parses from "host:port". The version is a GameVersion value and must match the server;
ghast does not probe the server for it. Here it is V1_8_9; for a 26.3 server write
GameVersion::V26_3, or parse a string such as "26.3" or "1.16.5". Everything else in the
program stays the same on every version. See Versions.
A Client is only configuration. Nothing connects until you run it, and one Client can run any
number of sessions one after another, which is how the afk example reconnects.
The behaviour
A behaviour is any type that implements Behavior. Only tick is required; the other methods
have defaults.
nameis used in log lines.interestslists the optional packet groups the bot needs. Chat is optional: withoutInterests::CHAT, chat packets are dropped before they are decoded andctx.events().chatstays empty.startruns once, on the first tick the player is in the world, just before that tick'stick. Here it prints the position and sends a chat line.ctx.chatsends a chat line or a command, truncated to the version's limit.tickruns every 50 ms while the player is in the world.
Inside tick the bot reads and acts through the TickContext:
ctx.events().chatholds the chat messages that arrived since the previous tick;plain()turns one into plain text.ctx.is_dead()andctx.respawn()handle the death screen. Theafkexample waits 20 ticks before respawning, because vanilla's death screen enables its buttons after 20 ticks.ctx.set_aim(yaw, pitch)sets where the bot looks. The client turns toward the target in whole mouse counts, at most 14 degrees of yaw per tick by default, so a quarter turn takes several ticks. The target only holds for the tick it is set in, so the bot sets it every tick.ctx.set_controlssets the movement keys. Keys stay held until you change them. The physics turns them into movement exactly as the vanilla client would, so holdingjumpon the ground makes the player jump.ctx.tick()counts ticks since the connection entered the play state.
Why aim and keys are intents, and when actions are sent, is the subject of The tick model.
Running and stopping
client.run_until(&mut bot, shutdown) connects, logs in, runs the tick loop and returns when
the session ends. It ends with a SessionEnd:
Quit(reason)when the behaviour callsctx.disconnect, as this bot does after 1200 ticks;Shutdownwhen theshutdownfuture completes, here on Ctrl-C;Kicked { reason, .. }orServerClosedwhen the server ends it;Transferred { host, port }when a 26.x server transfers the bot and following transfers is off.
SessionEnd is #[non_exhaustive], so a match on it needs a _ arm. Errors such as a refused
connection or a timeout come back as Err(ClientError). If you do not need a shutdown signal,
client.run(&mut bot) is the same without one.
If the behaviour panics, ghast catches it, stops the behaviour and keeps the player standing and answering the server until the session ends.
Next steps
- The tick model explains what happens in one tick and what a behaviour may do in it.
- Async helpers (ext) shows how to write multi-step jobs (walk there, dig this, wait for that message) as straight-line async code instead of a state machine.