CraftingTable.py
CraftingTable.py 是一个 Fabric 模组,允许你通过 Python 脚本以事件驱动的方式控制 Minecraft。它受 discord.py 启发,让你能够异步处理服务器事件并直接从 Python 执行命令。
⚠️ 注意: 这是一个早期开发版本(v0.0.2)。未来将添加许多功能。
? 从 v0.0.1 升级?
事件系统和库文件发生了重大更改。在更新之前,请阅读迁移指南(向下滚动此页面或访问 GitHub),以确保你的脚本继续正常工作。
要求
要使用 CraftingTable.py,需要以下软件:
- Fabric Loader: 0.17.2 或更高版本
- Fabric API: 0.97.3 或更高版本
- Java: 17 或更高版本
- Python: 推荐 3.11(其他版本也可能可用)
确保在运行模组之前安装了这些依赖项。
事件系统
现在使用 @mc.event 装饰器注册事件。这取代了旧的函数命名约定,提供了更好的灵活性和控制力。
基本语法
@mc.event("event_name", priority=1)
async def your_function_name(parameters):
...
event_name: 要监听的事件名称。
priority(可选):定义执行顺序的整数。
可用事件
| 事件名称 |
描述 |
参数 |
"player_join" |
当玩家加入时触发 |
player — 玩家名称 (str) |
"player_leave" |
当玩家离开时触发 |
player — 玩家名称 (str) |
"chat" |
当发送聊天消息时触发 |
message — 消息内容 (str) type — 消息详情 (str) player — 玩家名称 (str) |
"item_use" |
当玩家使用物品时触发 |
world — 世界类型 (str) item_id — 物品 ID (str) count — 物品数量 (int) player — 玩家名称 (str) hand — 主手或副手 (str) nbt — NBT 数据 (str,可选,如果物品没有 NBT 则设为 None) |
提供的函数和变量
| 函数 / 变量 |
描述 |
mc.cmd(command: str) |
实时执行服务器命令。示例:mc.cmd("give player minecraft:apple 5") |
mc.log(message: str) |
将消息打印到服务器日志。示例:mc.log("Player joined") |
管理命令
- /ctpy reload: 重新加载你的
mod.py 并立即应用更改。
- 注意:如果你为游戏启动时不存在的某个事件添加了装饰器,则需要完全重启游戏才能在 Fabric 端注册监听器。
入门指南
首次启动模组后,会在 Minecraft 游戏目录中生成以下文件:
config/craftingtablepy/
├─ mod.py ← 在此处编写你的 Python 事件脚本
├─ craftingtable.py ← 用于导入的 Python 模块,包含 mc.cmd 和 mc.log
编辑 mod.py 来定义你的事件处理器。例如:
import craftingtable as mc
@mc.event("player_join")
async def welcome_player(player):
mc.cmd(f"title {player} actionbar Welcome, {player}!")
mc.cmd(f"give {player} minecraft:apple 5")
@mc.event("chat", priority=1)
async def greeting(message, type, player):
if message.lower() == "hello":
mc.cmd(f"say Hello, {player}!")
在游戏中,运行 /ctpy reload 以应用你的更改!
使用方法
- 像往常一样安装 Fabric 模组。
- 启动 Minecraft 以生成
mod.py 和 craftingtable.py。
- 编辑
mod.py 来定义你的 Python 脚本。
- 启动服务器,你的脚本将自动处理事件并执行命令。
关于重载的技术说明
为了确保所有事件都能动态重载,建议在启动时在 mod.py 中包含你计划使用的所有装饰器。即使函数体为空(pass),拥有装饰器也能让 Fabric 端的监听器初始化,从而允许你稍后通过 /ctpy reload 添加逻辑。
迁移指南 (v0.0.1 → v0.0.2)
要升级到 v0.0.2,请仔细遵循以下步骤。仅替换 jar 文件是不够的。
1. 更新模组文件
用新的 CraftingTable.py v0.0.2 版本替换 mods 文件夹中的旧 .jar 文件。
2. 强制重新生成库
内部 Python 库已更新。你必须删除以下文件以允许模组生成最新版本:
- 路径:
/config/craftingtablepy/craftingtable.py
- 注意:如果不删除此文件,你的脚本将无法识别新的装饰器系统。
3. 更新你的脚本 (mod.py)
事件系统已从“命名约定”更改为“基于装饰器”。没有装饰器的旧函数将不再被执行。
所需更改的示例:
| 版本 |
mod.py 中的示例代码 |
| 旧版 (v0.0.1) |
async def on_chat(message, type, player): |
| 新版 (v0.0.2) |
@mc.event("chat")
async def on_chat(message, type, player): |
事件系统
现在使用 @mc.event 装饰器注册事件。这提供了对执行过程的更好控制,并允许使用自定义函数名。
基本语法
@mc.event("event_name", priority=1)
async def your_custom_function_name(parameters):
...
未来计划
- 添加对其他版本的支持。
- 添加更多高级事件类型和 API。
- 提高大型服务器的稳定性和性能。
许可证
MIT
CraftingTable.py
CraftingTable.py is a Fabric mod that allows you to control Minecraft using Python scripts in an event-driven way. Inspired by discord.py, it lets you handle server events asynchronously and execute commands directly from Python.
⚠️ Note: This is an early development version (v0.0.2). Many features will be added in the future.
? Upgrading from v0.0.1?
Significant changes have been made to the event system and library files. Please read the Migration Guide (Scroll down this page or visit GitHub) before updating to ensure your scripts continue to work.
Requirements
To use CraftingTable.py, the following software is required:
- Fabric Loader: 0.17.2 or higher
- Fabric API: 0.97.3 or higher
- Java: 17 or higher
- Python: 3.11 recommended (other versions may also work)
Make sure these dependencies are installed before running the mod.
Event System
Events are now registered using the @mc.event decorator. This replaces the old function-naming convention for better flexibility and control.
Basic Syntax
@mc.event("event_name", priority=1)
async def your_function_name(parameters):
...
event_name: The name of the Minecraft event to listen for.
priority (Optional): An integer defining the execution order.
Available Events
| Event Name |
Description |
Parameters |
"player_join" |
Triggered when a player joins |
player — player name (str) |
"player_leave" |
Triggered when a player leaves |
player — player name (str) |
"chat" |
Triggered when a chat message is sent |
message — message content (str) type — message details (str) player — player name (str) |
"item_use" |
Triggered when a player uses an item |
world — world type (str) item_id — item ID (str) count — item count (int) player — player name (str) hand — main hand or off-hand (str) nbt — NBT data (str, optional, set to None if the item has no NBT) |
Provided Functions and Variables
| Function / Variable |
Description |
mc.cmd(command: str) |
Executes a server command in real-time. Example: mc.cmd("give player minecraft:apple 5") |
mc.log(message: str) |
Prints a message to the server log. Example: mc.log("Player joined") |
Administration Commands
- /ctpy reload: Reloads your
mod.py and applies changes immediately.
- Note: If you add a decorator for an event that was not present when the game started, a full game restart is required to register the listener on the Fabric side.
Getting Started
After first launching the mod, the following files are generated in your Minecraft game directory:
config/craftingtablepy/
├─ mod.py ← Write your Python event scripts here
├─ craftingtable.py ← Python module for import, contains mc.cmd and mc.log
Edit mod.py to define your event handlers. For example:
import craftingtable as mc
@mc.event("player_join")
async def welcome_player(player):
mc.cmd(f"title {player} actionbar Welcome, {player}!")
mc.cmd(f"give {player} minecraft:apple 5")
@mc.event("chat", priority=1)
async def greeting(message, type, player):
if message.lower() == "hello":
mc.cmd(f"say Hello, {player}!")
While in-game, run /ctpy reload to apply your changes!
Usage
- Install the Fabric mod as usual.
- Launch Minecraft to generate
mod.py and craftingtable.py.
- Edit
mod.py to define your Python scripts.
- Start the server and your scripts will automatically handle events and execute commands.
Technical Note on Reloading
To ensure all events can be reloaded dynamically, it is recommended to include all decorators you plan to use in your mod.py at startup. Even if the function body is empty (pass), having the decorator present allows the Fabric-side listener to initialize, enabling you to add logic later via /ctpy reload.
Migration Guide (v0.0.1 → v0.0.2)
To upgrade to v0.0.2, please follow these steps carefully. Simply replacing the jar file is not enough.
1. Update the Mod File
Replace the old .jar file in your mods folder with the new CraftingTable.py v0.0.2 version.
2. Force Regenerate the Library
The internal Python library has been updated. You must delete the following file to allow the mod to generate the latest version:
- Path:
/config/craftingtablepy/craftingtable.py
- Note: If you don't delete this, your scripts will fail to recognize the new decorator system.
3. Update Your Scripts (mod.py)
The event system has changed from "naming convention" to "decorator-based". Old functions without decorators will no longer be executed.
Example of changes required:
| Version |
Example Code in mod.py |
| Old (v0.0.1) |
async def on_chat(message, type, player): |
| New (v0.0.2) |
@mc.event("chat")
async def on_chat(message, type, player): |
Event System
Events are now registered using the @mc.event decorator. This provides better control over execution and allows for custom function names.
Basic Syntax
@mc.event("event_name", priority=1)
async def your_custom_function_name(parameters):
...
Future Plans
- Add support for other versions.
- Add more advanced event types and APIs.
- Improve stability and performance for large-scale servers.
License
MIT
评论 0
登录后参与评分
还没有评论,来抢沙发吧