From 4cc6cb95dc6faf411f4be6e50b6230708f6ca6f7 Mon Sep 17 00:00:00 2001 From: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Date: Mon, 16 Jun 2025 12:25:44 +0200 Subject: [PATCH] Commands for adding and removing actions (#38317) --- .../Actions/Commands/AddActionCommand.cs | 75 +++++++++++++++++ .../Actions/Commands/RemoveActionCommand.cs | 84 +++++++++++++++++++ Content.Shared/Actions/SharedActionsSystem.cs | 13 +++ .../en-US/actions/commands/addaction.ftl | 11 +++ .../en-US/actions/commands/rmaction.ftl | 12 +++ 5 files changed, 195 insertions(+) create mode 100644 Content.Server/Actions/Commands/AddActionCommand.cs create mode 100644 Content.Server/Actions/Commands/RemoveActionCommand.cs create mode 100644 Resources/Locale/en-US/actions/commands/addaction.ftl create mode 100644 Resources/Locale/en-US/actions/commands/rmaction.ftl diff --git a/Content.Server/Actions/Commands/AddActionCommand.cs b/Content.Server/Actions/Commands/AddActionCommand.cs new file mode 100644 index 0000000000..c2c471e0ea --- /dev/null +++ b/Content.Server/Actions/Commands/AddActionCommand.cs @@ -0,0 +1,75 @@ +using System.Linq; +using Content.Server.Administration; +using Content.Shared.Actions; +using Content.Shared.Actions.Components; +using Content.Shared.Administration; +using Content.Shared.Prototypes; +using Robust.Shared.Console; +using Robust.Shared.Prototypes; + +namespace Content.Server.Actions.Commands; + +[AdminCommand(AdminFlags.Debug)] +public sealed class AddActionCommand : LocalizedEntityCommands +{ + [Dependency] private readonly IPrototypeManager _prototypes = default!; + [Dependency] private readonly SharedActionsSystem _actions = default!; + [Dependency] private readonly IPrototypeManager _prototypeManager = default!; + + public override string Command => "addaction"; + + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length != 2) + { + shell.WriteError(Loc.GetString(Loc.GetString("cmd-addaction-invalid-args"))); + return; + } + + if (!NetEntity.TryParse(args[0], out var targetUidNet) || !EntityManager.TryGetEntity(targetUidNet, out var targetEntity)) + { + shell.WriteLine(Loc.GetString("shell-entity-uid-must-be-number")); + return; + } + + if (!EntityManager.HasComponent(targetEntity)) + { + shell.WriteError(Loc.GetString("cmd-addaction-actions-not-found")); + return; + } + + if (!_prototypes.TryIndex(args[1], out var proto) || + !proto.HasComponent()) + { + shell.WriteError(Loc.GetString("cmd-addaction-action-not-found", ("action", args[1]))); + return; + } + + if (_actions.AddAction(targetEntity.Value, args[1]) == null) + { + shell.WriteError(Loc.GetString("cmd-addaction-adding-failed")); + } + } + + public override CompletionResult GetCompletion(IConsoleShell shell, string[] args) + { + if (args.Length == 1) + { + return CompletionResult.FromHintOptions( + CompletionHelper.Components(args[0]), + Loc.GetString("cmd-addaction-player-completion")); + } + + if (args.Length != 2) + return CompletionResult.Empty; + + var actionPrototypes = _prototypeManager.EnumeratePrototypes() + .Where(p => p.HasComponent()) + .Select(p => p.ID) + .Order(); + + return CompletionResult.FromHintOptions( + actionPrototypes, + Loc.GetString("cmd-addaction-action-completion")); + } +} diff --git a/Content.Server/Actions/Commands/RemoveActionCommand.cs b/Content.Server/Actions/Commands/RemoveActionCommand.cs new file mode 100644 index 0000000000..541f8f88f9 --- /dev/null +++ b/Content.Server/Actions/Commands/RemoveActionCommand.cs @@ -0,0 +1,84 @@ +using Content.Server.Administration; +using Content.Shared.Actions; +using Content.Shared.Actions.Components; +using Content.Shared.Administration; +using Robust.Shared.Console; + +namespace Content.Server.Actions.Commands; + +[AdminCommand(AdminFlags.Debug)] +public sealed class RemoveActionCommand : LocalizedEntityCommands +{ + [Dependency] private readonly SharedActionsSystem _actions = default!; + + public override string Command => "rmaction"; + + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length != 2) + { + shell.WriteError(Loc.GetString(Loc.GetString("cmd-rmaction-invalid-args"))); + return; + } + + if (!NetEntity.TryParse(args[0], out var targetUidNet) || !EntityManager.TryGetEntity(targetUidNet, out var targetEntity)) + { + shell.WriteLine(Loc.GetString("shell-could-not-find-entity-with-uid", ("uid", args[0]))); + return; + } + + if (!NetEntity.TryParse(args[1], out var targetActionUidNet) || !EntityManager.TryGetEntity(targetActionUidNet, out var targetActionEntity)) + { + shell.WriteLine(Loc.GetString("shell-could-not-find-entity-with-uid", ("uid", args[1]))); + return; + } + + if (!EntityManager.HasComponent(targetEntity)) + { + shell.WriteError(Loc.GetString("cmd-rmaction-actions-not-found")); + return; + } + + if (_actions.GetAction(targetActionEntity) is not { } ent) + { + shell.WriteError(Loc.GetString("cmd-rmaction-not-an-action")); + return; + } + + _actions.SetTemporary(ent.Owner, true); + + _actions.RemoveAction(ent.Owner); + } + + public override CompletionResult GetCompletion(IConsoleShell shell, string[] args) + { + if (args.Length == 1) + { + return CompletionResult.FromHintOptions( + CompletionHelper.Components(args[0]), + Loc.GetString("cmd-rmaction-player-completion")); + } + + if (args.Length == 2) + { + if (!NetEntity.TryParse(args[0], out var targetUidNet) || !EntityManager.TryGetEntity(targetUidNet, out var targetEntity)) + return CompletionResult.Empty; + + if (!EntityManager.HasComponent(targetEntity)) + return CompletionResult.Empty; + + var actions = _actions.GetActions(targetEntity.Value); + + var options = new List(); + foreach (var action in actions) + { + var hint = Loc.GetString("cmd-rmaction-action-info", ("action", action)); + options.Add(new CompletionOption(action.Owner.ToString(), hint)); + } + + return CompletionResult.FromHintOptions(options, Loc.GetString("cmd-rmaction-action-completion")); + } + + return CompletionResult.Empty; + } +} diff --git a/Content.Shared/Actions/SharedActionsSystem.cs b/Content.Shared/Actions/SharedActionsSystem.cs index 583bd177dc..ad93d6c998 100644 --- a/Content.Shared/Actions/SharedActionsSystem.cs +++ b/Content.Shared/Actions/SharedActionsSystem.cs @@ -1019,4 +1019,17 @@ public abstract class SharedActionsSystem : EntitySystem // TODO: Check for charge recovery timer return action.Cooldown.HasValue && action.Cooldown.Value.End > curTime; } + + /// + /// Marks the action as temporary. + /// Temporary actions get deleted upon being removed from an entity. + /// + public void SetTemporary(Entity ent, bool temporary) + { + if (!Resolve(ent.Owner, ref ent.Comp, false)) + return; + + ent.Comp.Temporary = temporary; + Dirty(ent); + } } diff --git a/Resources/Locale/en-US/actions/commands/addaction.ftl b/Resources/Locale/en-US/actions/commands/addaction.ftl new file mode 100644 index 0000000000..be1cbf27de --- /dev/null +++ b/Resources/Locale/en-US/actions/commands/addaction.ftl @@ -0,0 +1,11 @@ +# addaction +cmd-addaction-desc = Adds an action to the target entity. The action will not work if the target requires an additional component on their entity (such as Dragon's Devour). +cmd-addaction-help = addaction + +cmd-addaction-invalid-args = Expected exactly 2 arguments. +cmd-addaction-actions-not-found = Target entity cannot use actions. +cmd-addaction-action-not-found = Can't find matching action prototype {$action}. +cmd-addaction-adding-failed = Failed to add the action. + +cmd-addaction-player-completion = +cmd-addaction-action-completion = diff --git a/Resources/Locale/en-US/actions/commands/rmaction.ftl b/Resources/Locale/en-US/actions/commands/rmaction.ftl new file mode 100644 index 0000000000..0226d3d17c --- /dev/null +++ b/Resources/Locale/en-US/actions/commands/rmaction.ftl @@ -0,0 +1,12 @@ +# rmaction +cmd-rmaction-desc = Removes an action from an entity. +cmd-rmaction-help = rmaction + +cmd-rmaction-invalid-args = Expected exactly 2 arguments. +cmd-rmaction-actions-not-found = Target entity cannot use actions. +cmd-rmaction-not-an-action = Target entity is not an action. + +cmd-rmaction-player-completion = +cmd-rmaction-action-completion = + +cmd-rmaction-action-info = {$action} -- 2.51.2