Repository navigation
Commands
Commands allow users to interact with your plugin through the server console or other command interfaces.
Each command is represented by a class.
To create a command you must:
- Create a new class
- Inherit from
Command - Apply the
Commandattribute to the class
The Command attribute contains the following properties:
-
CommandName
The primary name used to call the command. -
Aliases
Alternative names that can also trigger the command. -
Description
A short description used by the help command. -
Parameters
A list of parameters used by the command.
These are only displayed in the help command and are not enforced automatically.
You must override the Execute method.
This method will be called whenever the command is executed.
Example:
using DSMOOFramework.Commands;
[Command(
CommandName = "example",
Aliases = ["test", "awesome"],
Description = "My Awesome Example Command",
Parameters = []
)]
public class ExampleCommand : Command
{
public override CommandResult Execute(string command, string[] args, ICommandSender sender)
{
return "My Awesome Example Command";
}
}💡 Note
Similar to Managers and Configs, command classes are automatically created by the framework and support dependency injection.
In the previous example we returned a string, even though the method expects a CommandResult.
This works because CommandResult has an implicit conversion operator, which automatically converts a string into a CommandResult.
A CommandResult contains two properties:
- Message – The text returned to the user
- ResultType – Determines how the result is displayed
Available result types:
public enum ResultType
{
Success,
Error,
NoPermission,
NotFound,
MissingParameter,
InvalidParameter,
}The ResultType affects how the response is displayed:
- It determines the color of the message
- It adds a prefix to the response
These should be used to clearly communicate incorrect command usage or errors.
Here is an example that validates command parameters and returns appropriate result types.
using DSMOOFramework.Commands;
using DSMOOFramework.Logger;
[Command(
CommandName = "example",
Aliases = ["test", "awesome"],
Description = "My Awesome Example Command",
Parameters = ["message"]
)]
public class ExampleCommand(ILogger logger) : Command
{
public override CommandResult Execute(string command, string[] args, ICommandSender sender)
{
if (args.Length == 0)
return new CommandResult
{
Message = "You must specify an message!",
ResultType = ResultType.MissingParameter
};
var message = args[0];
logger.Info(message);
return $"Printed {message} to the console!";
}
}You can also override the PreExecute method.
This method is called before Execute.
Behavior:
- If
PreExecutereturns ResultType.Success,Executewill be called. - If it returns any other result type, the result will be returned immediately and
Executewill not run.
This makes PreExecute useful for:
- validating arguments
- checking permissions
- performing early validation
Example:
using DSMOOFramework.Commands;
using DSMOOFramework.Logger;
[Command(
CommandName = "example",
Aliases = ["test", "awesome"],
Description = "My Awesome Example Command",
Parameters = ["message"]
)]
public class ExampleCommand(ILogger logger) : Command
{
public override CommandResult PreExecute(string command, string[] args, ICommandSender sender)
{
if (args.Length == 0)
return new CommandResult
{
Message = "You must specify an message!",
ResultType = ResultType.MissingParameter
};
// Returns a default success result and checks for permissions
return base.PreExecute(command, args);
}
public override CommandResult Execute(string command, string[] args, ICommandSender sender)
{
var message = args[0];
logger.Info(message);
return $"Printed {message} to the console!";
}
}Next you should take a look at the available managers provided by the framework: