fix rmobjective command and add completion options (#36396)

fix rmobjective command
This commit is contained in:
slarticodefast
2025-04-08 16:54:13 +02:00
committed by GitHub
parent e6ea77d21b
commit b276524468
3 changed files with 62 additions and 19 deletions

View File

@@ -1,52 +1,81 @@
using Content.Server.Administration; using Content.Server.Administration;
using Content.Shared.Administration; using Content.Shared.Administration;
using Content.Shared.Mind; using Content.Shared.Mind;
using Content.Shared.Objectives.Systems;
using Robust.Server.Player; using Robust.Server.Player;
using Robust.Shared.Console; using Robust.Shared.Console;
namespace Content.Server.Objectives.Commands namespace Content.Server.Objectives.Commands
{ {
[AdminCommand(AdminFlags.Admin)] [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 override string Command => "rmobjective";
public string Description => "Removes an objective from the player's mind."; public override void Execute(IConsoleShell shell, string argStr, string[] args)
public string Help => "rmobjective <username> <index>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{ {
if (args.Length != 2) if (args.Length != 2)
{ {
shell.WriteLine("Expected exactly 2 arguments."); shell.WriteError(Loc.GetString(Loc.GetString("cmd-rmobjective-invalid-args")));
return; return;
} }
var mgr = IoCManager.Resolve<IPlayerManager>(); if (!_players.TryGetSessionByUsername(args[0], out var session))
var minds = _entityManager.System<SharedMindSystem>();
if (!mgr.TryGetSessionByUsername(args[0], out var session))
{ {
shell.WriteLine("Can't find the playerdata."); shell.WriteError(Loc.GetString("cmd-rmojective-player-not-found"));
return; 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; return;
} }
if (int.TryParse(args[1], out var i)) if (int.TryParse(args[1], out var i))
{ {
var mindSystem = _entityManager.System<SharedMindSystem>(); shell.WriteLine(Loc.GetString(_mind.TryRemoveObjective(mindId, mind, i)
shell.WriteLine(mindSystem.TryRemoveObjective(mindId, mind, i) ? "cmd-rmobjective-success"
? "Objective successfully removed!" : "cmd-rmobjective-failed"));
: "Objective removing failed. Maybe the index is out of bounds? Check lsobjectives!");
} }
else 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<CompletionOption>();
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;
}
} }
} }

View File

@@ -371,7 +371,7 @@ public abstract partial class SharedMindSystem : EntitySystem
// garbage collection - only delete the objective entity if no mind uses it anymore // 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 // This comes up for stuff like paradox clones where the objectives share the same entity
var mindQuery = new AllEntityQueryEnumerator<MindComponent>(); var mindQuery = AllEntityQuery<MindComponent>();
while (mindQuery.MoveNext(out _, out var queryComp)) while (mindQuery.MoveNext(out _, out var queryComp))
{ {
if (queryComp.Objectives.Contains(objective)) if (queryComp.Objectives.Contains(objective))

View File

@@ -0,0 +1,14 @@
# addobjectives
cmd-rmobjective-desc = Removes an objective from the player's mind.
cmd-rmobjective-help = rmobjective <username> <index>
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 = <Player>
cmd-rmobjective-index-completion = <Index>