From: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Date: Sun, 8 Sep 2024 07:28:43 +0000 (+0000) Subject: Add completions to addobjective and localize it (#30456) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=4b8bedaeef285ba1b5b042a8bd4a22fb1fa4d277;p=space-station-14.git Add completions to addobjective and localize it (#30456) * Add completions to addobjective and localise it * Cleanup * Fix * Make a manager to handle subscribtion completion options This is so we can unsubscribe to prototype reloads properly * Convert the manager into a system * Move the system into the systems folder I forgor * Merge CompletionsSystem into ObjectivesSystem --- diff --git a/Content.Server/Entry/EntryPoint.cs b/Content.Server/Entry/EntryPoint.cs index 76d1b0deda..aa355d7051 100644 --- a/Content.Server/Entry/EntryPoint.cs +++ b/Content.Server/Entry/EntryPoint.cs @@ -14,6 +14,7 @@ using Content.Server.Info; using Content.Server.IoC; using Content.Server.Maps; using Content.Server.NodeContainer.NodeGroups; +using Content.Server.Objectives; using Content.Server.Players; using Content.Server.Players.JobWhitelist; using Content.Server.Players.PlayTimeTracking; diff --git a/Content.Server/IoC/ServerContentIoC.cs b/Content.Server/IoC/ServerContentIoC.cs index 62a37e5db2..3851f145c4 100644 --- a/Content.Server/IoC/ServerContentIoC.cs +++ b/Content.Server/IoC/ServerContentIoC.cs @@ -14,6 +14,7 @@ using Content.Server.Mapping; using Content.Server.Maps; using Content.Server.MoMMI; using Content.Server.NodeContainer.NodeGroups; +using Content.Server.Objectives; using Content.Server.Players; using Content.Server.Players.JobWhitelist; using Content.Server.Players.PlayTimeTracking; diff --git a/Content.Server/Objectives/Commands/AddObjectiveCommand.cs b/Content.Server/Objectives/Commands/AddObjectiveCommand.cs index f15dbf370c..3eddd45208 100644 --- a/Content.Server/Objectives/Commands/AddObjectiveCommand.cs +++ b/Content.Server/Objectives/Commands/AddObjectiveCommand.cs @@ -1,56 +1,73 @@ -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 "; - public void Execute(IConsoleShell shell, string argStr, string[] args) + if (!_prototypes.TryIndex(args[1], out var proto) || + !proto.HasComponent()) { - if (args.Length != 2) - { - shell.WriteLine("Expected exactly 2 arguments."); - return; - } - - var mgr = IoCManager.Resolve(); - if (!mgr.TryGetSessionByUsername(args[0], out var data)) - { - shell.WriteLine("Can't find the playerdata."); - return; - } - - var minds = _entityManager.System(); - if (!minds.TryGetMind(data, out var mindId, out var mind)) - { - shell.WriteLine("Can't find the mind."); - return; - } - - if (!IoCManager.Resolve() - .TryIndex(args[1], out var proto) || - !proto.TryGetComponent(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"))); } } diff --git a/Content.Server/Objectives/ObjectivesSystem.cs b/Content.Server/Objectives/ObjectivesSystem.cs index 382cb1ab44..61df1414c0 100644 --- a/Content.Server/Objectives/ObjectivesSystem.cs +++ b/Content.Server/Objectives/ObjectivesSystem.cs @@ -11,6 +11,8 @@ using Robust.Shared.Prototypes; 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; @@ -24,11 +26,22 @@ public sealed class ObjectivesSystem : SharedObjectivesSystem [Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly EmergencyShuttleSystem _emergencyShuttle = default!; + private IEnumerable? _objectives; + public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnRoundEndText); + + _prototypeManager.PrototypesReloaded += CreateCompletions; + } + + public override void Shutdown() + { + base.Shutdown(); + + _prototypeManager.PrototypesReloaded -= CreateCompletions; } /// @@ -249,6 +262,32 @@ public sealed class ObjectivesSystem : SharedObjectivesSystem return Loc.GetString("objectives-player-named", ("name", name)); } + + + private void CreateCompletions(PrototypesReloadedEventArgs unused) + { + CreateCompletions(); + } + + /// + /// Get all objective prototypes by their IDs. + /// This is used for completions in + /// + public IEnumerable Objectives() + { + if (_objectives == null) + CreateCompletions(); + + return _objectives!; + } + + private void CreateCompletions() + { + _objectives = _prototypeManager.EnumeratePrototypes() + .Where(p => p.HasComponent()) + .Select(p => p.ID) + .Order(); + } } /// diff --git a/Resources/Locale/en-US/objectives/commands/addobjectives.ftl b/Resources/Locale/en-US/objectives/commands/addobjectives.ftl new file mode 100644 index 0000000000..80fe3017cb --- /dev/null +++ b/Resources/Locale/en-US/objectives/commands/addobjectives.ftl @@ -0,0 +1,12 @@ +# addobjectives +cmd-addobjective-desc = Adds an objective to the player's mind. +cmd-addobjective-help = addobjective + +cmd-addobjective-invalid-args = Expected exactly 2 arguments. +cmd-addobjective-player-not-found = Can't find the playerdata. +cmd-addobjective-mind-not-found = Can't find the mind. +cmd-addobjective-objective-not-found = Can't find matching objective prototype {$obj} +cmd-addobjective-adding-failed = Failed to add the objective. Maybe requirements dont allow that objective to be added. + +cmd-addobjective-player-completion = +cmd-add-objective-obj-completion =