Files
minecraft-bot/README.md
T

362 lines
13 KiB
Markdown

# Mosfet Minecraft Bot
A Minecraft 26.3 bot written in Python that uses an offline-mode account to
play. The connection path targets protocol 777; older Minecraft protocol
versions are not supported.
The original bot included wood/sand/crop farming, villager trading, eating,
sleeping, and threat avoidance. On the current 26.3 runtime, path following,
crop harvest/replant, tree harvesting/log farming, sand gathering/farming, and
loiter/threat avoidance are migrated; other legacy jobs remain unavailable
until ported to protocol 777.
## Requirements and setup
- Python 3.11+
- The locally updated pyCraft checkout at `../pyCraft`
From the repository root, create and activate a virtual environment, then
install the connection-mode dependencies:
```
$ uv venv env
$ source env/bin/activate
$ uv pip install --python env/bin/python astar flask flask-cors munch watchdog -e ../pyCraft
```
## Running
Run the bot directly with Python. For the Protospace offline-mode server:
```
$ SERVER=127.0.0.1 python mosfet.py
```
`mosfet.py` defaults to that server, player name `mosfet1`, and the current
AuthMe password. Override `SERVER`, `PORT`, `PLAYER_NAME`, or `GAME_PASSWORD`
through environment variables as needed. Microsoft authentication is not used.
The dashboard is served at `/` (normally `http://localhost:3300`) and can be
rebuilt with `npm --prefix web_interface run build`. It uses bundled Minecraft
item icons where available, then fetches and caches missing textures from the
26.3 asset CDN. If no texture is available, it displays a two-letter
placeholder.
Inventory item icons are from https://mcitemgallery.com/
Python edits under `mosfet/` are hot-reloaded in place; the player connection is
kept alive. Edits to connection/bootstrap files are reported as deferred until
the next manual start, rather than triggering an automatic reconnect.
The 26.3 client now handles AuthMe, chat commands, chunk/world-state caching,
modern block/item/entity data, entity tracking, inventory/component sync,
path-following movement (`1goto x y z`), inventory draining (`1drain`),
sign-directed item-specific and general chest caching (`1cache`, also run by
crop farming at the inventory threshold), bed sleeping (`1sleep`, or `1sleep new` to place a bed;
also run by crop farming at night), food use (`1eat`, also run as needed by
crop farming), the crop harvest/replant loop (`1farm crops`), tree/log farming
(`1farm logs`, with `1farm wood` as an alias), sand gathering/farming
(`1gather sand`, `1farm sand`), and death-screen respawning (`1respawn`).
Other inventory/window actions and resource-farming/trading jobs remain disabled
until their 26.3 packets are migrated.
The default server is `127.0.0.1`; set `SERVER` explicitly only when you
intentionally want another server.
## Usage
[How to Cache Items](docs/cache_items.md)
[How to Grab Supplies](docs/grab_supplies.md)
## Signs
The bot reads both the front and back of loaded signs. Use `!signs [distance]`
to inspect nearby text and coordinates; the default distance is 32 blocks. Sign
matching is case-insensitive. For bot-addressed signs, the first word must be
the bot's full name (for example, `mosfet1`) or its group name (for example,
`mosfet`, which addresses bots ending in `1`, `2`, or `3`). A colon after the
address is optional.
### Sand origin
Address a sign with `sand origin` to choose the search center for `1gather sand`
or `1farm sand`:
```
mosfet sand origin
```
The bot uses the nearest matching loaded sign within 200 blocks. A full bot
name such as `mosfet1` can be used instead of the group name. Sand is harvested
in safe 3x3x1 sections. After each section, pickup/cache/food/sleep maintenance
runs before the job starts the next one. If no matching sign is found, the job
keeps its previous loaded origin, or uses the bot's current block position when
no loaded origin is set.
### Cache labels
Put one or more item-category labels on a sign next to a chest. A standing
sign can be placed on top of the chest, or a wall sign can be attached to a
horizontal side. If both a side-adjacent chest and a chest directly below
the sign are nearby, the side chest takes priority. Otherwise, if no chest is
in those positions, the nearest chest(s) within two blocks are associated.
Double chests are treated as one container.
Each recognized line adds items to the chest's accepted categories:
- `Seeds` — `beetroot_seeds`, `melon_seeds`, `pumpkin_seeds`,
`torchflower_seeds`, `wheat_seeds`, and `pitcher_pod`.
- `Wheat` — wheat only; wheat seeds are not included.
- `Logs` — every log item in the current 26.3 data, including stripped logs,
wood, stems, and hyphae.
- `Oak Logs` (and labels such as `Spruce Logs`) — that tree's log family,
including stripped and wood forms, plus stems/hyphae where applicable.
- `Saplings` — all tree saplings and propagules in the current 26.3 data,
including the mangrove propagule.
Labels ignore case and surrounding spaces/colons. During a cache pass, labeled
chests within 105 blocks are considered for matching inventory items. For
example:
```
Seeds
Logs
Saplings
```
### General cache chests
To make a normal chest a general-purpose cache destination, put `cache` after
the bot or group name on its sign:
```
mosfet cache
mosfet1 cache
```
The sign can be on top of or attached to the chest, using the same chest
association rules as item labels. General cache chests within 105 blocks are
used only after all matching item-specific labeled chests, even if a general
chest is closer. Unmarked trapped chests are no longer discovered or used for
caching. If no signed destination is usable, Mosfet can still place a normal
chest from its inventory.
### Supply chests
Put the sign on top of or attach it to the supply chest, then address it with
the bot or bot-group name followed by `supplies`:
```
mosfet1 supplies
```
The address and `supplies` may also be on separate sign lines. The bot searches
within 80 blocks and takes only supplies requested by its current job; the
sign itself does not specify item types. For example, the wood/log farming job
can use an addressed supply chest to obtain an axe.
### Throw items at a sign
A throw directive makes a cache pass drop matching inventory stacks near the
sign:
```
mosfet1 throw Logs
mosfet throw Wheat Seeds
mosfet1 throw oak_log
mosfet1 throw Oak Logs
```
Category labels can target groups (`Logs`) or one tree family (`Oak Logs`). An
exact item name such as `oak_log` needs no `minecraft:` prefix; prefixed names
are also accepted. The exact item form matches only that item, while `Oak Logs` includes stripped
and wood forms for that species. Other multiword exact item names can be written
with spaces or underscores. The bot searches for a reachable, standable position
within three blocks with line of sight to the sign, then drops whole stacks of
the requested items. Use this only where dropping those items is intended.
### Work-area boundaries
A bot-addressed sign can limit where work targets are selected:
```
mosfet1 stay within 24 circle
mosfet stay within 12 squares
```
`stay within N` defaults to a circle if no shape is specified. `circle` uses
horizontal Euclidean distance; `square` or `squares` uses the maximum of the
horizontal X/Z offsets. Height does not count toward the limit. The first
non-empty line starts with the address; put one or more `stay within` rules on
that line or later lines. The nearest matching policy sign among loaded signs
is active, and multiple rules on it are all enforced. Work-target searches and
normal work destinations respect the boundary; support trips such as caching
or retrieving supplies have separate routing and return safeguards.
### Safety refuges
Mark a trusted refuge with the bot or group name followed by `safety` or
`refuge`:
```
mosfet1 safety
mosfet refuge
```
When the threat monitor detects a hostile while a job is active, it searches
for an addressed refuge within 80 blocks and flees to a reachable opening near
it. After reaching safety, the bot waits for tracked hostiles to leave its
40-block clearing radius, then waits 10 seconds before resuming the interrupted
job. While waiting, it may eat when hungry and use an existing bed within five
blocks when it is night or storming; refuge mode does not place a new bed.
## Commands
The command prefix character is the last character of the bot's name. For
example, if the bot's name is `mattstack`, then you would issue commands like
`kfarm wood` or `kpos`. This lets you run multiple bots on the same server.
In the following examples, we'll assume the bot's name is `mosfet1`, so commands
would be ran like `1farm wood` or `1pos`.
The exception are the below public commands, they can optionally be prefixed with `!`
and all bots will run the command.
The current 26.3 command path handles public chat and direct messages, replying
in the same channel. Verified commands are `!ping`, `!pos`, `!dist`, `!loaded`,
`!info x y z`, `!safesand x y z`, `!inv` (basic slot/count data), `1cache`,
`1drain`, `1eat`,
`1respawn`, `1sleep`, `1loiter`, addressed path movement such as `1goto x y z`,
`1farm crops`, `1farm logs` (also available as `1farm wood`), `1gather sand`,
and `1farm sand`. The command catalog below is retained from the legacy bot;
other inventory actions, arbitrary block interaction, and the other
farming/trading commands are not yet available on 26.3. Common item
components (names, damage, enchantments, book
contents, and shulker contents) are decoded; unrecognized component types are
marked partial rather than silently discarded.
### Public Commands
These can be ran by anyone, all bots will reply.
`!help` - prints this whole help message to console
`!help [command]` - replies in-game explaining command
`!ping` - replies with "pong"
`!echo [data]` - replies with "data"
`!pos` - replies with position and dimension
`!dist` - replies with your distance from the bot
`!afk` - goes AFK with /afk
`!unafk` - goes not AFK with /afk
`!error` - raises an error
`!inv` - replies and prints current inventory
`!time` - replies with Minecraft world time
`!count [id]` - counts the number of items with that id
`!loaded` - replies with the current loaded area
`!players` - prints the current players
`!players clear` - clears the current player list
`!objects` - prints the current items on ground
`!objects clear` - clears the current object list
`!mobs` - prints the current mobs
`!mobs clear` - clears the current mob list
`!monsters` - prints the current monsters
`!villagers` - prints the current villagers
`!threats` - prints the dangerous monsters within 20 blocks
`!threats [num]` - prints the dangerous monsters within num blocks
`"zzz" or !zzz` - bot does /afk to let others sleep
`!tree` - replies with the closest tree
`!block x y z` - replies what block is at (x, y, z)
`!safesand x y z` - checks whether sand at the coordinate is safe to dig
### Bot-specific Commands
These will only run for the bot they are addressed to. Replace `1` with the last character of the bot's name.
`1respawn` - respawns the bot if it's dead
`1gather wood` - gathers wood from the world
`1gather sand` - gathers sand from the world
`1farm logs` (or `1farm wood`) - farms trees and caches their logs
`1farm sand` - gathers sand, first obtaining a shovel from a supply chest
`1farm wart` - farms netherwart from a certain area
`1farm crop` - farms mature crops from a certain area
`1loiter` - stands still but eats, sleeps, and flees
`1trade` - sells items to villagers to get emeralds
`1stop` - stops the current job and resets bot
`1drop` - drops the current stack its holding
`1select [id]` - moves item with id into main hand
`1dump [id]` - drops all items matching id
`1drain` - drops all items in inventory
`1fill [x] [y] [z] [x] [y] [z]` - fills the cuboid with the block at the first coordinate
`1here` - bot comes to your location
`1goto [x] [y] [z]` - sends the bot to coordinate (x, y, z)
`1close` - closes the current Minecraft window
`1click [slot] [button] [mode]` - clicks the current window
`1use` - use the item it's currently holding
`1interact [entity id]` - interacts with that entity
### Authorized Commands
These dangerous commands can only be ran by the bot owner.
`1print [expression]` - replies with Python eval(expression)
`1exit` - exits the program
## License
This program is free and open-source software licensed under the MIT License.
Please see the `LICENSE` file for details.
That means you have the right to study, change, and distribute the software and
source code to anyone and for any purpose. You deserve these rights. Please take
advantage of them because I like pull requests and would love to see this code
put to use.
## Acknowledgements
Thanks to Isaia, sose, and the devs behind pyCraft.