I'm currently working on my first serious fCraft command.
As I've said several times before, I'm slowly learning to mod fCraft, and this is my first real project.
The command is now working like this:
A player with permission SendModreq does /Modreq [Message]
All players with permission ReadModreq will get this message: (Modreq)BobKare: [Message]
/// <summary> Sends a Mod request (/Modreq). </summary>
/// <param name="player"> Player writing the message. </param>
/// <param name="rawMessage"> Message text. </param>
/// <returns> True if message was sent, false if it was cancelled by an event callback. </returns>
public static bool SendModreq([NotNull] Player player, [NotNull] string rawMessage)
{
if (player == null) throw new ArgumentNullException("player");
if (rawMessage == null) throw new ArgumentNullException("rawMessage");
var recepientList = Server.Players.Can(Permission.ReadModreq)
.NotIgnoring(player)
.Union(player);
string formattedMessage = String.Format("&P(Modreq){0}&P: {1}",
player.ClassyName,
rawMessage);
var e = new ChatSendingEventArgs(player,
rawMessage,
formattedMessage,
ChatMessageType.Staff,
recepientList);
if (!SendInternal(e)) return false;
Logger.Log(LogType.GlobalChat,
"(Modreq){0}: {1}", player.Name, rawMessage);
return true;
}
I have copied a lot of fCraft "templates", and all is tested. Works fine.
To do:
Add ability for players with permission ReadModreq: /Modreq list - Displays all written Modreqs with an ID sorted numerically
Add ability for players with permission ReadModreq: /Modreq abort [ID] - To use when you've finished a Modreq/remove spam
I would really like some help for this, even I'm looking for a method for adding this.
if (message.ToLower() == "remove")
{
string ID = cmd.Next();
if (ID != null)
{
double Num; //number vs text checker
bool isNum = double.TryParse(ID, out Num);
if (isNum)
{
int ReqID = Convert.ToInt16(ID);
if(ModRequests[ReqID] != null){
ModRequests.RemoveAt(ReqID);
}
player.Message("&SModRequest {0} has been removed", ReqID.ToString());
return;
}
else
player.Message("&SYou need to enter a valid ID. /Modreq List");
}
}
if (message.ToLower() == "clear")
{
ModRequests.Clear();
player.Message("The ModRequest list was emptied");
}
Note: You may have a problem if the list saves too many messages and you try to print them. Maybe add a if(ModRequests.Count() < 50) around the List Adder?
Last edited by Jonty800 on January 20th, 2012, 5:55 pm, edited 3 times in total.
Put the "public static List<string>" above the first "static readonly CommandDescriptor" on the page. Understand that?
The everything else should be put in the command, right under the part where it recognizes color codes and before the part where the message is actually sent.
string message = cmd.NextAll().Trim();
if (message.Length > 0)
{
if (player.Can(Permission.UseColorCodes) && message.Contains("%"))
{
message = Color.ReplacePercentCodes(message);
}
//commands start here
if (message.ToLower() == "remove")
{
string ID = cmd.Next();
if (ID != null)
{
double Num; //number vs text checker
bool isNum = double.TryParse(ID, out Num);
if (isNum)
{
int ReqID = Convert.ToInt16(ID);
ModRequests.RemoveAt(ReqID);
player.Message("&SModRequest {0} has been removed", ReqID.ToString());
return;
}
else
player.Message("&SYou need to enter a valid ID. /Modreq List");
return;
}
}
//and end before the message is sent
Chat.SendAdmin(player, message);
ModRequests.Add(player.ClassyName + "&P: " + message);
//and the list adder should be under the chat sender
}
Last edited by Jonty800 on January 20th, 2012, 5:43 pm, edited 2 times in total.
static void ModreqHandler(Player player, Command cmd)
{
if (player.Info.IsMuted)
{
player.MessageMuted();
return;
}
if (player.DetectChatSpam()) return;
string message = cmd.NextAll().Trim();
if (message.Length > 0)
{
if (player.Can(Permission.UseColorCodes) && message.Contains("%"))
{
message = Color.ReplacePercentCodes(message);
}
Chat.SendModreq(player, message);
ModRequests.Add(player.ClassyName + "&P: " + message);
if (player.Can(Permission.UseColorCodes) && message.Contains("%"))
{
message = Color.ReplacePercentCodes(message);
}
if (message.ToLower() == "list")
{
if (ModRequests.Count() > 0)
{
for (int i = 0; i < ModRequests.Count; i++)
{
player.Message("{0} &S- {1}", ModRequests[i], i.ToString());
}
return;
}
else
player.Message("&SThe ModRequest list is empty");
return;
}
//commands start here
if (message.ToLower() == "remove")
{
string ID = cmd.Next();
if (ID != null)
{
double Num; //number vs text checker
bool isNum = double.TryParse(ID, out Num);
if (isNum)
{
int ReqID = Convert.ToInt16(ID);
ModRequests.RemoveAt(ReqID);
player.Message("&SModRequest {0} has been removed", ReqID.ToString());
return;
}
else
player.Message("&SYou need to enter a valid ID. /Modreq List");
return;
}
}
//and end before the message is sent
Chat.SendModreq(player, message);
ModRequests.Add(player.ClassyName + "&P: " + message);
//and the list adder should be under the chat sender
}
}
/Modreq list does the following:
Creates a Modreq named: list
Shows the list of Modreqs (BobKare: list - 0)
if (message.ToLower() == "list")
{
if (player.Can(Permission.ReadModreq))
{
if (ModRequests.Count() > 0)
{
for (int i = 0; i < ModRequests.Count; i++)
{
player.Message("{0} &S- {1}", ModRequests[i], i.ToString());
}
return;
}
else
player.Message("&SThe ModRequest list is empty");
return;
}
else
player.Message("You are not allowed to read ModRequests!");
}
EDIT2: I get several errors on other cmds in ChatCommands.cs! Most like this: Excepted class, delegate, enum, interface or struct.
/Modreq [Message] - Displays a message to everyone which can ReadModreq
/Modreq list - Displays a list of Modreqs w/ID
/Modreq remove [ID] - Removes a Modreq from the list
/Modreq clear - Clears the list
public static List<string> ModRequests = new List<string>();
#region Modreq
static readonly CommandDescriptor CdModreq = new CommandDescriptor
{
Name = "Modreq",
Aliases = new[] { "mod" },
Category = CommandCategory.Chat | CommandCategory.Moderation,
NotRepeatable = true,
IsConsoleSafe = false,
DisableLogging = false,
Usage = "/Modreq [Request]",
Help = "Request help from a staff member",
Handler = ModreqHandler
};
static void ModreqHandler(Player player, Command cmd)
{
if (player.Info.IsMuted)
{
player.MessageMuted();
return;
}
if (player.DetectChatSpam()) return;
string message = cmd.NextAll().Trim();
if (message.Length > 0)
{
if (player.Can(Permission.UseColorCodes) && message.Contains("%"))
{
message = Color.ReplacePercentCodes(message);
}
if( message.ToLower().StartsWith("done ") )
{
string ID = message.Substring(7).Trim();
if (ID != null)
{
if (player.Can(Permission.ReadModreq))
{
double Num; //number vs text checker
bool isNum = double.TryParse(ID, out Num);
if (isNum)
{
int ReqID = Convert.ToInt16(ID);
if (ReqID < ModRequests.Count && ReqID > 0 && ModRequests[ReqID] != null)
{
ModRequests.RemoveAt(ReqID);
Server.Message("&SModRequest &C{0}&S was done by {1}", ReqID.ToString(), player.ClassyName);
return;
}
else
{
player.Message("&SID not found! &H/ModReq list");
return;
}
}
else
{
player.Message("&SYou need to enter a valid ID. &H/Modreq List");
return;
}
}
else
{
player.Message("&SYou are not allowed to read ModRequests!");
return;
}
}
else
{
player.Message("&SYou need to enter an ID");
}
}
if (message.ToLower().StartsWith("clear"))
{
if (player.Can(Permission.ClearModreq))
{
ModRequests.Clear();
player.Message("The ModRequest list was emptied");
return;
}
else
{
player.Message("You are not allowed to clear the list of ModRequests!");
return;
}
}
if (message.ToLower().StartsWith("list"))
{
if (player.Can(Permission.ReadModreq))
{
if (ModRequests.Count() > 0)
{
for (int i = 0; i < ModRequests.Count; i++)
{
player.Message("{0} &S- {1}", ModRequests[i], i.ToString());
}
return;
}
else
player.Message("&SThe ModRequest list is empty");
return;
}
else
{
player.Message("You are not allowed to read ModRequests!");
return;
}
}
if (player.Can(Permission.SendModreq))
{
if (message != "list" | message != "remove" | message != "clear")
{
Chat.SendModreq(player, message);
}
ModRequests.Add(player.ClassyName + "&P: " + message);
}
else
{
player.Message("You are not allowed to send ModRequests!");
return;
}
}
}
Problem:
When doing /Modreq done [ID] it spams an error saying the index is out of range, either it's an existing ID or an ID which is invalid (not existing).
Mark that when you use /Modreq done [Negative Value], for example /Modreq done -3 it works like it should work, saying that you need to enter a valid ID.