ChameleonClothingSystem.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Content.Server.IdentityManagement;
  2. using Content.Shared.Clothing.Components;
  3. using Content.Shared.Clothing.EntitySystems;
  4. using Content.Shared.IdentityManagement.Components;
  5. using Content.Shared.Prototypes;
  6. using Robust.Shared.Prototypes;
  7. namespace Content.Server.Clothing.Systems;
  8. public sealed class ChameleonClothingSystem : SharedChameleonClothingSystem
  9. {
  10. [Dependency] private readonly IPrototypeManager _proto = default!;
  11. [Dependency] private readonly IComponentFactory _factory = default!;
  12. [Dependency] private readonly IdentitySystem _identity = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<ChameleonClothingComponent, MapInitEvent>(OnMapInit);
  17. SubscribeLocalEvent<ChameleonClothingComponent, ChameleonPrototypeSelectedMessage>(OnSelected);
  18. }
  19. private void OnMapInit(EntityUid uid, ChameleonClothingComponent component, MapInitEvent args)
  20. {
  21. SetSelectedPrototype(uid, component.Default, true, component);
  22. }
  23. private void OnSelected(EntityUid uid, ChameleonClothingComponent component, ChameleonPrototypeSelectedMessage args)
  24. {
  25. SetSelectedPrototype(uid, args.SelectedId, component: component);
  26. }
  27. private void UpdateUi(EntityUid uid, ChameleonClothingComponent? component = null)
  28. {
  29. if (!Resolve(uid, ref component))
  30. return;
  31. var state = new ChameleonBoundUserInterfaceState(component.Slot, component.Default, component.RequireTag);
  32. UI.SetUiState(uid, ChameleonUiKey.Key, state);
  33. }
  34. /// <summary>
  35. /// Change chameleon items name, description and sprite to mimic other entity prototype.
  36. /// </summary>
  37. public void SetSelectedPrototype(EntityUid uid, string? protoId, bool forceUpdate = false,
  38. ChameleonClothingComponent? component = null)
  39. {
  40. if (!Resolve(uid, ref component, false))
  41. return;
  42. // check that wasn't already selected
  43. // forceUpdate on component init ignores this check
  44. if (component.Default == protoId && !forceUpdate)
  45. return;
  46. // make sure that it is valid change
  47. if (string.IsNullOrEmpty(protoId) || !_proto.TryIndex(protoId, out EntityPrototype? proto))
  48. return;
  49. if (!IsValidTarget(proto, component.Slot, component.RequireTag))
  50. return;
  51. component.Default = protoId;
  52. UpdateIdentityBlocker(uid, component, proto);
  53. UpdateVisuals(uid, component);
  54. UpdateUi(uid, component);
  55. Dirty(uid, component);
  56. }
  57. private void UpdateIdentityBlocker(EntityUid uid, ChameleonClothingComponent component, EntityPrototype proto)
  58. {
  59. if (proto.HasComponent<IdentityBlockerComponent>(_factory))
  60. EnsureComp<IdentityBlockerComponent>(uid);
  61. else
  62. RemComp<IdentityBlockerComponent>(uid);
  63. if (component.User != null)
  64. _identity.QueueIdentityUpdate(component.User.Value);
  65. }
  66. }