Files
OldThink/Content.Server/Paper/PaperComponent.cs

112 lines
3.2 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
2021-06-09 22:19:39 +02:00
using Content.Server.UserInterface;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Paper;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.Utility;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Paper
{
[RegisterComponent]
#pragma warning disable 618
[ComponentReference(typeof(SharedPaperComponent))]
public sealed class PaperComponent : SharedPaperComponent, IExamine, IInteractUsing
#pragma warning restore 618
{
2021-12-08 17:32:32 +01:00
[Dependency] private readonly IEntityManager _entMan = default!;
public PaperAction Mode;
[DataField("content")]
public string Content { get; set; } = "";
[DataField("contentSize")]
public int ContentSize { get; set; } = 500;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(PaperUiKey.Key);
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
}
Mode = PaperAction.Read;
UpdateUserInterface();
}
public void SetContent(string content)
{
Content = content + '\n';
UpdateUserInterface();
2021-12-08 17:32:32 +01:00
if (!_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance))
return;
var status = string.IsNullOrWhiteSpace(content)
? PaperStatus.Blank
: PaperStatus.Written;
appearance.SetData(PaperVisuals.Status, status);
}
public void UpdateUserInterface()
{
UserInterface?.SetState(new PaperBoundUserInterfaceState(Content, Mode));
}
2021-12-20 12:42:42 +01:00
public void Examine(FormattedMessage message, bool inDetailsRange)
{
if (!inDetailsRange)
return;
if (Content == "")
return;
message.AddMarkup(
Loc.GetString(
"paper-component-examine-detail-has-words"
)
);
}
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
var msg = (PaperInputText) obj.Message;
if (string.IsNullOrEmpty(msg.Text))
return;
if (msg.Text.Length + Content.Length <= ContentSize)
Content += msg.Text + '\n';
2021-12-08 17:32:32 +01:00
if (_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance))
{
2021-10-09 14:48:53 +01:00
appearance.SetData(PaperVisuals.Status, PaperStatus.Written);
}
2021-12-08 17:32:32 +01:00
_entMan.GetComponent<MetaDataComponent>(Owner).EntityDescription = "";
UpdateUserInterface();
}
2021-02-04 17:44:49 +01:00
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
2022-02-08 14:08:11 +11:00
if (!EntitySystem.Get<TagSystem>().HasTag(eventArgs.Using, "Write"))
return false;
2021-12-08 17:32:32 +01:00
if (!_entMan.TryGetComponent(eventArgs.User, out ActorComponent? actor))
return false;
Mode = PaperAction.Write;
UpdateUserInterface();
UserInterface?.Open(actor.PlayerSession);
return true;
}
}
}