Hey guys, this'll be my second release for MPGH. In this one, I'll be releasing a /rank command I made. This command is 100% bug-free, and is easily customizable to fit your server. For the sake of the release, I have edited the code to the standard ranks on a FSOD source.
How to use it?:
/rank username rankId
How does it work?: I made a comment above the command if you read it'll explain. I'll get a little more in-depth as to why it should be bug free.
So pretty much I have created a basic boolean that checks if a string has the chars 0-9 in it. If they do not, it'll send an message to the user executing the command. Now we have an extra check in place to ensure the rank we are giving is actually a rank. So in FSOD the max rank would be 3 because the hierarchy for ranks goes: Player (0), Donator (1), Admin (2), Owner (3). So we added checks to ensure that the argument being given was a valid rank and if not, would also throw a message to the player.
Now, the player has been given a rank but we need to tell them they have been given a rank and what rank they were given, as well as tell the user who executed the command in case they gave the wrong rank, etc.
Then, I created an Array called
ranks to hold all the rank names as well as Parsed the
rank argument from a string to an int so it can be used to access the name of the rank in the
ranks Array.
Let's get started:
in
wServer/realm/commands/AdminCommands.cs, Add the following command:
Code:
/**
* @Author Wilson (AKA: NigrKiller @ MPGH)
*
* Sets any player's rank.
*
* Iterates through chars 0-9 for our {@code rank} argument to see if it is
* a valid rank being set.
*
* Parses our {@code rank} argument into an int and displays the newly
* earned rank to the players through our {@code ranks} Array.
*/
internal class Rank : Command {
public Rank() : base("rank", 1) { }
private const int MAX_RANK = 3;
bool validRank(string rankId) {
return rankId.All(c => c >= '0' && c <= '9');
}
String[] ranks = { "Player", "Donator", "Administrator",
"Owner" };
protected override bool Process(Player player, RealmTime time, string[] args) {
var playerToRank = player.Manager.FindPlayer(args[0]);
string rank = args[1];
if (playerToRank == null) {
player.SendError("Player not found! They may be offline.");
return false;
}
if (!validRank(rank) && int.Parse(rank) < 0 && int.Parse(rank) > MAX_RANK) {
player.SendError("Invalid Rank! Use: /rank username rankId");
return false;
}
player.Manager.Database.DoActionAsync(db => {
var execute = db.CreateQuery();
execute.CommandText = "UPDATE accounts SET rank="+ rank +" WHERE id=@accId;";
execute.Parameters.AddWithValue("@accId", playerToRank.AccountId);
execute.ExecuteNonQuery();
});
playerToRank.SendInfo("You have been ranked "+ ranks[int.Parse(rank)] +" by "+ player.Name +".");
player.SendInfo("You have given "+ playerToRank.Name +" the rank, "+ ranks[int.Parse(rank)] +".");
return true;
}
}
To edit the command, simply do the following:
This is the default permission on FSOD. It pretty much means if you're rank 3 you can use it. Change it to suit your permissions.
For the MAX_RANK constant, change the value to your max rank on your server.
For the ranks Array, simply add all of the names of your ranks (in order) from 0-?
Note: You
MUST add the names of the ranks to the Array if you change the max rank otherwise you will get an ArrayIndexOutOfBounds Exception (Is that what it's called in C#? Again, sorry I come from Java lol)
Conclusion: All in all, I could not find a single bug while testing this, but if you find one I'll be happy to fix it. This works flawlessly on a regular FSOD source so if it doesn't work you're either typing the command wrong or couldn't follow instructions to add this command...