You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The general pattern is to subclass CommandScript, override the GetCommands function, and provide a top-level function which calls the constructor. You may also want to define permissions to use the command, so that is included below as well.
Choose a prefix for your command
For the remaining code examples, I'll be using my.
Add new permission
In src/server/game/Accounts/RBAC.h, you'll find enum RBACPermissions. This contains all of the permissions defined for the RBAC system. At the end, you can add your custom permissions (starting at enum value 1000, use a higher value if appropriate). Add RBAC_PERM_COMMAND_MY here, before RBAC_PERM_MAX.
enum RBACPermissions
{
// ... lots of permissions//// IF YOU ADD NEW PERMISSIONS, ADD THEM IN MASTER BRANCH AS WELL! //// custom permissions 1000+ RBAC_PERM_COMMAND_MY = 1000,
RBAC_PERM_MAX
};
Add entrypoint to cs_script_loader.cpp
In src/server/scripts/Commands/cs_script_loader.cpp, define a top-level function called AddSC_my_commandscript.
voidAddSC_my_commandscript();
Call the entrypoint in cs_script_loader.cpp
At the end of the file, there's a function called AddCommandsScripts(). Call the entrypoint at the bottom of the function
voidAddCommandsScripts()
{
// other entrypointsAddSC_my_commandscript();
}
Create the new CommandScript
Create a new file in src/server/scripts/Commands called cs_my.cpp.
Create the CommandScript class
In cs_my.cpp, create a new class called my_commandscript, which is a subclass of CommandScript, having a public constructor, and overriding the GetCommands function. This example uses the new way of providing command functionality, so you'll also need to use the Trinity::ChatCommands namespace.
In the constructor, call the base constructor, passing in the name of the class.
GetCommands() needs to return a ChatCommandTable. Each command is defined by a name, TypedHandler, RBAC permissions, and whether it is exposed to the Console. The TypedHandler function needs to return a boolean value, so we'll return true for now.
For this simple Hello world example, we'll just log something to the console. This will require us to include Log.h in order to use TC_LOG_INFO.
// top of file
#include"Chat.h"
#include"Log.h"// in my_commandscriptstaticboolHandleMyCommand(ChatHandler* handler)
{
TC_LOG_INFO("server", "Hello world from my command.");
returntrue;
}
Provide the implementation of the entrypoint
After the class, provide the implementation of the entrypoint declared in cs_script_loader.cpp
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The general pattern is to subclass CommandScript, override the GetCommands function, and provide a top-level function which calls the constructor. You may also want to define permissions to use the command, so that is included below as well.
Choose a prefix for your command
For the remaining code examples, I'll be using
my.Add new permission
In
src/server/game/Accounts/RBAC.h, you'll findenum RBACPermissions. This contains all of the permissions defined for the RBAC system. At the end, you can add your custom permissions (starting at enum value 1000, use a higher value if appropriate). AddRBAC_PERM_COMMAND_MYhere, beforeRBAC_PERM_MAX.Add entrypoint to cs_script_loader.cpp
In
src/server/scripts/Commands/cs_script_loader.cpp, define a top-level function calledAddSC_my_commandscript.Call the entrypoint in cs_script_loader.cpp
At the end of the file, there's a function called
AddCommandsScripts(). Call the entrypoint at the bottom of the functionCreate the new CommandScript
Create a new file in
src/server/scripts/Commandscalledcs_my.cpp.Create the CommandScript class
In
cs_my.cpp, create a new class calledmy_commandscript, which is a subclass of CommandScript, having a public constructor, and overriding the GetCommands function. This example uses the new way of providing command functionality, so you'll also need to use theTrinity::ChatCommandsnamespace.In the constructor, call the base constructor, passing in the name of the class.
Create the ChatCommandTable and Handler
GetCommands()needs to return aChatCommandTable. Each command is defined by a name, TypedHandler, RBAC permissions, and whether it is exposed to the Console. The TypedHandler function needs to return a boolean value, so we'll returntruefor now.Implement HandleMyCommand
For this simple Hello world example, we'll just log something to the console. This will require us to include
Log.hin order to useTC_LOG_INFO.Provide the implementation of the entrypoint
After the class, provide the implementation of the entrypoint declared in
cs_script_loader.cppCompile
From the root of the repo, run the command you used when you set up the server initially (for example, Compiling the source code from the Linux guide)
Start the worldserver, and test the command
Start your worldserver, and you'll be presented with the TC CLI. Run
.helpand you should see themycommand in the listThen just run
.myand see Hello world printed to the console.Coming soon:
All reactions