Move Access to ECS (#4826)
* Moved access to ecs * Fixed tests * Moved test to integration * Better IoC * Moved preset ID card * Moved id card to ECS * Moved access component to ECS * Fixed pda access * Final touches Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
70
Content.Server/Access/Systems/IdCardSystem.cs
Normal file
70
Content.Server/Access/Systems/IdCardSystem.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Content.Server.Access.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
|
||||
namespace Content.Server.Access.Systems
|
||||
{
|
||||
public class IdCardSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<IdCardComponent, ComponentInit>(OnInit);
|
||||
}
|
||||
|
||||
private void OnInit(EntityUid uid, IdCardComponent id, ComponentInit args)
|
||||
{
|
||||
id.OriginalOwnerName ??= id.Owner.Name;
|
||||
UpdateEntityName(uid, id);
|
||||
}
|
||||
|
||||
public bool TryChangeJobTitle(EntityUid uid, string jobTitle, IdCardComponent? id = null)
|
||||
{
|
||||
if (!Resolve(uid, ref id))
|
||||
return false;
|
||||
|
||||
id.JobTitle = jobTitle;
|
||||
UpdateEntityName(uid, id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryChangeFullName(EntityUid uid, string fullName, IdCardComponent? id = null)
|
||||
{
|
||||
if (!Resolve(uid, ref id))
|
||||
return false;
|
||||
|
||||
id.FullName = fullName;
|
||||
UpdateEntityName(uid, id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the <see cref="Entity.Name"/> of <see cref="Component.Owner"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If either <see cref="FullName"/> or <see cref="JobTitle"/> is empty, it's replaced by placeholders.
|
||||
/// If both are empty, the original entity's name is restored.
|
||||
/// </remarks>
|
||||
private void UpdateEntityName(EntityUid uid, IdCardComponent? id = null)
|
||||
{
|
||||
if (!Resolve(uid, ref id))
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(id.FullName) && string.IsNullOrWhiteSpace(id.JobTitle))
|
||||
{
|
||||
id.Owner.Name = id.OriginalOwnerName;
|
||||
return;
|
||||
}
|
||||
|
||||
var jobSuffix = string.IsNullOrWhiteSpace(id.JobTitle) ? string.Empty : $" ({id.JobTitle})";
|
||||
|
||||
id.Owner.Name = string.IsNullOrWhiteSpace(id.FullName)
|
||||
? Loc.GetString("access-id-card-component-owner-name-job-title-text",
|
||||
("originalOwnerName", id.OriginalOwnerName),
|
||||
("jobSuffix", jobSuffix))
|
||||
: Loc.GetString("access-id-card-component-owner-full-name-job-title-text",
|
||||
("fullName", id.FullName),
|
||||
("jobSuffix", jobSuffix));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user