using System.Diagnostics.CodeAnalysis; using Content.Server.Access.Components; using Content.Server.Clothing.Systems; using Content.Server.Implants; using Content.Server.Popups; using Content.Shared.Access.Components; using Content.Shared.Access.Systems; using Content.Shared.Clothing.Components; using Content.Shared.Implants; using Content.Shared.Interaction; using Content.Shared.Inventory; using Content.Shared.Lock; using Content.Shared.PDA; using Content.Shared.Roles; using Content.Shared.StatusIcon; using Content.Shared.UserInterface; using Content.Shared.VoiceMask; using Robust.Server.GameObjects; namespace Content.Server.Access.Systems { public sealed partial class AgentIDCardSystem : SharedAgentIdCardSystem { [Dependency] private PopupSystem _popupSystem = default!; [Dependency] private IdCardSystem _cardSystem = default!; [Dependency] private UserInterfaceSystem _uiSystem = default!; [Dependency] private ChameleonClothingSystem _chameleon = default!; [Dependency] private ChameleonControllerSystem _chamController = default!; [Dependency] private LockSystem _lock = default!; [Dependency] private SharedJobStatusSystem _jobStatus = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnAfterInteract); // BUI SubscribeLocalEvent(AfterUIOpen); SubscribeLocalEvent(OnNameChanged); SubscribeLocalEvent(OnJobChanged); SubscribeLocalEvent(OnJobIconChanged); SubscribeLocalEvent>(OnChameleonControllerOutfitChangedItem); SubscribeLocalEvent>(OnVoiceMaskNameChanged); } private void OnChameleonControllerOutfitChangedItem(Entity ent, ref InventoryRelayedEvent args) { if (!TryComp(ent, out var idCardComp)) return; ProtoMan.Resolve(args.Args.ChameleonOutfit.Job, out var jobProto); var jobIcon = args.Args.ChameleonOutfit.Icon ?? jobProto?.Icon; var jobName = args.Args.ChameleonOutfit.Name ?? jobProto?.Name ?? ""; if (jobIcon != null) _cardSystem.TryChangeJobIcon(ent, ProtoMan.Index(jobIcon.Value), idCardComp); if (jobName != "") _cardSystem.TryChangeJobTitle(ent, Loc.GetString(jobName), idCardComp); // If you have forced departments use those over the jobs actual departments. if (args.Args.ChameleonOutfit?.Departments?.Count > 0) _cardSystem.TryChangeJobDepartment(ent, args.Args.ChameleonOutfit.Departments, idCardComp); else if (jobProto != null) _cardSystem.TryChangeJobDepartment(ent, jobProto, idCardComp); // Ensure that you chameleon IDs in PDAs correctly. Yes this is sus... // There is one weird interaction: If the job / icon don't match the PDAs job the chameleon will be updated // to the PDAs IDs sprite but the icon and job title will not match. There isn't a way to get around this // really as there is no tie between job -> pda or pda -> job. var idSlotGear = _chamController.GetGearForSlot(args, "id"); if (idSlotGear == null) return; var proto = ProtoMan.Index(idSlotGear); if (!proto.TryComp(out var comp, EntityManager.ComponentFactory)) return; if (TryComp(ent, out var chameleonComp) && chameleonComp.CanBeSetByController) _chameleon.SetSelectedPrototype(ent, comp.IdCard, component: chameleonComp); } private void OnVoiceMaskNameChanged(Entity ent, ref InventoryRelayedEvent args) { if (!TryComp(ent, out var idCard)) return; if (!args.Args.VoiceMask.Comp.ChangeIDName) return; _cardSystem.TryChangeFullName(ent, args.Args.NewName, idCard); } private void OnAfterInteract(EntityUid uid, AgentIDCardComponent component, AfterInteractEvent args) { if (args.Target == null || !args.CanReach || _lock.IsLocked(uid) || !TryComp(args.Target, out var targetAccess) || !HasComp(args.Target)) return; if (!TryComp(uid, out var access) || !HasComp(uid)) return; var beforeLength = access.Tags.Count; access.Tags.UnionWith(targetAccess.Tags); var addedLength = access.Tags.Count - beforeLength; _popupSystem.PopupEntity(Loc.GetString("agent-id-new", ("number", addedLength), ("card", args.Target)), args.Target.Value, args.User); if (addedLength > 0) Dirty(uid, access); } private void AfterUIOpen(EntityUid uid, AgentIDCardComponent component, AfterActivatableUIOpenEvent args) { if (!_uiSystem.HasUi(uid, AgentIDCardUiKey.Key)) return; if (!TryComp(uid, out var idCard)) return; var state = new AgentIDCardBoundUserInterfaceState(idCard.FullName ?? "", idCard.LocalizedJobTitle ?? "", idCard.JobIcon); _uiSystem.SetUiState(uid, AgentIDCardUiKey.Key, state); } private void OnJobChanged(EntityUid uid, AgentIDCardComponent comp, AgentIDCardJobChangedMessage args) { if (!TryComp(uid, out var idCard)) return; _cardSystem.TryChangeJobTitle(uid, args.Job, idCard); } private void OnNameChanged(EntityUid uid, AgentIDCardComponent comp, AgentIDCardNameChangedMessage args) { if (!TryComp(uid, out var idCard)) return; _cardSystem.TryChangeFullName(uid, args.Name, idCard); } private void OnJobIconChanged(EntityUid uid, AgentIDCardComponent comp, AgentIDCardJobIconChangedMessage args) { if (!TryComp(uid, out var idCard)) return; if (!ProtoMan.Resolve(args.JobIconId, out var jobIcon)) return; _cardSystem.TryChangeJobIcon(uid, jobIcon, idCard); if (TryFindJobProtoFromIcon(jobIcon, out var job)) _cardSystem.TryChangeJobDepartment(uid, job, idCard); _jobStatus.UpdateStatus(Transform(uid).ParentUid); } private bool TryFindJobProtoFromIcon(JobIconPrototype jobIcon, [NotNullWhen(true)] out JobPrototype? job) { foreach (var jobPrototype in ProtoMan.EnumeratePrototypes()) { if (jobPrototype.Icon == jobIcon.ID) { job = jobPrototype; return true; } } job = null; return false; } } }