-using Content.Server.Administration;
+using System.Linq;
+using Content.Server.Administration;
using Content.Shared.Administration;
using Content.Shared.Mind;
using Content.Shared.Objectives.Components;
+using Content.Shared.Prototypes;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.Prototypes;
-namespace Content.Server.Objectives.Commands
+namespace Content.Server.Objectives.Commands;
+
+[AdminCommand(AdminFlags.Admin)]
+public sealed class AddObjectiveCommand : LocalizedEntityCommands
{
- [AdminCommand(AdminFlags.Admin)]
- public sealed class AddObjectiveCommand : IConsoleCommand
+ [Dependency] private readonly IPlayerManager _players = default!;
+ [Dependency] private readonly IPrototypeManager _prototypes = default!;
+ [Dependency] private readonly SharedMindSystem _mind = default!;
+ [Dependency] private readonly ObjectivesSystem _objectives = default!;
+
+ public override string Command => "addobjective";
+
+ public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
- [Dependency] private readonly IEntityManager _entityManager = default!;
+ if (args.Length != 2)
+ {
+ shell.WriteError(Loc.GetString(Loc.GetString("cmd-addobjective-invalid-args")));
+ return;
+ }
+
+ if (!_players.TryGetSessionByUsername(args[0], out var data))
+ {
+ shell.WriteError(Loc.GetString("cmd-addobjective-player-not-found"));
+ return;
+ }
+
+ if (!_mind.TryGetMind(data, out var mindId, out var mind))
+ {
+ shell.WriteError(Loc.GetString("cmd-addobjective-mind-not-found"));
+ return;
+ }
- public string Command => "addobjective";
- public string Description => "Adds an objective to the player's mind.";
- public string Help => "addobjective <username> <objectiveID>";
- public void Execute(IConsoleShell shell, string argStr, string[] args)
+ if (!_prototypes.TryIndex<EntityPrototype>(args[1], out var proto) ||
+ !proto.HasComponent<ObjectiveComponent>())
{
- if (args.Length != 2)
- {
- shell.WriteLine("Expected exactly 2 arguments.");
- return;
- }
-
- var mgr = IoCManager.Resolve<IPlayerManager>();
- if (!mgr.TryGetSessionByUsername(args[0], out var data))
- {
- shell.WriteLine("Can't find the playerdata.");
- return;
- }
-
- var minds = _entityManager.System<SharedMindSystem>();
- if (!minds.TryGetMind(data, out var mindId, out var mind))
- {
- shell.WriteLine("Can't find the mind.");
- return;
- }
-
- if (!IoCManager.Resolve<IPrototypeManager>()
- .TryIndex<EntityPrototype>(args[1], out var proto) ||
- !proto.TryGetComponent<ObjectiveComponent>(out _))
- {
- shell.WriteLine($"Can't find matching objective prototype {args[1]}");
- return;
- }
-
- if (!minds.TryAddObjective(mindId, mind, args[1]))
- {
- // can fail for other reasons so dont pretend to be right
- shell.WriteLine("Failed to add the objective. Maybe requirements dont allow that objective to be added.");
- }
+ shell.WriteError(Loc.GetString("cmd-addobjective-objective-not-found", ("obj", args[1])));
+ return;
}
+
+ if (!_mind.TryAddObjective(mindId, mind, args[1]))
+ {
+ // can fail for other reasons so dont pretend to be right
+ shell.WriteError(Loc.GetString("cmd-addobjective-adding-failed"));
+ }
+ }
+
+ public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
+ {
+ if (args.Length == 1)
+ {
+ var options = _players.Sessions.OrderBy(c => c.Name).Select(c => c.Name).ToArray();
+
+ return CompletionResult.FromHintOptions(options, Loc.GetString("cmd-addobjective-player-completion"));
+ }
+
+ if (args.Length != 2)
+ return CompletionResult.Empty;
+
+ return CompletionResult.FromHintOptions(
+ _objectives.Objectives(),
+ Loc.GetString(Loc.GetString("cmd-add-objective-obj-completion")));
}
}
using Robust.Shared.Random;
using System.Linq;
using System.Text;
+using Content.Server.Objectives.Commands;
+using Content.Shared.Prototypes;
using Robust.Server.Player;
using Robust.Shared.Utility;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly EmergencyShuttleSystem _emergencyShuttle = default!;
+ private IEnumerable<string>? _objectives;
+
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText);
+
+ _prototypeManager.PrototypesReloaded += CreateCompletions;
+ }
+
+ public override void Shutdown()
+ {
+ base.Shutdown();
+
+ _prototypeManager.PrototypesReloaded -= CreateCompletions;
}
/// <summary>
return Loc.GetString("objectives-player-named", ("name", name));
}
+
+
+ private void CreateCompletions(PrototypesReloadedEventArgs unused)
+ {
+ CreateCompletions();
+ }
+
+ /// <summary>
+ /// Get all objective prototypes by their IDs.
+ /// This is used for completions in <see cref="AddObjectiveCommand"/>
+ /// </summary>
+ public IEnumerable<string> Objectives()
+ {
+ if (_objectives == null)
+ CreateCompletions();
+
+ return _objectives!;
+ }
+
+ private void CreateCompletions()
+ {
+ _objectives = _prototypeManager.EnumeratePrototypes<EntityPrototype>()
+ .Where(p => p.HasComponent<ObjectiveComponent>())
+ .Select(p => p.ID)
+ .Order();
+ }
}
/// <summary>