Skip to content

Auto Command [Hard Dependency]

InsanityGod edited this page Feb 3, 2026 · 1 revision

Attributes

[AutoCommand]

Can be applied on methods to automatically register them as a command in game, keep in mind that for instance methods (so not static) the instance needs to be resolve able through the service provider.

Properties:

  • Name (Method.Name by default): The name of the command.
  • Path (null by default): Path to the command to register this as a sub command of.
  • Side (EnumAppSide.Server by default*): On which side this command should be registered.
  • RequiredPrivelege (Privilege.chat by default): The privilege required to execute this command.
  • RequiresPlayer (false by default): Whether this command can only be run by an actual player.

*This default changes if the method has ICoreClientAPI or ICoreServerAPI as a parameter (you cannot accept these at the same time, unless you specify the Side manually)

Note: keep in mind that client side commands start with . while server side commands start with /

In addition you can describe your command using code documentation (make sure to enable this)

[CommandParameter]

Can be applied to the parameters of an [AutoCommand] method to manually specify how the argument should be collected, if its not present then it will decide this automatically.

Properties:

In addition validation can be done through Validator Attributes

Examples

public static class Commands
{
    /// <summary>Will give you information about the held item stack.</summary>
    /// <example>/Debug HeldItemStack</example>
    [AutoCommand(Path = "Debug")]
    public static ItemStack HeldItemStack([Required(ErrorMessage = "Not holding any item")] ItemStack heldItem) => heldItem;

    /// <summary>Will give you information about the entity you are looking at.</summary>
    /// <example>/Debug Entity</example>
    [AutoCommand(Path = "Debug")]
    public static string Entity([Required(ErrorMessage = "Not looking at an Entity")] Entity entity) => entity.GetName();

    /// <summary>Prints a neat little message about how you think the chance of rain is like today.</summary>
    /// <example>/Forecast 25</example>
    [AutoCommand]
    public static string Forecast(ICoreAPI api, [CommandParameter(ContextualSource = EContextualSource.Caller)] IPlayer callingPlayer, [Range(0, 100)] int chance = 50) => string.Format(
        ForecastText,
        callingPlayer.PlayerName,
        chance,
        api.World.Calendar.PrettyDate()
    );

    public const string ForecastText = "{0} says that there will be a {1}% chance of rain on {2}";
}

image image image image image image

Manual Registration

Auto Commands can also be manually created for adapting them to existing methods. Keep in mind that this means you cannot add [CommandParameter] attributes to the parameters this way, so if you need more control consider creating a wrapper method instead.

Examples

public override void StartServerSide(ICoreServerAPI api)
{
    base.StartServerSide(api);
    
    new AutoCommand(
        AccessTools.PropertyGetter(typeof(IWorldAccessor), nameof(IWorldAccessor.DefaultSpawnPosition)), 
        nameof(IWorldAccessor.DefaultSpawnPosition)
    ).GetOrRegister(api);
}
image

Clone this wiki locally