创建 Chat Bots
注意事项
提示
目前,此页面仅包含Chat Bot API的基础知识,这些详细信息足以教您如何制作基本的Chat Bot。 欲了解更多详情,您需要查看 ChatBot.cs和 示例。 此页面将在未来得到改进。
Minecraft 命令行客户端 拥有丰富的 C# API ,允许您创建Chat Bot(即插件),从而帮你实现普通脚本难以完成的复杂自动化任务。
要求
- C# 编程语言的基本知识
- 文本编辑器
如果您不熟悉C# 编程语言,我们建议查看以下资源:
速成教学
更深入的内容:
快速入门
本教程假定你已经具备 C# 的基础知识
提示
我们会在下文混用 Chat Bot 和脚本这两个词
在您安装MCC的同一文件夹中创建一个空文件,并重命名为 ExampleChatBot.cs
粘贴下面的示例代码:
//MCCScript 1.0
MCC.LoadBot(new ExampleChatBot());
//MCCScript Extensions
// The code and comments above are defining a "Script Metadata" section
// Every single chat bot (script) must be a class which extends the ChatBot class.
// Your class must be instantiates in the "Script Metadata" section and passed to MCC.LoadBot function.
class ExampleChatBot : ChatBot
{
// This method will be called when the script has been initialized for the first time, it's called only once
// Here you can initialize variables, eg. Dictionaries. etc...
public override void Initialize()
{
LogToConsole("An example Chat Bot has been initialized!");
}
// This is a function that will be run when we get a chat message from a server
// In this example it just detects the type of the message and prints it out
public override void GetText(string text)
{
string message = "";
string username = "";
text = GetVerbatim(text);
if (IsPrivateMessage(text, ref message, ref username))
{
LogToConsole(username + " has sent you a private message: " + message);
}
else if (IsChatMessage(text, ref message, ref username))
{
LogToConsole(username + " has said: " + message);
}
}
}启动 MCC,连接到服务器并运行: /script ExampleChatBot.cs.
若您的操作无误,控制台日志中应该会显示:[Example Chat Bot] An example Chat Bot has been initialised! 这条消息
Chat Bot的结构
Chat Bot (Script) 的结构如下:
<script metadata>
<chat bot class>Script Metadata是一个有着特殊格式的区域,通过注释来区分C#代码和MCC的专有内容。
每个Chat Bot (Script) 都必须在开头包含这一部分才能运行。
Script Metadata 的格式
//MCCScript 1.0 标志着Script Metadata部分的开始,这行代码必须始终位于第一行,否则Chat Bot (Script) 将不会被加载并报错。
//MCCScript Extensions 标志着 Script Metadata 的结尾,该行代码必须在Chat Bot (Script) 类之前进行定义。
为了让你的Chat Bot (脚本)能够在//MCCScript 1.0和//MCCScript Extensions这两行之间正确加载,你必须实例化你的Chat Bot (脚本)类,并将其传递给MCC.LoadBot函数。
例子
MCC.LoadBot(new YourChatBotClassNameHere());Script Metadata 允许通过: //using <namespace> 和 /dll <dll name>来引入 C# 包和库。
提示
请勿在 // 和关键字之间添加空格
默认已加载以下程序包:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using MinecraftClient;
using MinecraftClient.Mapping;
using MinecraftClient.Inventory;示例:
//using System.Collections.Immutable
//dll MyDll.dll完整示例:
//MCCScript 1.0
//using System.Collections.Immutable
//dll MyDll.dll
MCC.LoadBot(new ExampleChatBot());
//MCCScript ExtensionsChat Bot Class
在Script Metadata部分结束之后,你基本上可以定义任意数量的类,唯一的限制是:你的Chat Bot (脚本)的主类必须继承自ChatBot类。
没有任何强制要求的方法,一切都是可选的。
当 Chat Bot (脚本)首次初始化时 Initialize 将被调用。
你可以在其中初始化变量,例如 字典等
提示
如果是像数据库连接这种资源,最好是在 AfterGameJoined(加入游戏后)里建立,然后在OnDisconnect(断开连接时)里关闭
示例
您可以在GitHub仓库中的Chat Bot和Config目录中找到许多示例 。
C# API
在撰写本文时,C# API 已在尚未合并的分支中进行了更改,因此现在您可以使用ChatBot.cs作为参考。
每种方法都通过标准 C# 文档注释进行了详细注释。
以后,我们将会根据代码中的文档制作一个脚本来自动生成这个部分。
