Repository navigation
API ‐ CommandManager
Register new command with function.
CommandManager.Register(new Command("hub", "Teleport back to hub"), tpHub);
You will also need to create function for every command that will get called on command use. Example below shows function for "hub" command including trasfering to hub World if player isn't there.
private void tpHub(Player player)
{
if (player.CurrentWorld.LevelName != "hub")
{
player.ChangeWorld(Server.GetWorld("hub"), new Vector3(2, -54, -11));
}
else
{
player.Teleport(new Vector3(2, -54, -11));
}
}
Unregister built-in or other registered commands that you don't need.
CommandManager.Unregister("about")
Command can be also initialized using optional permission level and parameters. In example you can see how to register command with permission 0 (anyone can access) and BoolP parameter that means alwaysday command will have true and false options.
CommandManager.Register(new Command("alwaysday", "Locks and unlocks day-night cycle.", 0, new List<Parameter>() { new BoolP("lock", true) }), Action);
You can also have multiple parameters and make more complex commands. Example is showing working clear command implementation using these parameters.
Bool after parameter tells if parameter is optional and command can be executed without this parameter otherwise error will be displayed about invalid command syntax. new IntP("data", true)// true - optional, command can be executed without this value, false - command can't be executed without this parameter
CommandManager.Register(new Command("clear", "Clears items from player inventory.", 0, new List<Parameter>() { new PlayerP("player"), new ItemP("ItemName", true), new IntP("data", true), new IntP("maxCount", true) }), Action);
public static void Action(CommandAction action)
{
var targets = action.GetPlayers();
var item = action.GetItem();
var count = action.GetInt();
var data = action.GetInt();
foreach (var target in targets)
{
target.Inventory.Clear(item);
}
if (targets.Count() > 0)
{
action.Player.SendMessage($"Cleared the inventory of {string.Join(", ", targets.Select(i => i.Username))}");
}
}
Add various parameter types to the command.
If optional bool is true - optional, command can be executed without this value, false - command can't be executed without this parameter.
Add int parameter to the command.
CommandManager.Register(new Command("inttest", "int test command.", 0, new List<Parameter>() { new IntP("int value") }), Action);
public static void Action(CommandAction action)
{
int number = action.GetInt(action, 0); //int value from command
}
Add float parameter to the command.
CommandManager.Register(new Command("floattest", "float test command.", 0, new List<Parameter>() { new FloatP("float value") }), Action);
public static void Action(CommandAction action)
{
float number = action.GetFloat(action, 0); //float value from command
}
Add string parameter to the command.
CommandManager.Register(new Command("stringtest", "string test command.", 0, new List<Parameter>() { new StringP("string value") }), Action);
public static void Action(CommandAction action)
{
string text = action.GetString(action, 0); //string value from command
}
Add Player parameter to the command.
CommandManager.Register(new Command("playertest", "player test command.", 0, new List<Parameter>() { new PlayerP("player value") }), Action);
public static void Action(CommandAction action)
{
List<Player> players = action.GetPlayers(action, 0); //players value from command
}
### Vector3P(string, bool(optional))
Add Vector3 position parameter to the command.
CommandManager.Register(new Command("vec3test", "vec3 test command.", 0, new List() { new Vector3P("vector3 value") }), Action);
public static void Action(CommandAction action) { Vector3 position = action.GetVector3(action, 0); //vector3 value from command }
Add Json parameter to the command.
CommandManager.Register(new Command("jsontest", "json test command.", 0, new List<Parameter>() { new JsonP("json value") }), Action);
public static void Action(CommandAction action)
{
string json = action.GetJson(action, 0); //JSON value from command
}
### EnumP(string, bool(optional))
Add enum parameter to the command.
CommandManager.Register(new Command("enumtest", "enum test command.", 0, new List() { new EnumP("enumvalue") }), Action);
public static void Action(CommandAction action) { string enum = action.GetEnum(action, 0); //enum value from command }
Add bool parameter to the command.
CommandManager.Register(new Command("booltest", "booltest command.", 0, new List<Parameter>() { new BoolP("boolvalue") }), Action);
public static void Action(CommandAction action)
{
bool test = action.GetBool(action, 0); //bool value from command
}
### ItemP(string, bool(optional))
Add item parameter to the command.
CommandManager.Register(new Command("bitemtest", "itemtest command.", 0, new List() { new BoolP("itemvalue") }), Action);
public static void Action(CommandAction action) { Item item = action.GetItem(action, 0); //item value from command }
action.Get...() are functions that returns specific value from CommandAction using index (int) where parameter is located in Command.
For example in here first parameter is new PlayerP("player") then we will also need to read it first so it is var targets = action.GetPlayers() next parameter is new ItemP("ItemName", true) so we will need to read Item next var item = action.GetItem();.
It is really important to keep right order otherwise you will get wrong values or errors because parameters and read functions need to match in right order how command was sent.
CommandManager.Register(new Command("clear", "Clears items from player inventory.", 0, new List<Parameter>() { new PlayerP("player"), new ItemP("ItemName", true), new IntP("data", true), new IntP("maxCount", true) }), Clear.Action);
public static void Action(CommandAction action)
{
var targets = action.GetPlayers(); // first because it is first in List<Parameter>()
var item = action.GetItem(); // secound because it is secound in List<Parameter>()
var count = action.GetInt(); // third because it is third ... so on
var data = action.GetInt();
}
Since you need to keep Get functions in right order, just in case if some parameter can't be read you can use action.GetUnkown() to get it's value in object type or action.Skip() function to skip this parameter and read next one.