2121*/
2222#endregion
2323
24+ using System ;
2425using UnityEngine ;
2526
2627namespace Essentials . Api . Command {
2728
2829 public static class CommandExtensions {
2930
3031 /// <summary>
31- /// Try get a vector3 from 3 arguments, starting in <paramref name="initialIndex"/>
32+ /// Try to parse a Vector3 from 3 arguments, starting in <paramref name="initialIndex"/>
3233 /// </summary>
3334 /// <param name="src">Source</param>
3435 /// <param name="initialIndex"> Initial index </param>
@@ -60,6 +61,38 @@ public static bool IsInRange(this ICommandArgument src, int minInclusive, int ma
6061 return val >= minInclusive && val <= maxInclusive ;
6162 }
6263
64+ /// <summary>
65+ /// Try to convert the argument to byte.
66+ /// </summary>
67+ /// <param name="value">the converted value</param>
68+ /// <param name="error">
69+ /// CommandResult.LangError("NUMBER_BETWEEN", byte.MinValue, byte.MaxValue) if out of range;
70+ // CommandResult.LangError("INVALID_NUMBER", src.ToString()) if invalid.
71+ /// </param>
72+ /// <returns>true if sucessfull, otherwise false</returns>
73+ public static bool TryConvertToByte ( this ICommandArgument src , out byte value , out CommandResult error ) {
74+ value = 0 ;
75+ error = null ;
76+ try {
77+ value = byte . Parse ( src . ToString ( ) ) ;
78+ return true ;
79+ } catch ( OverflowException ) {
80+ error = CommandResult . LangError ( "NUMBER_BETWEEN" , byte . MinValue , byte . MaxValue ) ;
81+ } catch ( FormatException ) {
82+ error = CommandResult . LangError ( "INVALID_NUMBER" , src . ToString ( ) ) ;
83+ }
84+ return false ;
85+ }
86+
87+ public static bool TryConvertToByte ( this ICommandArgument src , out byte ? value , out CommandResult error ) {
88+ byte result ;
89+ if ( src . TryConvertToByte ( out result , out error ) ) {
90+ value = result ;
91+ return true ;
92+ }
93+ value = null ;
94+ return false ;
95+ }
6396 }
6497
6598}
0 commit comments