26.04.2015, 12:03
Quote:
Hi nighthammer, that's very easy:
You can create a method and tag it with [Command("commandname")]. This will create a command called /commandname and the method underneath it will be used to handle the command. Example: PHP код:
The arguments of the method will tell SampSharp what kind of arguments the command accepts! The first argument, player, will always be needed. You can add integers, strings, whatever as arguments and use them in the command! In the example above, string name is used to ask for the group name that the player wants information about. If incorrent command arguments are given by a player, SampSharp will automatically correct the player by sending Usage: /group [name]. It cannot be simpler! If you have any other questions, i'm more than happy to help you out. |
Defining and detection of parameter types
By default SA-MP# looks at the parameters of the method to determine the parameters of the command. The parameter types it can automatically detect are: int, float, string*, GtaPlayer or any subclass of GtaPlayer and any enumerator(e.g. VehicleModelType).
Notice that by default SA-MP# thinks that you want a single word as argument when a method accepts a string:
PHP код:
[Command("tell")]
public static void TellCommand(Player sender, Player receiver, string message)
{
//..
}
If you want the string parameter to accept text instead of a single word, you need to mark it as a text parameter:
PHP код:
[Command("tell")]
[Text("message")]
public static void TellCommand(Player sender, Player receiver, string message)
{
//..
}
PHP код:
public class DetectedCommand : Command
{
//...
public static Func<Type, string, ParameterAttribute> ResolveParameterType { get; set; }
//...
// default value:
ResolveParameterType = (type, name) =>
{
if (type == typeof (int)) return new IntegerAttribute(name);
if (type == typeof (string)) return new WordAttribute(name);
if (type == typeof (float)) return new FloatAttribute(name);
if (typeof (GtaPlayer).IsAssignableFrom(type)) return new PlayerAttribute(name);
return type.IsEnum ? new EnumAttribute(name, type) : null;
};
}
PHP код:
[Command("tell")]
[Player("receiver")]
[Text("message")]
public static void TellCommand(Player sender, Player receiver, string message)
{
//..
}
There are two places where you can define you commands: a static method anywhere in your code, or in your player class. I haven't checked this but AFAIK the command method must be public!
-player class
If you have a Player class which is a sub class of GtaPlayer, you can put the commands straight in there:
PHP код:
class Player : GtaPlayer
{
//...
[Command("help")]
public void HelpCommand()
{
//...
}
//...
}
-a static method
Can be placed in any public class. Accepts a Player as first parameter which is the sender
PHP код:
public class AnyClass
{
[Command("help")]
public static void HelpCommand(Player sender)
{
//...
}
}
If you create admin commands, you obviously do not want any odd player to be able to use the command.
In the Command attribute you can specify various properties, including the "PermissionCheckMethod".
PHP код:
public class AnyClass
{
public static bool IsAdmin(Player player)
{
return player.AdminLevel > 0;
}
[Command("adminhelp", PermissionCheckMethod="IsAdmin")]
public static void AdminHelpCommand(Player sender)
{
//...
}
}
// or
class Player : GtaPlayer
{
// ...
public bool IsAdmin()
{
return AdminLevel > 0;
}
[Command("adminhelp", PermissionCheckMethod="IsAdmin")]
public void AdminHelpCommand()
{
//...
}
// ...
}
If you return false in the permission check method, SA-MP# will not call the command. If you return true, it will.
command groups
If you don't want to use command groups, skip this chapter
You can group commands together. If you, for example, want to use the following commands structure, this can be helpful for you:
Код:
/help /player menu -or- /p menu -or- /menu /player status -or- /p status /admin player kick [player] -or- /admin p kick [player] -or- /a player kick [player] -or- /a p kick [player] -or- /kick [player] /admin player ban [player] -or- /admin p ban [player] -or- /a player ban [player] -or- /a p ban [player] /admin serverstatus -or- /a serverstatus /admin restartserver -or- /a restartserver
PHP код:
CommandGroup.Register(string name, string alias = null, CommandGroup parent = null);
You can implement all commands listed above like this:
(I'm not checking for permissions here, just showing how command groups work)
PHP код:
//Somewhere near you initialization code...
CommandGroup.Register("player", "p");
var admin = CommandGroup.Register("admin", "a");
CommandGroup.Register("player", "p", admin);
// In some class...
[Command("help")]
public static void Help(Player s) {}
[Command("menu", Shortcut="menu")]
[CommandGroup("player")] // specify the group this command is in
public static void PlayerMenu(Player s) {}
[Command("status")]
[CommandGroup("player")]
public static void PlayerStatus(Player s) {}
[Command("kick", Shortcut="kick")]
[CommandGroup("admin", "player")]
public static void AdminPlayerKick(Player s, Player kickee) {}
[Command("ban")]
[CommandGroup("admin", "player")]
public static void AdminPlayerBan(Player s, Player banee) {}
[Command("serverstatus")]
[CommandGroup("admin")]
public static void AdminServerstatus(Player s) {}
[Command("restartserver")]
[CommandGroup("admin")]
public static void AdminRestartserver(Player s) {}
In the example above, I've given all command groups aliases and given the menu and kick commands a shortcut.
It is also possible to give a command an alias:
PHP код:
[Command("kick", Alias="kick")]
If you do use command groups, there is. For example, you have The following command:
PHP код:
[Command("b", Alias="c", Shortcut="d")]
[CommandGroup("a", "e")]
public static void abc(Player x){}
/a b
/a c
/e b
/e c
/d
As you can see, if you decide to call the command using the shortcut, you do not need to type in the command group. If you decide to call the command using the alias, you still need to enter the command group(or an alias of the command group)
Ignore case
By default, SA-MP# ignores the case of commands both /help and /HeLP will call a command tagged with [Command("help")]. However, if you do not wish this to happen you can disable this:
PHP код:
// the /CAPSLOCK command
[Command("CAPSLOCK", IgnoreCase=false)]
public static void something(Player s) {}
You can also make a command return a boolean. If it returns true, SA-MP# will assume the command has successfully been executed. If it returns false, SA-MP# will assume the command has not been executed and will show the "unknown command" message to the player.
Example:
PHP код:
[Command("something")]
public static bool something(Player s)
{
if(!s.CanDoSomething) return false;
s.SendClientMessage("You did something!");
return true;
}
Inheriting the Command class
If you do not want to use all the attributes and things explained above, you can also inherit from the Command class:
https://github.com/ikkentim/SampShar...nds/Command.cs
I'm not going to show an example of how to do this, because the system explained above is much easier, flexible and useful.
But if you decide to create a sub class of Command, you need to create an instance of it once to allow SA-MP# to find it:
PHP код:
//Somewhere near you initialization code...
new MyAwesomeCommand();
Lets say you have a Street class
PHP код:
class Street
{
public string Name { get; private set;}
//Lots more info...
}
you can create a custom attribute:
PHP код:
public class StreetAttribute : WordAttribute
{
public StreetAttribute(string name)
: base(name)
{
}
public override bool Check(ref string command, out object output)
{
// Let the WordAttribute process the command first
if (!base.Check(ref command, out output))
return false;
string word = (output as string).ToLower();
Street result = null;
// TODO: find a street that matches the name stored in the word variable and store it in result
// If no street could be found, return false.
output = result;
return true;
}
}
PHP код:
[Command("goto")]
[Street("street")]
public static void goto(Player sender, Street street)
{
}
If you want SA-MP# to automatically detect this, you need to add it to the parameter resolve delegate:
PHP код:
//Somewhere near you initialization code...
DetectedCommand.ResolveParameterType = (type, name) =>
{
if (type == typeof (Street)) return new StreetAttribute(name);
// I haven't tested this, but AFAIK it should not recursively call this delegate, but call the previous (default) delegate.
return DetectedCommand.ResolveParameterType(type, name);
};
PHP код:
[Command("goto")]
public static void goto(Player sender, Street street)
{
}
If you want a parameter to have a default value, it needs to be at the end of the command:
PHP код:
[Command("tell")]
[Text("message")]
public static void tell(Player sender, Player receiver, int times = 1, string message = "I like trains!")
{
}
Usage: /tell [receiver] (times) (message)
Notice the ( ) brackets instead of [ ]. This indicates that these parameters are optional.
usage format message
If you enter invalid parameters you will be shown a "Usage" message. By default this message looks like this:
Quote:
Usage: /tell [receiver] (times) (message) |
PHP код:
public class DetectedCommand : Command
{
//...
public static Func<string, ParameterAttribute[], string> UsageFormat { get; set; }
//...
// default value:
UsageFormat = (name, parameters) =>
string.Format("Usage: /{0}{1}{2}", name, parameters.Any() ? ": " : string.Empty,
string.Join(" ", parameters.Select(
p => p.Optional
? string.Format("({0})", p.DisplayName)
: string.Format("[{0}]", p.DisplayName)
))
);
}
PHP код:
//Somewhere near you initialization code...
DetectedCommand.UsageFormat = (name, parameters) =>
{
return "some usage message";
}
If you have any remaining questions, don't hesitate to ask.