Repository navigation
Auto Command [Hard Dependency]
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.Nameby default): The name of the command. -
Path(nullby default): Path to the command to register this as a sub command of. -
Side(EnumAppSide.Serverby default*): On which side this command should be registered. -
RequiredPrivelege(Privilege.chatby default): The privilege required to execute this command. -
RequiresPlayer(falseby 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)
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:
-
Provider(EParamProvider.ArgumentParserby default): How the parameter should be provided. -
ContextualSource(EContextualSource.ArgumentParserby default): Allows for parameters to be resolved contextually, if set will overrideProvidertoEParamProvider.ContextualProvider
In addition validation can be done through Validator Attributes
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}";
}

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.
public override void StartServerSide(ICoreServerAPI api)
{
base.StartServerSide(api);
new AutoCommand(
AccessTools.PropertyGetter(typeof(IWorldAccessor), nameof(IWorldAccessor.DefaultSpawnPosition)),
nameof(IWorldAccessor.DefaultSpawnPosition)
).GetOrRegister(api);
}