mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-14 15:34:37 +01:00
utils for JSON files and RNG/rolling, abstracted arg parsing for commands
This commit is contained in:
@@ -16,6 +16,7 @@ public class commandHandler : Attribute
|
||||
Description = description;
|
||||
Example = example;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
@@ -48,6 +49,22 @@ public abstract class Command
|
||||
prop.SetValue(this, arg.Value);
|
||||
}
|
||||
}
|
||||
|
||||
protected T Parse<T>(string? value, T fallback = default)
|
||||
{
|
||||
var tryParseMethod = typeof(T).GetMethod("TryParse", [typeof(string), typeof(T).MakeByRefType()]);
|
||||
|
||||
if (tryParseMethod != null)
|
||||
{
|
||||
var parameters = new object[] { value, null };
|
||||
bool success = (bool)tryParseMethod.Invoke(null, parameters);
|
||||
|
||||
if (success)
|
||||
return (T)parameters[1];
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
public static class CommandHandler
|
||||
|
||||
Reference in New Issue
Block a user