LabelSystem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Content.Server.Labels.Components;
  2. using Content.Shared.Containers.ItemSlots;
  3. using Content.Shared.Examine;
  4. using Content.Shared.Labels;
  5. using Content.Shared.Labels.Components;
  6. using Content.Shared.Labels.EntitySystems;
  7. using Content.Shared.Paper;
  8. using JetBrains.Annotations;
  9. using Robust.Shared.Containers;
  10. namespace Content.Server.Labels
  11. {
  12. /// <summary>
  13. /// A system that lets players see the contents of a label on an object.
  14. /// </summary>
  15. [UsedImplicitly]
  16. public sealed class LabelSystem : SharedLabelSystem
  17. {
  18. [Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
  19. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  20. public const string ContainerName = "paper_label";
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. SubscribeLocalEvent<PaperLabelComponent, ComponentInit>(OnComponentInit);
  25. SubscribeLocalEvent<PaperLabelComponent, ComponentRemove>(OnComponentRemove);
  26. SubscribeLocalEvent<PaperLabelComponent, EntInsertedIntoContainerMessage>(OnContainerModified);
  27. SubscribeLocalEvent<PaperLabelComponent, EntRemovedFromContainerMessage>(OnContainerModified);
  28. SubscribeLocalEvent<PaperLabelComponent, ExaminedEvent>(OnExamined);
  29. }
  30. /// <summary>
  31. /// Apply or remove a label on an entity.
  32. /// </summary>
  33. /// <param name="uid">EntityUid to change label on</param>
  34. /// <param name="text">intended label text (null to remove)</param>
  35. /// <param name="label">label component for resolve</param>
  36. /// <param name="metadata">metadata component for resolve</param>
  37. public override void Label(EntityUid uid, string? text, MetaDataComponent? metadata = null, LabelComponent? label = null)
  38. {
  39. if (!Resolve(uid, ref label, false))
  40. label = EnsureComp<LabelComponent>(uid);
  41. label.CurrentLabel = text;
  42. NameMod.RefreshNameModifiers(uid);
  43. Dirty(uid, label);
  44. }
  45. private void OnComponentInit(EntityUid uid, PaperLabelComponent component, ComponentInit args)
  46. {
  47. _itemSlotsSystem.AddItemSlot(uid, ContainerName, component.LabelSlot);
  48. UpdateAppearance((uid, component));
  49. }
  50. private void OnComponentRemove(EntityUid uid, PaperLabelComponent component, ComponentRemove args)
  51. {
  52. _itemSlotsSystem.RemoveItemSlot(uid, component.LabelSlot);
  53. }
  54. private void OnExamined(EntityUid uid, PaperLabelComponent comp, ExaminedEvent args)
  55. {
  56. if (comp.LabelSlot.Item is not {Valid: true} item)
  57. return;
  58. using (args.PushGroup(nameof(PaperLabelComponent)))
  59. {
  60. if (!args.IsInDetailsRange)
  61. {
  62. args.PushMarkup(Loc.GetString("comp-paper-label-has-label-cant-read"));
  63. return;
  64. }
  65. if (!EntityManager.TryGetComponent(item, out PaperComponent? paper))
  66. // Assuming yaml has the correct entity whitelist, this should not happen.
  67. return;
  68. if (string.IsNullOrWhiteSpace(paper.Content))
  69. {
  70. args.PushMarkup(Loc.GetString("comp-paper-label-has-label-blank"));
  71. return;
  72. }
  73. args.PushMarkup(Loc.GetString("comp-paper-label-has-label"));
  74. var text = paper.Content;
  75. args.PushMarkup(text.TrimEnd());
  76. }
  77. }
  78. private void OnContainerModified(EntityUid uid, PaperLabelComponent label, ContainerModifiedMessage args)
  79. {
  80. if (!label.Initialized) return;
  81. if (args.Container.ID != label.LabelSlot.ID)
  82. return;
  83. UpdateAppearance((uid, label));
  84. }
  85. private void UpdateAppearance(Entity<PaperLabelComponent, AppearanceComponent?> ent)
  86. {
  87. if (!Resolve(ent, ref ent.Comp2, false))
  88. return;
  89. var slot = ent.Comp1.LabelSlot;
  90. _appearance.SetData(ent, PaperLabelVisuals.HasLabel, slot.HasItem, ent.Comp2);
  91. if (TryComp<PaperLabelTypeComponent>(slot.Item, out var type))
  92. _appearance.SetData(ent, PaperLabelVisuals.LabelType, type.PaperType, ent.Comp2);
  93. }
  94. }
  95. }