Code: Select all
static readonly CommandDescriptor CdWhoIs = new CommandDescriptor
{
Name = "WhoIs", //you should change the command name, unless you remove /Whois from /Info
Category = CommandCategory.Info,
Permissions = new Permission[] { Permission.ViewOthersInfo },
IsConsoleSafe = false, //I don't need console access for this, hasn't been tested for true
Usage = "/Whois DisplayedName",
Handler = WhoIsHandler
};
private static void WhoIsHandler(Player player, Command cmd)
{
string Name = cmd.Next(); //the desired displayedName
if (string.IsNullOrEmpty(Name)) //check if null and print usage
{
CdWhoIs.PrintUsage(player);
return;
}
Name = Color.StripColors(Name.ToLower()); //make the argument lowercase, and strip colors
//here we want a collection of players who HAVE a displayedName,
//and the displayedName either contains the string in question (Name)
PlayerInfo[] Names = PlayerDB.PlayerInfoList.Where(p => p.DisplayedName != null &&
Color.StripColors(p.DisplayedName.ToLower()).Contains(Name))
.ToArray();
Array.Sort(Names, new PlayerInfoComparer(player));
if (Names.Length < 1){ //if no results were found
player.Message("&WNo results found with that DisplayedName");
return;
}
if (Names.Length == 1){ //if one result was found
player.Message("One player found with that DisplayedName: {0}", Names[0].Rank.Color + Names[0].Name); //display the 1 results name, coloured by rank color
return;
}
if (Names.Length <= 30){
MessageManyMatches(player, Names); //if all names fit onto a page, print all names found
}else{ //else, use pagination to display the results
int offset;
if (!cmd.NextInt(out offset)) offset = 0;
if (offset >= Names.Length)
offset = Math.Max(0, Names.Length - 30);
PlayerInfo[] Part = Names.Skip(offset).Take(30).ToArray();
MessageManyMatches(player, Part);
if (offset + Part.Length < Names.Length){
player.Message("Showing {0}-{1} (out of {2}). Next: &H/Whois {3} {4}",
offset + 1, offset + Part.Length, Names.Length,
Name, offset + Part.Length);
}else{
player.Message("Showing matches {0}-{1} (out of {2}).",
offset + 1, offset + Part.Length, Names.Length);
}
}
}
//simple static method to format the list of names[] and join them to a string for printing
static void MessageManyMatches(Player player, PlayerInfo[] names)
{
if (names == null) throw new ArgumentNullException("names");
string nameList = names.JoinToString(", ", p => p.Rank.Color + p.Name + "&S(" + p.ClassyName + "&S)");
player.Message("More than one player matched with that DisplayedName: {0}",
nameList);
}