Files
crystall-punk-14/Content.Server/Commands/CommandUtils.cs
TemporalOroboros f284b43ff6 Fixes obsolete Transform warnings in Content. (#25256)
* Fix TransformComponent.MapPosition warnings in Content.Client

* Fix TransformComponent.MapPosition warnings in Content.IntegrationTests

* Fix TransformComponent.MapPosition warnings in Content.Shared

* Fix TransformComponent.MapPosition warnings in Content.Server

* Fix TransformComponent.WorldPosition warnings in Content.Shared

* Fix TransformComponent.WorldPosition warnings in Content.Client
Excepts ClickableComponent b/c that needs to be ECS'd entirely later

* Fix TransformComponent.WorldPosition warnings in Content.Server

* Fix TransformComponent.WorldRotation warnings in Content.*

* Fix TransformComponent.MapPosition warnings I missed

* Fix TransformComponent.WorldMatrix warnings in Content.*

* Fix TransformComponent.InvWorldMatrix warnings in Content.*

* Fix TransformComponent.GetWorldPositionRotationMatrixWithInv warnings in Content.*

* Fix TransformComponent.GetWorldPositionRotationMatrix warnings in Content.*

* Fix TransformComponent.GetWorldPositionRotation warnings in Content.*

* Fix TransformComponent.Anchored.set warnings in Content.*

* Fix TransformComponent.Coordinates.set warnings in Content.*

* Fix TransformComponent.LocalPosition.set warnings in Content.*

* Fix TransformComponent.AttachToGridOrMap warnings in Content.*

* Fix TransformComponent.AttachParent warnings in Content.*

* Preempt TransformComponent.LocalRotation.set warnings in Content.Shared

* Preempt TransformComponent.LocalRotation.set warnings in Content.Client

* Preempt TransformComponent.LocalRotation.set warnings in Content.IntegrationTests

* Preempt TransformComponent.LocalRotation.set warnings in Content.Server

* Fix/Preempt the remaining obsolete TransformComponent properties/methods in Content.*

* ECS ClickableComponent

* Fix obsolete SharedTransformSystem methods in Content.*

* Fix ExplosionOverlay `SharedTransformSystem` dependency

* Maybe fix null eye position breaking tests

* MGS requested changes
2024-02-27 12:06:20 +11:00

87 lines
4.0 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.Network;
using Robust.Shared.Player;
namespace Content.Server.Commands
{
/// <summary>
/// Utilities for writing commands
/// </summary>
public static class CommandUtils
{
/// <summary>
/// Gets the player session for the player with the indicated id,
/// sending a failure to the performer if unable to.
/// </summary>
public static bool TryGetSessionByUsernameOrId(IConsoleShell shell,
string usernameOrId, ICommonSession performer, [NotNullWhen(true)] out ICommonSession? session)
{
var plyMgr = IoCManager.Resolve<IPlayerManager>();
if (plyMgr.TryGetSessionByUsername(usernameOrId, out session)) return true;
if (Guid.TryParse(usernameOrId, out var targetGuid))
{
if (plyMgr.TryGetSessionById(new NetUserId(targetGuid), out session)) return true;
shell.WriteLine("Unable to find user with that name/id.");
return false;
}
shell.WriteLine("Unable to find user with that name/id.");
return false;
}
/// <summary>
/// Gets the attached entity for the player session with the indicated id,
/// sending a failure to the performer if unable to.
/// </summary>
public static bool TryGetAttachedEntityByUsernameOrId(IConsoleShell shell,
string usernameOrId, ICommonSession performer, out EntityUid attachedEntity)
{
attachedEntity = default;
if (!TryGetSessionByUsernameOrId(shell, usernameOrId, performer, out var session)) return false;
if (session.AttachedEntity == null)
{
shell.WriteLine("User has no attached entity.");
return false;
}
attachedEntity = session.AttachedEntity.Value;
return true;
}
public static string SubstituteEntityDetails(IConsoleShell shell, EntityUid ent, string ruleString)
{
var entMan = IoCManager.Resolve<IEntityManager>();
var xfmSys = entMan.System<SharedTransformSystem>();
var transform = entMan.GetComponent<TransformComponent>(ent);
var worldPosition = xfmSys.GetWorldPosition(transform);
// gross, is there a better way to do this?
ruleString = ruleString.Replace("$ID", ent.ToString());
ruleString = ruleString.Replace("$WX", worldPosition.X.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$WY", worldPosition.Y.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$LX", transform.LocalPosition.X.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$LY", transform.LocalPosition.Y.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$NAME", entMan.GetComponent<MetaDataComponent>(ent).EntityName);
if (shell.Player is { } player)
{
if (player.AttachedEntity is {Valid: true} p)
{
var pTransform = entMan.GetComponent<TransformComponent>(p);
var pPosition = xfmSys.GetWorldPosition(pTransform);
ruleString = ruleString.Replace("$PID", ent.ToString());
ruleString = ruleString.Replace("$PWX", pPosition.X.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$PWY", pPosition.Y.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$PLX", pTransform.LocalPosition.X.ToString(CultureInfo.InvariantCulture));
ruleString = ruleString.Replace("$PLY", pTransform.LocalPosition.Y.ToString(CultureInfo.InvariantCulture));
}
}
return ruleString;
}
}
}