|
Post by bulldogg on Jul 31, 2008 19:39:44 GMT
if i want to make an on/off command and have an if statment depending on what parameter is chosen how would i do it? would it be a command or a targeting command for the syntax/parameter?
this is just a big guess, ive removed the if/else inner code to make it look more simple on here
|
|
|
Post by Eagle on Aug 2, 2008 8:56:33 GMT
First of all you'd want to make sure that the option entered is a valid choice, if there's more options you should use a collection instead here: string option = parameters.ToLower().Trim(); if(option != "on" && option != "off") { Events.PrivateChat.Fire( player, string.Format("Unrecognized switch {0}, use on/off instead.", option) ); return; }With just the two options to check you could then use the following boolean expression: option == "on"That will evaluate to true if the on option was provided, or false if not
|
|
|
Post by bulldogg on Aug 12, 2008 17:18:13 GMT
right ok, i get that bit but im still stuck on how i can have multiple parameters, such as
/shout [message]
|
|
|
Post by Eagle on Aug 12, 2008 18:13:59 GMT
Everything after shout is given in the parameter string, you'll just have to parse that however you see fit. Could use the Split method for instance.
|
|
|
Post by bulldogg on Aug 12, 2008 18:32:59 GMT
ah so flac doesnt apply a name to each peice in the string ok makes sense now
|
|
|
Post by bulldogg on Aug 12, 2008 19:01:17 GMT
if i use ' ' as a splitter and my command is
/shout big this text to shout
it will take
big this text to shout
as seperate items, i need the first item which would be big/medium/small [0] to be taken, and the rest of the text which in this would be [1][2][3][4] to be kept together, and there could be loads of text, would i have to rebuild it from item 1 and loop through to the end?
|
|
|
Post by Eagle on Aug 12, 2008 21:51:09 GMT
That was merely an example to get you on your way actually, you'd have to do string parsing to get the proper result and for that there's many ways. Personally I prefer to use regular expressions, here's how I would go about it:
internal class Shout: AdminCommand { private Dictionary<string, Action<string>> _messageAction; private Regex _shoutRegularExpression;
public Shout() { _messageAction = new Dictionary<string, Action<string>>(); _messageAction["small"] = SendSmallMessage; _messageAction["medium"] = SendMediumMessage; _messageAction["large"] = SendLargeMessage; string options = string.Join("|", new List<string>(_messageAction.Keys).ToArray()); string pattern = string.Format(@"^\s*(?<Size>((?i){0}(?-i)))\s+(?<Message>.+)$", options); _shoutRegularExpression = new Regex(pattern, RegexOptions.Singleline | RegexOptions.CultureInvariant); Syntax += string.Format(" [{0}] [message]", options); Description = "Shouts to universe."; }
private void SendSmallMessage(string message) { throw new NotImplementedException(); }
private void SendMediumMessage(string message) { throw new NotImplementedException(); }
private void SendLargeMessage(string message) { throw new NotImplementedException(); }
public override void Execute(Player user, string parameters) { if(!_shoutRegularExpression.IsMatch(parameters)) { // Invalid input, show error message to user return; } Match match = _shoutRegularExpression.Match(parameters); _messageAction[match.Groups["Size"].Value.ToLower()](match.Groups["Message"].Value); } }
|
|
|
Post by bulldogg on Mar 13, 2009 9:32:39 GMT
back to this thign again hehe
basically im wanting to grab an extra p[arameter for my shout plugin, this time its colour.
currenty i have /shout [message] but i want /shout [message].
i understand the regex would be
^\s*(?<Size>((?i){0}(?-i)))\s+(?<Colour>((?i){1}(?-i)))\s+(?<Message>.+)$
...i think :S
im just wondering how i could have it get the size small|medium|large, then get the colour and apply it to that small medium large block in the example code above?
could i just have it split the parameters and get the second one for simplicity, then have a function converting the name to the right colour hex which is then grabbed from within SendMediumMessage() for example
|
|
|
Post by Eagle on Mar 13, 2009 15:59:01 GMT
Depends on how you wish to specify colour really, if you intent to use a list of values similar to the size options then you're right. In that case you could set up a dictionary containing the colour map. This would translate to something like this:
internal class Shout: AdminCommand { private Dictionary<string, uint> _sizes; private Dictionary<string, uint> _colours; private void InitializeSizes() { _sizes = new Dictionary<string, uint>(); _sizes["small"] = 0x50; _sizes["medium"] = 0x58; _sizes["large"] = 0x10; } private void InitializeColours() { _colours = new Dictionary<string, uint>(); _colours["red"] = 0x0000FF00; _colours["green"] = 0x00FF0000; _colours["blue"] = 0xFF000000; }
public Shout() { InitializeSizes(); InitializeColours(); string sizeOptions = string.Join("|", new List<string>(_sizes.Keys).ToArray()); string colourOptions = string.Join("|", new List<string>(_colours.Keys).ToArray()); ParametersPattern = string.Format(@"^\s*(?<Size>((?i){0}(?-i)))\s+(?<Colour>((?i){1}(?-i)))\s+(?<Message>.+)$", sizeOptions, colourOptions); Syntax += string.Format(" [{0}] [{1}] [message]", sizeOptions, colourOptions); Description = "Shouts to universe."; }
public override void Execute(Player user, string parameters) { Match match = ParametersRegularExpression.Match(parameters); UniverseCustomChat.Fire( _colours[match.Groups["Colour"].Value.ToLower()] | _sizes[match.Groups["Size"].Value.ToLower()], match.Groups["Message"].Value ); } }
|
|
|
Post by bulldogg on Mar 14, 2009 19:26:22 GMT
ah thanks eagle, i was so close to that but i was trying to use strings instead of uint BD
|
|
|
Post by Eagle on Mar 15, 2009 8:45:45 GMT
Glad to be of help
|
|