using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using Content.Shared.Containers.ItemSlots; using Content.Shared.Examine; using Content.Shared.Labels.Components; using Content.Shared.NameModifier.EntitySystems; using Content.Shared.Paper; using Robust.Shared.Containers; using Robust.Shared.Utility; namespace Content.Shared.Labels.EntitySystems; public sealed partial class LabelSystem : EntitySystem { [Dependency] private NameModifierSystem _nameModifier = default!; [Dependency] private ItemSlotsSystem _itemSlots = default!; [Dependency] private SharedAppearanceSystem _appearance = default!; public const string ContainerName = "paper_label"; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnLabelCompMapInit); SubscribeLocalEvent(OnLabelShutdown); SubscribeLocalEvent(OnExamine); SubscribeLocalEvent(OnRefreshNameModifiers); SubscribeLocalEvent(OnComponentInit); SubscribeLocalEvent(OnComponentRemove); SubscribeLocalEvent(OnContainerModified); SubscribeLocalEvent(OnContainerModified); SubscribeLocalEvent(OnExamined); } private void OnLabelCompMapInit(Entity ent, ref MapInitEvent args) { if (ent.Comp.LocalizedLabel is { } locId) { ent.Comp.CurrentLabel = Loc.GetString(locId); Dirty(ent); } _nameModifier.RefreshNameModifiers(ent.Owner); } private void OnLabelShutdown(Entity ent, ref ComponentShutdown args) { _nameModifier.RefreshNameModifiers(ent.Owner); } /// /// Add, change, or remove a label on an entity. /// /// /// If is or an empty string, the will be removed. /// /// EntityUid to change label on /// intended label text (null to remove) /// label component for resolve /// metadata component for resolve // TODO - Change signature to `Label(Entity ent, string? text)` public void Label(EntityUid uid, string? text, MetaDataComponent? metadata = null, LabelComponent? label = null) { // If setting the label to be blank, just remove the label. if (string.IsNullOrEmpty(text)) { RemoveLabel((uid, label)); return; } label = EnsureComp(uid); label.CurrentLabel = FormattedMessage.EscapeText(text); _nameModifier.RefreshNameModifiers(uid); Dirty(uid, label); } /// /// Removes the label from an entity. /// /// The entity from which the label should be removed. /// true if a label was removed, or false if the entity already didn't have a label. public bool RemoveLabel(Entity ent) { return RemComp(ent); } /// /// Returns the text of the label from an entity, or if it doesn't have a label. /// /// The entity from which to get the label text. [Pure] public string? GetLabelText(Entity ent) { if (!Resolve(ent, ref ent.Comp, logMissing: false)) return null; return ent.Comp.CurrentLabel; } /// /// Returns true if an entity has a visible label. /// /// The entity to check for a label. [Pure] public bool HasLabel(EntityUid ent) { return HasComp(ent); } private void OnExamine(Entity ent, ref ExaminedEvent args) { if (!ent.Comp.Examinable) return; if (ent.Comp.CurrentLabel == null) return; var message = new FormattedMessage(); message.AddText(Loc.GetString("hand-labeler-has-label", ("label", ent.Comp.CurrentLabel))); args.PushMessage(message); } private void OnRefreshNameModifiers(Entity entity, ref RefreshNameModifiersEvent args) { // We need to check lifestage so labels queued for deferred removal don't get applied. if (!string.IsNullOrEmpty(entity.Comp.CurrentLabel) && entity.Comp.LifeStage < ComponentLifeStage.Stopping) args.AddModifier("comp-label-format", extraArgs: ("label", entity.Comp.CurrentLabel)); } private void OnComponentInit(Entity ent, ref ComponentInit args) { _itemSlots.AddItemSlot(ent, ContainerName, ent.Comp.LabelSlot); UpdateAppearance(ent); } private void OnComponentRemove(Entity ent, ref ComponentRemove args) { _itemSlots.RemoveItemSlot(ent, ent.Comp.LabelSlot); } private void OnExamined(Entity ent, ref ExaminedEvent args) { if (ent.Comp.LabelSlot.Item is not {Valid: true} item) return; using (args.PushGroup(nameof(PaperLabelComponent))) { if (!args.IsInDetailsRange) { args.PushMarkup(Loc.GetString("comp-paper-label-has-label-cant-read")); return; } // Assuming yaml has the correct entity whitelist, this should not happen. if (!TryComp(item, out var paper)) return; if (string.IsNullOrWhiteSpace(paper.Content)) { args.PushMarkup(Loc.GetString("comp-paper-label-has-label-blank")); return; } args.PushMarkup(Loc.GetString("comp-paper-label-has-label")); var text = paper.Content; args.PushMarkup(text.TrimEnd()); } } // Not ref-sub due to being used for multiple subscriptions. private void OnContainerModified(EntityUid uid, PaperLabelComponent label, ContainerModifiedMessage args) { if (!label.Initialized) return; if (args.Container.ID != label.LabelSlot.ID) return; UpdateAppearance((uid, label)); } private void UpdateAppearance(Entity ent) { if (!Resolve(ent, ref ent.Comp2, false)) return; var slot = ent.Comp1.LabelSlot; _appearance.SetData(ent, PaperLabelVisuals.HasLabel, slot.HasItem, ent.Comp2); if (TryComp(slot.Item, out var type)) _appearance.SetData(ent, PaperLabelVisuals.LabelType, type.PaperType, ent.Comp2); } /// /// Retrieves a label with the specified component from the default label slot. /// public bool TryGetLabel(Entity ent, [NotNullWhen(true)] out Entity? label) where T : Component { label = null; if (!Resolve(ent, ref ent.Comp, false)) return false; if (ent.Comp.LabelSlot.Item is not { } labelEnt) return false; if (!TryComp(labelEnt, out var labelComp)) return false; label = (labelEnt, labelComp); return true; } }