Make aghost command work on other players using optional argument (#26546)

* Translations

* Make aghost command work on other players using optional argument

* Reviews
This commit is contained in:
Simon
2024-03-31 04:05:44 +02:00
committed by GitHub
parent c1b5576cc2
commit 87a56b25c3
3 changed files with 115 additions and 61 deletions

View File

@@ -1,38 +1,89 @@
using Content.Server.GameTicking; using System.Linq;
using Content.Server.GameTicking;
using Content.Server.Ghost;
using Content.Server.Mind;
using Content.Shared.Administration; using Content.Shared.Administration;
using Content.Shared.Ghost; using Content.Shared.Ghost;
using Content.Shared.Mind; using Content.Shared.Mind;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Console; using Robust.Shared.Console;
namespace Content.Server.Administration.Commands namespace Content.Server.Administration.Commands;
{
[AdminCommand(AdminFlags.Admin)] [AdminCommand(AdminFlags.Admin)]
public sealed class AGhost : IConsoleCommand public sealed class AGhost : LocalizedCommands
{ {
[Dependency] private readonly IEntityManager _entities = default!; [Dependency] private readonly IEntityManager _entities = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
public string Command => "aghost"; public override string Command => "aghost";
public string Description => "Makes you an admin ghost."; public override string Description => LocalizationManager.GetString("aghost-description");
public string Help => "aghost"; public override string Help => "aghost";
public void Execute(IConsoleShell shell, string argStr, string[] args) public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{ {
if (args.Length == 1)
{
var names = _playerManager.Sessions.OrderBy(c => c.Name).Select(c => c.Name).ToArray();
return CompletionResult.FromHintOptions(names, LocalizationManager.GetString("shell-argument-username-optional-hint"));
}
return CompletionResult.Empty;
}
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length > 1)
{
shell.WriteError(LocalizationManager.GetString("shell-wrong-arguments-number"));
return;
}
var player = shell.Player; var player = shell.Player;
var self = player != null;
if (player == null) if (player == null)
{ {
shell.WriteLine("Nah"); // If you are not a player, you require a player argument.
if (args.Length == 0)
{
shell.WriteError(LocalizationManager.GetString("shell-need-exactly-one-argument"));
return; return;
} }
var didFind = _playerManager.TryGetSessionByUsername(args[0], out player);
if (!didFind)
{
shell.WriteError(LocalizationManager.GetString("shell-target-player-does-not-exist"));
return;
}
}
// If you are a player and a username is provided, a lookup is done to find the target player.
if (args.Length == 1)
{
var didFind = _playerManager.TryGetSessionByUsername(args[0], out player);
if (!didFind)
{
shell.WriteError(LocalizationManager.GetString("shell-target-player-does-not-exist"));
return;
}
}
var mindSystem = _entities.System<SharedMindSystem>(); var mindSystem = _entities.System<SharedMindSystem>();
var metaDataSystem = _entities.System<MetaDataSystem>();
var ghostSystem = _entities.System<SharedGhostSystem>();
var transformSystem = _entities.System<TransformSystem>();
var gameTicker = _entities.System<GameTicker>();
if (!mindSystem.TryGetMind(player, out var mindId, out var mind)) if (!mindSystem.TryGetMind(player, out var mindId, out var mind))
{ {
shell.WriteLine("You can't ghost here!"); shell.WriteError(self
? LocalizationManager.GetString("aghost-no-mind-self")
: LocalizationManager.GetString("aghost-no-mind-other"));
return; return;
} }
var metaDataSystem = _entities.System<MetaDataSystem>();
if (mind.VisitingEntity != default && _entities.TryGetComponent<GhostComponent>(mind.VisitingEntity, out var oldGhostComponent)) if (mind.VisitingEntity != default && _entities.TryGetComponent<GhostComponent>(mind.VisitingEntity, out var oldGhostComponent))
{ {
mindSystem.UnVisit(mindId, mind); mindSystem.UnVisit(mindId, mind);
@@ -43,11 +94,11 @@ namespace Content.Server.Administration.Commands
var canReturn = mind.CurrentEntity != null var canReturn = mind.CurrentEntity != null
&& !_entities.HasComponent<GhostComponent>(mind.CurrentEntity); && !_entities.HasComponent<GhostComponent>(mind.CurrentEntity);
var coordinates = player.AttachedEntity != null var coordinates = player!.AttachedEntity != null
? _entities.GetComponent<TransformComponent>(player.AttachedEntity.Value).Coordinates ? _entities.GetComponent<TransformComponent>(player.AttachedEntity.Value).Coordinates
: EntitySystem.Get<GameTicker>().GetObserverSpawnPoint(); : gameTicker.GetObserverSpawnPoint();
var ghost = _entities.SpawnEntity(GameTicker.AdminObserverPrototypeName, coordinates); var ghost = _entities.SpawnEntity(GameTicker.AdminObserverPrototypeName, coordinates);
_entities.GetComponent<TransformComponent>(ghost).AttachToGridOrMap(); transformSystem.AttachToGridOrMap(ghost, _entities.GetComponent<TransformComponent>(ghost));
if (canReturn) if (canReturn)
{ {
@@ -66,7 +117,6 @@ namespace Content.Server.Administration.Commands
} }
var comp = _entities.GetComponent<GhostComponent>(ghost); var comp = _entities.GetComponent<GhostComponent>(ghost);
EntitySystem.Get<SharedGhostSystem>().SetCanReturnToBody(comp, canReturn); ghostSystem.SetCanReturnToBody(comp, canReturn);
}
} }
} }

View File

@@ -0,0 +1,3 @@
aghost-description = Makes you an admin ghost.
aghost-no-mind-self = You can't ghost here!
aghost-no-mind-other = They can't ghost here!

View File

@@ -47,3 +47,4 @@ shell-argument-number-invalid = Argument {$index} must be a valid number!
# Hints # Hints
shell-argument-username-hint = <username> shell-argument-username-hint = <username>
shell-argument-username-optional-hint = [username]