mirror of
https://github.com/D1GQ/BetterAmongUs.git
synced 2026-10-19 15:51:52 +00:00
Add generic BaseArgument
This commit is contained in:
1 parent
cd77623c75
commit
463c6baf49
6 files changed
+38
-21
No files matched your search
@@ -1,37 +1,41 @@
|
||||
namespace BetterAmongUs.Commands.Arguments;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base class for command arguments in BetterAmongUs.
|
||||
/// Abstract base class for command arguments.
|
||||
/// </summary>
|
||||
/// <param name="command">The command this argument belongs to.</param>
|
||||
/// <param name="argInfo">Information about the argument.</param>
|
||||
/// <param name="argInfo">Information about the argument, such as its name, description, or format.</param>
|
||||
internal abstract class BaseArgument(BaseCommand command, string argInfo)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the command this argument belongs to.
|
||||
/// </summary>
|
||||
/// <value>The parent <see cref="BaseCommand"/> instance that owns this argument.</value>
|
||||
internal BaseCommand Command { get; } = command;
|
||||
|
||||
/// <summary>
|
||||
/// Gets information about the argument.
|
||||
/// </summary>
|
||||
/// <value>A string containing metadata about the argument, such as its name, description, or expected format.</value>
|
||||
internal string ArgInfo { get; } = argInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the current argument value.
|
||||
/// Gets or sets the current argument value as a string.
|
||||
/// </summary>
|
||||
/// <value>The raw string value of the argument. Defaults to an empty string.</value>
|
||||
internal string Arg { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the argument suggestions for auto-completion.
|
||||
/// </summary>
|
||||
/// <returns>An array of suggestion strings for the current argument.</returns>
|
||||
protected virtual string[] GetArgSuggestions()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the Argument back to its default state.
|
||||
/// Resets the argument back to its default state.
|
||||
/// </summary>
|
||||
internal void Reset()
|
||||
{
|
||||
@@ -41,7 +45,23 @@ internal abstract class BaseArgument(BaseCommand command, string argInfo)
|
||||
/// <summary>
|
||||
/// Gets the closest suggestion for the current argument value.
|
||||
/// </summary>
|
||||
/// <returns>The closest matching suggestion, or empty string if none found.</returns>
|
||||
/// <returns>The closest matching suggestion, or an empty string if none found.</returns>
|
||||
internal string GetClosestSuggestion() =>
|
||||
GetArgSuggestions().FirstOrDefault(name => name.StartsWith(Arg, StringComparison.OrdinalIgnoreCase)) ?? string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic abstract base class for typed command arguments.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The target type that this argument will be parsed into.</typeparam>
|
||||
/// <param name="command">The command this argument belongs to.</param>
|
||||
/// <param name="argInfo">Information about the argument, such as its name, description, or format.</param>
|
||||
internal abstract class BaseArgument<T>(BaseCommand command, string argInfo) : BaseArgument(command, argInfo)
|
||||
{
|
||||
/// <summary>
|
||||
/// Attempts to parse the current argument value to type <typeparamref name="T"/>.
|
||||
/// </summary>
|
||||
/// <param name="result">When this method returns, contains the parsed value if parsing succeeded; otherwise, the default value for <typeparamref name="T"/>.</param>
|
||||
/// <returns><c>true</c> if the argument was successfully parsed; otherwise, <c>false</c>.</returns>
|
||||
internal abstract bool TryParse(out T result);
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
/// </summary>
|
||||
/// <param name="command">The command this argument belongs to.</param>
|
||||
/// <param name="argInfo">Information about the argument (default: "{bool}").</param>
|
||||
internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool}") : BaseArgument(command, argInfo)
|
||||
internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool}") : BaseArgument<bool>(command, argInfo)
|
||||
{
|
||||
protected override string[] GetArgSuggestions()
|
||||
{
|
||||
@@ -17,7 +17,7 @@ internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool}
|
||||
/// </summary>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
internal bool TryParseBool(out bool result)
|
||||
internal override bool TryParse(out bool result)
|
||||
{
|
||||
if (Arg.ToLower() == "true")
|
||||
{
|
||||
@@ -32,7 +32,7 @@ internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool}
|
||||
else
|
||||
{
|
||||
BaseCommand.CommandErrorText($"Invalid Syntax!");
|
||||
result = false;
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace BetterAmongUs.Commands.Arguments;
|
||||
/// </summary>
|
||||
/// <param name="command">The command this argument belongs to.</param>
|
||||
/// <param name="argInfo">Information about the argument (default: "{player}").</param>
|
||||
internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{player}") : BaseArgument(command, argInfo)
|
||||
internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{player}") : BaseArgument<PlayerControl>(command, argInfo)
|
||||
{
|
||||
protected override string[] GetArgSuggestions()
|
||||
{
|
||||
@@ -38,7 +38,7 @@ internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{pla
|
||||
/// </summary>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
internal bool TryParsePlayer(out PlayerControl? result)
|
||||
internal override bool TryParse(out PlayerControl result)
|
||||
{
|
||||
foreach (var player in BAUPlugin.AllPlayerControls)
|
||||
{
|
||||
@@ -51,7 +51,7 @@ internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{pla
|
||||
}
|
||||
}
|
||||
|
||||
result = null;
|
||||
result = default!;
|
||||
BaseCommand.CommandErrorText("Player not found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
/// </summary>
|
||||
/// <param name="command">The command this argument belongs to.</param>
|
||||
/// <param name="argInfo">Information about the argument (default: "{String}").</param>
|
||||
internal sealed class StringArgument(BaseCommand command, string argInfo = "{String}") : BaseArgument(command, argInfo)
|
||||
internal sealed class StringArgument(BaseCommand command, string argInfo = "{String}") : BaseArgument<string>(command, argInfo)
|
||||
{
|
||||
protected override string[] GetArgSuggestions()
|
||||
{
|
||||
@@ -17,12 +17,9 @@ internal sealed class StringArgument(BaseCommand command, string argInfo = "{Str
|
||||
/// </summary>
|
||||
internal Func<string[]> ArgSuggestions { get; set; } = () => { return []; };
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string argument.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
internal string GetString()
|
||||
internal override bool TryParse(out string result)
|
||||
{
|
||||
return Arg;
|
||||
result = Arg;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -31,10 +31,10 @@ internal sealed class KickCommand : BaseCommand
|
||||
|
||||
internal override void Run()
|
||||
{
|
||||
if (!_playerArgument.TryParsePlayer(out var player))
|
||||
if (!_playerArgument.TryParse(out var player))
|
||||
return;
|
||||
|
||||
var isBan = _boolArgument.TryParseBool(out var ban);
|
||||
var isBan = _boolArgument.TryParse(out var ban);
|
||||
if (!isBan)
|
||||
return;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ internal sealed class PlayerInfoCommand : BaseCommand
|
||||
|
||||
internal override void Run()
|
||||
{
|
||||
if (!_playerArgument.TryParsePlayer(out var player))
|
||||
if (!_playerArgument.TryParse(out var player))
|
||||
return;
|
||||
|
||||
if (player.Data == null)
|
||||
|
||||
Reference in new issue
Block a user