ChatCommandAPI
A Lethal Company modding API for easy creation of Minecraft-style Chat Commands
模組介紹 作者原文
This is a mod for developers
You probably won't find much on this page as a player.
Instead, try checking out the page of the mod that brought you here
If you encounter any issues while playing or creating a mod with this API,
or you would like to request a new feature,
please open an issue on GitHub.
This mod is open-source, you can contribute to it
by opening a pull request
Getting started
- Include API in your project
Simply run dotnet add package baer1.ChatCommandAPI or add the following line to your csproj file:
Additionally, you should reference this mod in both your main plugin class and your manifest.json:
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency(ChatCommandAPI.MyPluginInfo.PLUGIN_GUID)]
public class ExampleMod: BaseUnityPlugin;
{
"dependencies": [
"BepInEx-BepInExPack-5.4.2305",
"baer1-ChatCommandAPI-1.2.1"
]
}
- Create Command Subclass
Simply create a non-static class that inherits the ChatCommandAPI.Command class.
The Invoke method contains the logic that will be run.
The default command name is the class name you assign to it, ExampleCommand in the example below.
You can run this command through the in-game chat. \(See below for changing it\)
This mod is case-insensitive \(/eXaMpLeCoMmAnD is valid\)
Example:
using ChatCommandAPI;
namespace ExampleMod;
public class ExampleCommand: Command
{
public override void Invoke(string args)
{
// Put your code here
return;
}
}
- Instantiate the Subclass _once_
using BepInEx;
namespace ExampleMod;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency(ChatCommandAPI.MyPluginInfo.PLUGIN_GUID)]
public class ExampleMod: BaseUnityPlugin
{
private void Awake()
{
_ = new ExampleCommand();
}
}
- Done!
You can now use the command /examplecommand in-game to run the function defined
in Step 2
Advanced usage
Customizing the command
You can overwrite several properties of the Command class to customize your command:
using ChatCommandAPI;
using ChatCommandAPI.Utils;
namespace ExampleMod;
public class ExampleCommand: Command
{
public override string Name => "My command"; // Display name
public override string Description => "Prints Hello World [amount] times"; // Short description
public override string Command => "examplecommand"; // Primary command
public override string Aliases => ["ex", "example"]; // Aliases (an alias may become the primary to avoid duplicates)
public override string Syntax => ["[amount]"]; // All valid syntaxes for this command (only for help, not validated)
public override bool Hidden => false; // Whether to hide this command from the help list (useful for debugging, default: false)
public override void Invoke(string args)
{
ushort amount = 1;
if (args.Length > 0)
if (!ushort.TryParse(args, out amount))
throw new InvalidArgumentsException(); // Report "Invalid arguments"
while (amount > 0)
{
Chat.Print("Hello, World!");
amount--;
}
}
}
The above results in the following being displayed on the help list:
My command - Prints Hello World [amount] times
/examplecommand [amount]
Server commands
Server commands are commands that anyone on the server can use, as long as the host has them installed.
They are very similar to the client commands.
Example (simple implementation of the ShipLoot mod as a server command):
using System.Linq;
using GameNetcodeStuff;
using UnityEngine;
using ChatCommandAPI;
using ChatCommandAPI.Utils;
namespace ExampleMod;
public class ShipLoot: ServerCommand
{
public override string Description => "Displays the value of all loot on the ship";
public override void Invoke(
PlayerControllerB caller, // Player that sent the command
string args
)
{
// We need the ship to calculate the value of the loot on it
var ship = GameObject.Find("/Environment/HangarShip");
if (ship == null)
throw new CommandException("Ship not found"); // Report "Ship no
安裝與使用 安裝:BepInEx 外掛
- 本站下載的壓縮包就是模組本體,通常是 BepInEx 外掛(解壓後得到 .dll 或外掛資料夾)。
- 建議流程:用社群模組管理器(如 r2modman)選中本遊戲後匯入該模組,管理器會自動裝好 BepInEx 前置並處理依賴。
- 手動流程:先在遊戲目錄裝好 BepInEx 載入器,再把解壓出的 .dll 或外掛資料夾放進 BepInEx/plugins/。
- 啟動遊戲,BepInEx 控制台會列印載入日誌;確認這個模組出現在日誌裡即為安裝成功。
帶「前置依賴」的模組需要一併安裝,否則不會載入。功能開關通常在遊戲內或外掛自帶的設定檔裡調整。
下面這些內容需要先安裝,否則該模組不會生效: