Files
tbd-station-14/Content.Server/Labels/Label/LabelSystem.cs
E F R b2da936848 Everything: Rich text redux (#5625)
* lord save me

* UI/ChatBox: Use the new `defStyle` param for `RenderMarkup`

The previous iteration didn't work because `AddMessage` can't inherit
its color from the PushColor (since we're not doing actual tag stacks
anymore).

* rebase touchup
2021-12-12 18:25:42 -08:00

101 lines
3.5 KiB
C#

using Content.Server.Labels.Components;
using Content.Server.Paper;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Examine;
using Content.Shared.Labels;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Utility;
using Robust.Shared.Utility.Markup;
namespace Content.Server.Labels
{
/// <summary>
/// A system that lets players see the contents of a label on an object.
/// </summary>
[UsedImplicitly]
public class LabelSystem : EntitySystem
{
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LabelComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<PaperLabelComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<PaperLabelComponent, ComponentRemove>(OnComponentRemove);
SubscribeLocalEvent<PaperLabelComponent, EntInsertedIntoContainerMessage>(OnContainerModified);
SubscribeLocalEvent<PaperLabelComponent, EntRemovedFromContainerMessage>(OnContainerModified);
SubscribeLocalEvent<PaperLabelComponent, ExaminedEvent>(OnExamined);
}
private void OnComponentInit(EntityUid uid, PaperLabelComponent component, ComponentInit args)
{
_itemSlotsSystem.AddItemSlot(uid, component.Name, component.LabelSlot);
if (!EntityManager.TryGetComponent(uid, out AppearanceComponent appearance))
return;
appearance.SetData(PaperLabelVisuals.HasLabel, false);
}
private void OnComponentRemove(EntityUid uid, PaperLabelComponent component, ComponentRemove args)
{
_itemSlotsSystem.RemoveItemSlot(uid, component.LabelSlot);
}
private void OnExamine(EntityUid uid, LabelComponent? label, ExaminedEvent args)
{
if (!Resolve(uid, ref label))
return;
if (label.CurrentLabel == null)
return;
args.Message.AddText(Loc.GetString("hand-labeler-has-label", ("label", label.CurrentLabel)));
}
private void OnExamined(EntityUid uid, PaperLabelComponent comp, ExaminedEvent args)
{
if (comp.LabelSlot.Item is not {Valid: true} item)
return;
if (!args.IsInDetailsRange)
{
args.PushMarkup(Loc.GetString("comp-paper-label-has-label-cant-read"));
return;
}
if (!EntityManager.TryGetComponent(item, out PaperComponent paper))
// Assuming yaml has the correct entity whitelist, this should not happen.
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());
}
private void OnContainerModified(EntityUid uid, PaperLabelComponent label, ContainerModifiedMessage args)
{
if (args.Container.ID != label.LabelSlot.ID)
return;
if (!EntityManager.TryGetComponent(uid, out AppearanceComponent appearance))
return;
appearance.SetData(PaperLabelVisuals.HasLabel, label.LabelSlot.HasItem);
}
}
}