From b276524468670287341acac3eb6f7bcf5c4f7563 Mon Sep 17 00:00:00 2001 From: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Date: Tue, 8 Apr 2025 16:54:13 +0200 Subject: [PATCH] fix rmobjective command and add completion options (#36396) fix rmobjective command --- .../Commands/RemoveObjectiveCommand.cs | 65 ++++++++++++++----- Content.Shared/Mind/SharedMindSystem.cs | 2 +- .../en-US/objectives/commands/rmobjective.ftl | 14 ++++ 3 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 Resources/Locale/en-US/objectives/commands/rmobjective.ftl diff --git a/Content.Server/Objectives/Commands/RemoveObjectiveCommand.cs b/Content.Server/Objectives/Commands/RemoveObjectiveCommand.cs index b174ca94b6..4b300cd195 100644 --- a/Content.Server/Objectives/Commands/RemoveObjectiveCommand.cs +++ b/Content.Server/Objectives/Commands/RemoveObjectiveCommand.cs @@ -1,52 +1,81 @@ using Content.Server.Administration; using Content.Shared.Administration; using Content.Shared.Mind; +using Content.Shared.Objectives.Systems; using Robust.Server.Player; using Robust.Shared.Console; namespace Content.Server.Objectives.Commands { [AdminCommand(AdminFlags.Admin)] - public sealed class RemoveObjectiveCommand : IConsoleCommand + public sealed class RemoveObjectiveCommand : LocalizedEntityCommands { - [Dependency] private readonly IEntityManager _entityManager = default!; + [Dependency] private readonly IPlayerManager _players = default!; + [Dependency] private readonly SharedMindSystem _mind = default!; + [Dependency] private readonly SharedObjectivesSystem _objectives = default!; - public string Command => "rmobjective"; - public string Description => "Removes an objective from the player's mind."; - public string Help => "rmobjective "; - public void Execute(IConsoleShell shell, string argStr, string[] args) + public override string Command => "rmobjective"; + public override void Execute(IConsoleShell shell, string argStr, string[] args) { if (args.Length != 2) { - shell.WriteLine("Expected exactly 2 arguments."); + shell.WriteError(Loc.GetString(Loc.GetString("cmd-rmobjective-invalid-args"))); return; } - var mgr = IoCManager.Resolve(); - var minds = _entityManager.System(); - if (!mgr.TryGetSessionByUsername(args[0], out var session)) + if (!_players.TryGetSessionByUsername(args[0], out var session)) { - shell.WriteLine("Can't find the playerdata."); + shell.WriteError(Loc.GetString("cmd-rmojective-player-not-found")); return; } - if (!minds.TryGetMind(session, out var mindId, out var mind)) + if (!_mind.TryGetMind(session, out var mindId, out var mind)) { - shell.WriteLine("Can't find the mind."); + shell.WriteError(Loc.GetString("cmd-rmojective-mind-not-found")); return; } if (int.TryParse(args[1], out var i)) { - var mindSystem = _entityManager.System(); - shell.WriteLine(mindSystem.TryRemoveObjective(mindId, mind, i) - ? "Objective successfully removed!" - : "Objective removing failed. Maybe the index is out of bounds? Check lsobjectives!"); + shell.WriteLine(Loc.GetString(_mind.TryRemoveObjective(mindId, mind, i) + ? "cmd-rmobjective-success" + : "cmd-rmobjective-failed")); } else { - shell.WriteLine($"Invalid index {args[1]}!"); + shell.WriteError(Loc.GetString("cmd-rmobjective-invalid-index", ("index", args[1]))); } } + + public override CompletionResult GetCompletion(IConsoleShell shell, string[] args) + { + if (args.Length == 1) + { + return CompletionResult.FromHintOptions(CompletionHelper.SessionNames(), LocalizationManager.GetString("shell-argument-username-hint")); + } + if (args.Length == 2) + { + if (!_players.TryGetSessionByUsername(args[0], out var session)) + return CompletionResult.Empty; + + if (!_mind.TryGetMind(session, out var mindId, out var mind)) + return CompletionResult.Empty; + + if (mind.Objectives.Count == 0) + return CompletionResult.Empty; + + var options = new List(); + for (int i = 0; i < mind.Objectives.Count; i++) + { + var info = _objectives.GetInfo(mind.Objectives[i], mindId, mind); + var hint = info == null ? Loc.GetString("cmd-rmobjective-invalid-objective-info") : $"{mind.Objectives[i]} ({info.Value.Title})"; + options.Add(new CompletionOption(i.ToString(), hint)); + } + + return CompletionResult.FromOptions(options); + } + + return CompletionResult.Empty; + } } } diff --git a/Content.Shared/Mind/SharedMindSystem.cs b/Content.Shared/Mind/SharedMindSystem.cs index b2e053f5f1..1b1fcabc98 100644 --- a/Content.Shared/Mind/SharedMindSystem.cs +++ b/Content.Shared/Mind/SharedMindSystem.cs @@ -371,7 +371,7 @@ public abstract partial class SharedMindSystem : EntitySystem // garbage collection - only delete the objective entity if no mind uses it anymore // This comes up for stuff like paradox clones where the objectives share the same entity - var mindQuery = new AllEntityQueryEnumerator(); + var mindQuery = AllEntityQuery(); while (mindQuery.MoveNext(out _, out var queryComp)) { if (queryComp.Objectives.Contains(objective)) diff --git a/Resources/Locale/en-US/objectives/commands/rmobjective.ftl b/Resources/Locale/en-US/objectives/commands/rmobjective.ftl new file mode 100644 index 0000000000..82640f660d --- /dev/null +++ b/Resources/Locale/en-US/objectives/commands/rmobjective.ftl @@ -0,0 +1,14 @@ +# addobjectives +cmd-rmobjective-desc = Removes an objective from the player's mind. +cmd-rmobjective-help = rmobjective + +cmd-rmobjective-invalid-args = Expected exactly 2 arguments. +cmd-rmobjective-player-not-found = Can't find the playerdata. +cmd-rmobjective-mind-not-found = Can't find the mind. +cmd-rmobjective-success = Objective successfully removed! +cmd-rmobjective-failed = Objective removing failed. Maybe the index is out of bounds? Check lsobjectives! +cmd-rmobjective-invalid-index = Could not parse index { $index } as an integer. +cmd-rmobjective-invalid-objective-info = INVALID + +cmd-rmobjective-player-completion = +cmd-rmobjective-index-completion = -- 2.51.2