Create Commands

On this page we will explain how to create commands.

Creating commands is quite simple in Onsharp because you don't need to care about type converting and how many parameters the player entered. First we will explain some basic concepts of the command handling of Onsharp.

String Concatenation and Greedy Args

Strings in command executions are really problematic. The command api of Onset is working with arguments which will be split by any space, so if you try to enter arguments which are like messages containing spaces, you need to care about them because this messages will be split into pieces. Onsharp offers a built in string concatenation algorithm which will join string parts together, if their start and end with quotes.

This message: "Hello World" is than just one string and not two.

Parameter Converting

One big problem in the command handling process is the handling of the parameter types. C# is a strongly typed language, more typed than LUA so you need to care about the types. The old way would be to let you, the developers, care about the converting of these types, but Onsharp is better and will do the converting for you. Therefore you just need to define your command handler method signature and the converting will be automatically done by Onsharp.

Define Commands

Defining a command is really simple: Just create your handling method of the command - the first argument must be the player - and mark the method with the command attribute, which also defines the command name, Onsharp will do the rest:

using Onsharp.Commands;
using Onsharp.Entities;
using Onsharp.Plugins;

namespace Tutorial
{
    public class PluginMain : Plugin
    {
        public override void OnStart()
        {
             //Your start up logic here
        }
        
        public override void OnStop()
        {
            //Your stop and clean up logic here
        }  
        
        [Command("hello")]
        public void OnHelloCommand(Player player)
        {
            player.SendMessage("Hallo, " + player.Name + "!");
        } 
    }
}

Adding Command Parameters

Now, that you saw, how to create a really simple command, without any parameters, we will show you, how to create one with parameters:

using Onsharp.Commands;
using Onsharp.Entities;
using Onsharp.Enums;
using Onsharp.Plugins;

namespace Tutorial
{
    public class PluginMain : Plugin
    {
        public override void OnStart()
        {
             //Your start up logic here  
        }
        
        public override void OnStop()
        {
            //Your stop and clean up logic here
        }  
        
        [Command("vehicle")]
        public void OnHelloCommand(Player player, VehicleModel model)
        {
            Vehicle vehicle = Server.CreateVehicle(model, player.GetPosition());
            vehicle.SetPassenger(player, 0);
        } 
    }
}

The converting of the enum will be done by Onsharp. Onsharp will transform the enum via the name of the enum or the value of it. The user or the developer does not need to care about it.

Registering Commands

By default, any entry points as well as the plugin main class are registered for the command handling. But if you want to make a class, which is neither the plugin main class nor an entry point, can be registered manually. Therefore you just need to call either Server.RegisterCommands(new CommandHolderClass()) or Server.RegisterCommands<CommandHolderClass>() both of them can only be called from an entry point class. Please keep one thing in mind: Manually registering of commands with the second method will only work with static command handling methods, and with the first one only with non-static ones.

Predefinied Converters

Here is a list of predefinied converters:

  • Player Converter: Converts the given string by the player's name or SteamID64

  • Enum Converter: Converts the given string by the name or the value of the enum

  • Basic Converter: Converts the given string to the basic C# data types

Command Failure

If you want to handle command failure, like a player entered too less parameters you can do that by using the custom event "CommandFailure" with the player as the first argument, the Onsharp.Commands.CommandFailure enum as second, the entered line as the third and the command name as the fourth argument. With this event you can handle your own command failure like sending the player a message or something else.

Last updated