Display reason why command cannot be ran

This commit is contained in:
D1GQ committed 2026-01-29 22:06:19 -06:00
1 parent c2fb4e9505
commit 532638055c
5 files changed
+71 -13

No files matched your search

+8 -3
View File
@@ -57,10 +57,15 @@ internal abstract class BaseCommand
internal virtual bool ShowCommand() => true;
/// <summary>
/// Determines whether this command should be shown in suggestions.
/// Determines whether this command can be executed based on current conditions.
/// </summary>
/// <returns>True if the command should be shown in suggestions; otherwise, false.</returns>
internal virtual bool ShowSuggestion() => ShowCommand();
/// <param name="reason">When this method returns, contains the reason why the command cannot be run if it returns false; otherwise, an empty string.</param>
/// <returns>True if the command can be executed; otherwise, false.</returns>
internal virtual bool CanRunCommand(out string reason)
{
reason = string.Empty;
return true;
}
/// <summary>
/// Executes the command.
+13 -3
View File
@@ -1,5 +1,5 @@
using BetterAmongUs.Data;
using BetterAmongUs.Attributes;
using BetterAmongUs.Attributes;
using BetterAmongUs.Data;
using BetterAmongUs.Modules;
namespace BetterAmongUs.Commands;
@@ -9,7 +9,17 @@ internal sealed class DumpCommand : BaseCommand
{
internal override string Name => "dump";
internal override string Description => "Dump the entire log to the user's desktop";
internal override bool ShowCommand() => !GameState.IsInGamePlay;
internal override bool CanRunCommand(out string reason)
{
if (GameState.IsInGamePlay)
{
reason = "Only can run in lobby";
return false;
}
return base.CanRunCommand(out reason);
}
internal override void Run()
{
+17 -1
View File
@@ -8,7 +8,23 @@ internal sealed class ForceSkipCommand : BaseCommand
{
internal override string Name => "forceskip";
internal override string Description => "Force skips a meeting in progress";
internal override bool ShowCommand() => GameState.IsHost && GameState.IsMeeting;
internal override bool CanRunCommand(out string reason)
{
if (!GameState.IsHost)
{
reason = "Can only run as host";
return false;
}
if (!GameState.IsMeeting)
{
reason = "Can only run in meeting";
return false;
}
return base.CanRunCommand(out reason);
}
internal override void Run()
{
if (GameState.IsHost)
+12 -3
View File
@@ -1,6 +1,6 @@
using BetterAmongUs.Commands.Arguments;
using BetterAmongUs.Attributes;
using BetterAmongUs.Commands.Arguments;
using BetterAmongUs.Helpers;
using BetterAmongUs.Attributes;
using BetterAmongUs.Modules;
namespace BetterAmongUs.Commands;
@@ -10,6 +10,16 @@ internal sealed class KickCommand : BaseCommand
{
internal override string Name => "kick";
internal override string Description => "Kick a player from the game";
internal override bool CanRunCommand(out string reason)
{
if (!GameState.IsHost)
{
reason = "Can only run as host";
return false;
}
return base.CanRunCommand(out reason);
}
public KickCommand()
{
playerArgument = new PlayerArgument(this);
@@ -19,7 +29,6 @@ internal sealed class KickCommand : BaseCommand
private PlayerArgument playerArgument { get; }
private BoolArgument boolArgument { get; }
internal override bool ShowCommand() => GameState.IsHost;
internal override void Run()
{
var player = playerArgument.TryGetTarget();
@@ -52,6 +52,11 @@ internal static class ChatCommandsPatch
return true;
}
if (!closestCommand.CanRunCommand(out string _))
{
return false;
}
HandleCommand();
if (ChatPatch.ChatHistory.Count == 0 || ChatPatch.ChatHistory[^1] != text) ChatPatch.ChatHistory.Add(text);
@@ -130,8 +135,7 @@ internal static class ChatCommandsPatch
closestCommand = GetClosestCommand(typedParts[0]);
bool isSuggestionValid = closestCommand != null
&& (typedParts[0].Equals(closestCommand.Name, StringComparison.OrdinalIgnoreCase) || typedParts.Length == 1)
&& closestCommand.ShowSuggestion();
&& (typedParts[0].Equals(closestCommand.Name, StringComparison.OrdinalIgnoreCase) || typedParts.Length == 1);
if (isSuggestionValid)
{
@@ -167,8 +171,13 @@ internal static class ChatCommandsPatch
__instance.freeChatField.textArea.SetText(fullSuggestion);
}
if (!closestCommand.CanRunCommand(out string _))
{
fullSuggestion = fullSuggestion.ToColor("#FF0300".HexToColor());
}
commandText.text = fullSuggestion;
commandInfo.text = $"{closestCommand.Description}{GenerateArgumentInfo()}";
commandInfo.text = $"{closestCommand.Description}{GenerateArgumentInfo()}{GenerateCanRunInfo()}";
}
private static string GenerateSuggestion(string[] typedParts)
@@ -207,6 +216,15 @@ internal static class ChatCommandsPatch
return $" - <#a6a6a6>{argumentInfo}</color>";
}
private static string GenerateCanRunInfo()
{
if (!closestCommand.CanRunCommand(out string reason))
{
return $" - <#FF0300>{reason}</color>";
}
return string.Empty;
}
internal static BaseCommand? GetClosestCommand(string typedCommand)
{