A Python-first Mineflayer SDK with a synchronous, IDE-friendly callback API.
Project description
總覽
minethon 是教學導向的 Python mineflayer SDK。
底層透過 JSPyBridge 驅動 mineflayer,但公開 API 收斂成同步 callback、薄門面、完整 stub — 讓學生不需要先學 Node.js、EventEmitter、asyncio,也能一行一行看懂並自己仿寫。
特色
- 同步命令式 API —
bot.wait_spawn()→bot.move_forward(3)→bot.dig(),全部阻塞、回傳原生型別,學員寫一行看懂一行 - 同步 callback API — 繼承
EventAdaptor、覆寫on_<event>、用bot.bind(...)一次綁完,沒有await也沒有 event loop - 完整型別層 —
bot.pyi由 mineflayer 官方index.d.ts自動生成,IDE hover 顯示中文說明 - 單一事件入口 — 所有事件統一走
EventAdaptor子類別 +bot.bind(...),避免多套 API 並存造成學習負擔 - Class-based handler —
EventAdaptor內附完整型別簽名,IDE 的「Override methods」可一鍵填入正確參數 - Pathfinding — 內建 typed 支援的
mineflayer-pathfinder,bot.pathfinder.goto(...)直接可用 - 顯式版本釘選 — 非內建 plugin 必須傳版本字串,避免 JSPyBridge 在 runtime 偷裝 latest
前置需求
| 項目 | 需求 |
|---|---|
| Python | 3.14+ |
| Node.js | 22+ |
| Minecraft Server | Java Edition |
安裝
./setup.sh
setup.sh 會:
- 跑
uv sync安裝 Python 依賴 - 檢查 Node.js 22+
- 預裝 pinned 的
mineflayer、vec3、mineflayer-pathfinder
Node.js 必須在 PATH 中可用。
setup.sh啟動時會自動檢查。
快速開始
同步寫法(推薦初學者)
直線腳本,一行做一件事,沒有 callback 也沒有 await:
from minethon import create_bot
bot = create_bot(host="localhost", username="pybot")
bot.wait_spawn() # 卡住直到進入世界
bot.move_forward(3) # 往前走 3 格(不用 pathfinder)
bot.dig() # 挖掉正在看的方塊
x, y, z = bot.get_pos()
bot.chat(f"我在 ({x:.0f}, {y:.0f}, {z:.0f})")
完整方法表見 skills/minethon/(也是給 AI 看的接口說明)。
事件寫法
要「反應」聊天、被打、玩家進出等事件時,繼承 EventAdaptor:
from minethon import EventAdaptor, create_bot
from minethon.models import ChatMessage
bot = create_bot(host="localhost", username="pybot")
class Greeter(EventAdaptor):
def on_spawn(self) -> None:
bot.chat("hello")
def on_chat(
self,
username: str,
message: str,
translate: str | None,
json_msg: ChatMessage,
matches: list[str] | None,
) -> None:
if username == bot.username:
return
if message == "quit":
bot.quit("bye")
def on_end(self, reason: str) -> None:
print(f"Disconnected: {reason}")
bot.bind(Greeter())
bot.run_forever()
事件 API
唯一公開寫法:繼承 EventAdaptor、覆寫想要的 on_<event> 方法、用 bot.bind(instance) 綁定。
from minethon import EventAdaptor, create_bot
class My(EventAdaptor):
def on_spawn(self) -> None:
print(f"Spawned as {bot.username}")
def on_chat(self, username, message, *_):
if message == "quit":
bot.quit("bye")
bot = create_bot(host="localhost", username="pybot")
bot.bind(My())
bot.run_forever()
歷史寫法已全部移除,不再是公開 API:
@bot.on("chat")(字串)@bot.on(BotEvent.CHAT)/@bot.once(BotEvent.CHAT)(enum decorator)@bot.on_<event>/@bot.once_<event>(屬性 shortcut)
統一只剩 EventAdaptor 一條路,避免初學者在多套 API 之間迷失方向。
型別與匯入
常用型別可從 minethon.models 匯入:
from minethon.models import Block, ChatMessage, Entity, Item, Player, Vec3
這些是公開型別 shell,實際成員面以 src/minethon/bot.pyi 為準。
版本規則
create_bot(...)內部固定使用 pinned 的mineflayerbot.load_plugin("mineflayer-pathfinder")可省略版本,會用內建 pin- 其他 npm 套件必須顯式版本:
viewer = bot.require("prismarine-viewer", "1.33.0")
tool = bot.load_plugin("mineflayer-tool", "1.5.0", export_key="plugin")
這是刻意設計,用來避免 JSPyBridge 在 runtime 偷裝 latest,確保教學範例在學生環境可重現。
範例
| 範例 | 說明 |
|---|---|
| linear_actions | 同步命令式 API:走路、轉向、挖方塊、回報座標 |
| drasl_auth | 透過自建 Drasl 驗證伺服器連線並回應聊天 |
專案結構
src/minethon/
├── __init__.py # 使用者入口(re-export create_bot / Bot / BotEvent / EventAdaptor / 錯誤類)
├── bot.py # runtime façade:event decorator、plugin loading、版本 guard
├── bot.pyi # 生成的 IDE 型別層(由 scripts/generate_stubs.py 產出)
├── _events.py # 生成的 BotEvent StrEnum
├── _handlers.py # 生成的 EventAdaptor 基底類別
├── _bridge.py # JSPyBridge 封裝:callback thread、emitter 注入、handler 正規化
├── _type_shells.py # 內部型別 shell 實作
├── errors.py # 公開錯誤類(MinethonError、NotSpawnedError、VersionPinRequiredError 等)
├── py.typed # PEP 561 型別支援標記
└── models/ # 可匯入的公開型別 shell
├── __init__.py
└── __init__.pyi
scripts/
├── generate_stubs.py # 從 mineflayer / pathfinder d.ts 生成 bot.pyi / _events.py / _handlers.py
└── format.sh # 一鍵 regen → ruff → pyright → pytest
hover 說明的中文 docstring 直接住在
src/minethon/bot.pyi內;generate_stubs.pyregen 時會從現有.pyi讀回 docstring 再注入新生成的 stub,所以人工編輯不會被沖掉。
開發
一鍵跑完(regen stubs → format → lint → type-check → test):
./scripts/format.sh # 寫回格式修正
./scripts/format.sh --check # 只檢查不寫入(CI 模式)
對應的個別指令:
uv run python scripts/generate_stubs.py
uv run ruff format src scripts tests
uv run ruff check src scripts tests
uv run pyright src/
uv run pytest -m "not integration" --tb=short -q
IntelliJ / PyCharm 使用者注意
uv sync 會以 editable 模式安裝本專案,導致 IDEA 的 Python SDK 將專案目錄同時視為外部 library,可能使整個專案被標記為 excluded。
解法: File → Project Structure → SDKs → 選擇 Python interpreter → Paths 頁籤,移除指向本專案以及本專案 src/ 的路徑,然後 Apply。
貢獻
歡迎 PR 與 Issue!
送出前請確認:
- 遵循現有的程式碼風格與架構慣例(細節見
AGENTS.md) - 通過所有檢查
./scripts/format.sh --check
- 以
feature/your-feature或fix/your-fix命名分支 - 發布 PR 時,目標分支為
dev
授權
本專案採用 GNU Affero General Public License v3.0 授權。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file minethon-0.3.7.tar.gz.
File metadata
- Download URL: minethon-0.3.7.tar.gz
- Upload date:
- Size: 2.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a001af1c1034fd8e39893d5290ffb382ac49e0f1d1b2c85ce57bc5a45cbcafe5
|
|
| MD5 |
44c260f51eb8492339f4c182371e19b5
|
|
| BLAKE2b-256 |
45bd0de84132cf2682eed8c5c2c85c2194c147253eb06bfdae265ce5813b3f6d
|
Provenance
The following attestation bundles were made for minethon-0.3.7.tar.gz:
Publisher:
publish.yml on Hack-the-SDGs/minethon
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
minethon-0.3.7.tar.gz -
Subject digest:
a001af1c1034fd8e39893d5290ffb382ac49e0f1d1b2c85ce57bc5a45cbcafe5 - Sigstore transparency entry: 2043701239
- Sigstore integration time:
-
Permalink:
Hack-the-SDGs/minethon@4812687b9112f2e8db472c407f1f38b0064502b2 -
Branch / Tag:
refs/tags/v0.3.7 - Owner: https://github.com/Hack-the-SDGs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4812687b9112f2e8db472c407f1f38b0064502b2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file minethon-0.3.7-py3-none-any.whl.
File metadata
- Download URL: minethon-0.3.7-py3-none-any.whl
- Upload date:
- Size: 72.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02141b97da2eb4ffb3d6fad808059b13b2e0b3b96c1794b74c2ae21c33fc4d85
|
|
| MD5 |
09d120a065fbee62aff74451d2793d81
|
|
| BLAKE2b-256 |
ce7e9422a4b04a9c1f315aac79d5cc1e1d0b9e8b2e164a446ae2c39a0eb6f6fe
|
Provenance
The following attestation bundles were made for minethon-0.3.7-py3-none-any.whl:
Publisher:
publish.yml on Hack-the-SDGs/minethon
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
minethon-0.3.7-py3-none-any.whl -
Subject digest:
02141b97da2eb4ffb3d6fad808059b13b2e0b3b96c1794b74c2ae21c33fc4d85 - Sigstore transparency entry: 2043701259
- Sigstore integration time:
-
Permalink:
Hack-the-SDGs/minethon@4812687b9112f2e8db472c407f1f38b0064502b2 -
Branch / Tag:
refs/tags/v0.3.7 - Owner: https://github.com/Hack-the-SDGs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4812687b9112f2e8db472c407f1f38b0064502b2 -
Trigger Event:
push
-
Statement type: