diff --git a/README.md b/README.md index d3038df..5279b4c 100644 --- a/README.md +++ b/README.md @@ -1,364 +1,185 @@ # 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. +Mosfet is a Python bot for Minecraft 26.3. It connects with an offline-mode +account over protocol 777. -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. +It can navigate the world, farm crops, trees, sand and nether wart, manage +inventory and chests, sleep and eat, and respond to nearby threats. -## Requirements and setup +## Setup -- Python 3.11+ -- The locally updated pyCraft checkout at `../pyCraft` +Requirements: Python 3.11+ and the sibling `../pyCraft` checkout. -From the repository root, create and activate a virtual environment, then -install the connection-mode dependencies: +From the repository root: -``` -$ uv venv env -$ source env/bin/activate -$ uv pip install --python env/bin/python astar flask flask-cors munch watchdog -e ../pyCraft +```sh +uv venv env +source env/bin/activate +uv pip install --python env/bin/python astar flask flask-cors munch watchdog -e ../pyCraft ``` -## Running +## Run -Run the bot directly with Python. For the Protospace offline-mode server: +For a local offline-mode server: -``` -$ SERVER=127.0.0.1 python mosfet.py +```sh +SERVER=127.0.0.1 PLAYER_NAME=mosfet1 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. +The defaults are `SERVER=127.0.0.1` and `PLAYER_NAME=mosfet1`. Set `PORT` or +`GAME_PASSWORD` through the environment when needed. The bot uses AuthMe; it +does not use Microsoft authentication. -Inventory item icons are from https://mcitemgallery.com/ +The dashboard is normally at . Rebuild its assets with: -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. +```sh +npm --prefix web_interface run build +``` -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. +Python edits under `mosfet/` hot-reload without disconnecting the player. +Connection and startup changes take effect after restarting the bot. -## Usage +Item icons use bundled Minecraft textures where available. Missing textures are +fetched and cached from the Minecraft 26.3 asset CDN. The icon collection is +also credited to [mcitemgallery.com](https://mcitemgallery.com/). -[How to Cache Items](docs/cache_items.md) +## Commands -[How to Grab Supplies](docs/grab_supplies.md) +The command prefix is the last character of the bot's name. For `mosfet1`, use +`1`; for `mosfet2`, use `2`. + +| Command | Action | +| --- | --- | +| `1farm crops` | Harvest and replant mature crops | +| `1farm logs` / `1farm wood` | Harvest trees and cache the logs | +| `1farm sand` | Gather sand and red sand | +| `1farm wart` | Farm nether wart | +| `1gather wood` | Gather wood | +| `1gather sand` | Gather sand and red sand | +| `1sleep [new]` | Sleep; `new` requests a newly placed bed | +| `1eat` | Eat when needed | +| `1cache` | Run an inventory cache pass | +| `1drain` | Drop inventory stacks | +| `1goto x y z` | Navigate to coordinates | +| `1respawn` | Respawn after death | +| `1loiter` | Stand by while handling food, sleep and threats | +| `1stop` | Stop the current job | + +Public commands include `!ping`, `!pos`, `!dist`, `!signs [distance]`, and +`!safesand x y z`. Use `!help` in chat for the full command list. ## 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 any digit from `1` through `9`). 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 and red sand -are harvested in safe 3x3x1 sections; pickup/cache/food/sleep maintenance runs -between sections. 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. -- `Sand` — sand only. -- `Red Sand` — red sand only. -- `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. +Signs are read on both sides. Use `!signs [distance]` to inspect nearby text; +the default search distance is 32 blocks. Matching ignores case. Bot-addressed +signs start with a full bot name (`mosfet1`) or group name (`mosfet`). A group +name addresses bots ending in digits 1–9; the colon after an address is +optional. ### Work-area boundaries -A bot-addressed sign can limit where work targets are selected: +A work sign can limit where a bot selects work targets: -``` +```text 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. +The default shape is a circle. `square` and `squares` use the larger horizontal +X/Z offset; height does not count. The nearest matching policy sign among +loaded signs is active, and all rules on it apply. Work destinations must be +inside the boundary; the path to them may cross outside it. After a support trip +exceeds the boundary by more than 40 blocks, the bot returns to a reachable +position inside before finishing the task. + +### Supply chests + +Address a chest with `supplies` to make its contents available to jobs: + +```text +mosfet1 supplies +``` + +The address and `supplies` can be on separate lines. The bot searches within +80 blocks and takes only items requested by its current job. For example, +wood-farming can retrieve an axe from a supply chest. + +### Cache chests and labels + +Attach a sign to a chest or place it on top. A side-adjacent chest takes +priority over one below the sign; double chests count as one container. + +Use `cache` for a general-purpose destination: + +```text +mosfet cache +``` + +General cache chests are searched within 105 blocks, after item-specific +chests. If no signed destination is usable, the bot can place a chest from its +inventory. + +Item-category labels on a sign direct matching items to that chest: + +| Label | Items | +| --- | --- | +| `Seeds` | Crop seeds and propagules | +| `Wheat` | Wheat only; not wheat seeds | +| `Sand` / `Red Sand` | The matching sand type | +| `Logs` | All log items, including stripped and wood forms | +| `Oak Logs`, `Spruce Logs`, etc. | That tree's log family | +| `Saplings` | Saplings and propagules | + +Labels can be combined on one sign. They ignore case and surrounding spaces or +colons. See [How to Cache Items](docs/cache_items.md) for more detail. + +### Throw directives + +A `throw` directive drops matching inventory stacks near a sign: + +```text +mosfet1 throw Logs +mosfet throw Wheat Seeds +mosfet1 throw oak_log +``` + +Use category labels, tree-family labels, or an exact item name with or without +the `minecraft:` prefix. The bot looks for a reachable position within three +blocks, with line of sight to the sign. + +### Sand origin + +Use `sand origin` to set the center for `1gather sand` and `1farm sand`: + +```text +mosfet sand origin +``` + +The nearest matching loaded sign within 200 blocks is used. Without one, the +job keeps its previous origin or starts from the bot's current position. +Sand and red sand are collected in safe 3×3×1 sections. ### Safety refuges -Mark a trusted refuge with the bot or group name followed by `safety` or -`refuge`: +Mark a refuge with `safety` or `refuge`: -``` +```text 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. +When a hostile is detected during a job, the bot searches for an addressed +refuge within 80 blocks. It waits for tracked hostiles to leave the 40-block +area, then waits 10 seconds before resuming the job. While waiting, it may eat +or use a nearby bed at night or during a storm; refuge mode does not place a +new bed. -## Commands +## More information -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. +- [How to Cache Items](docs/cache_items.md) +- [How to Grab Supplies](docs/grab_supplies.md) -In the following examples, we'll assume the bot's name is `mosfet1`, so commands -would be ran like `1farm wood` or `1pos`. +## License and acknowledgements -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 and red sand from the world - -`1farm logs` (or `1farm wood`) - farms trees and caches their logs - -`1farm sand` - gathers sand and red 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. +Licensed under the MIT License; see [LICENSE](LICENSE). Thanks to Isaia, sose, +and the pyCraft developers.