Minor paper ECS and stamps (#7394)

Co-authored-by: fishfish458 <fishfish458>
This commit is contained in:
Fishfish458
2022-04-08 17:37:22 -06:00
committed by GitHub
parent eac154ca1b
commit ddf2d8815b
23 changed files with 450 additions and 133 deletions

View File

@@ -1,20 +1,10 @@
using System.Threading.Tasks;
using Content.Server.UserInterface;
using Content.Shared.Interaction;
using Content.Shared.Paper;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
namespace Content.Server.Paper
{
[RegisterComponent]
#pragma warning disable 618
[ComponentReference(typeof(SharedPaperComponent))]
public sealed class PaperComponent : SharedPaperComponent, IInteractUsing
#pragma warning restore 618
public sealed class PaperComponent : SharedPaperComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
public PaperAction Mode;
[DataField("content")]
public string Content { get; set; } = "";
@@ -22,72 +12,12 @@ namespace Content.Server.Paper
[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();
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));
}
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';
if (_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance))
{
appearance.SetData(PaperVisuals.Status, PaperStatus.Written);
}
_entMan.GetComponent<MetaDataComponent>(Owner).EntityDescription = "";
UpdateUserInterface();
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (!EntitySystem.Get<TagSystem>().HasTag(eventArgs.Using, "Write"))
return false;
if (!_entMan.TryGetComponent(eventArgs.User, out ActorComponent? actor))
return false;
Mode = PaperAction.Write;
UpdateUserInterface();
UserInterface?.Open(actor.PlayerSession);
return true;
}
[DataField("stampedBy")]
public List<string> StampedBy { get; set; } = new();
/// <summary>
/// Stamp to be displayed on the paper, state from beauracracy.rsi
/// </summary>
[DataField("stampState")]
public string? StampState { get; set; }
}
}

View File

@@ -1,36 +1,162 @@
using Content.Server.UserInterface;
using Content.Shared.Examine;
using Content.Shared.Paper;
using Content.Shared.Interaction;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Content.Server.Popups;
using Robust.Shared.Player;
using static Content.Shared.Paper.SharedPaperComponent;
namespace Content.Server.Paper
{
public sealed class PaperSystem : EntitySystem
{
[Dependency] private readonly TagSystem _tagSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PaperComponent, BeforeActivatableUIOpenEvent>(AfterUIOpen);
SubscribeLocalEvent<PaperComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<PaperComponent, BeforeActivatableUIOpenEvent>(BeforeUIOpen);
SubscribeLocalEvent<PaperComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<PaperComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<PaperComponent, PaperInputTextMessage>(OnInputTextMessage);
}
private void AfterUIOpen(EntityUid uid, PaperComponent component, BeforeActivatableUIOpenEvent args)
private void OnInit(EntityUid uid, PaperComponent paperComp, ComponentInit args)
{
component.Mode = SharedPaperComponent.PaperAction.Read;
component.UpdateUserInterface();
paperComp.Mode = PaperAction.Read;
UpdateUserInterface(uid, paperComp);
if (TryComp<AppearanceComponent>(uid, out var appearance))
{
if (paperComp.Content != "")
appearance.SetData(PaperVisuals.Status, PaperStatus.Written);
if (paperComp.StampState != null)
appearance.SetData(PaperVisuals.Stamp, paperComp.StampState);
}
}
private void OnExamined(EntityUid uid, PaperComponent component, ExaminedEvent args)
private void BeforeUIOpen(EntityUid uid, PaperComponent paperComp, BeforeActivatableUIOpenEvent args)
{
paperComp.Mode = PaperAction.Read;
UpdateUserInterface(uid, paperComp);
}
private void OnExamined(EntityUid uid, PaperComponent paperComp, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
if (component.Content == "")
if (paperComp.Content != "")
args.Message.AddMarkup(
Loc.GetString(
"paper-component-examine-detail-has-words", ("paper", uid)
)
);
if (paperComp.StampedBy.Count > 0)
{
args.Message.PushNewline();
string commaSeparated = string.Join(", ", paperComp.StampedBy);
args.Message.AddMarkup(
Loc.GetString(
"paper-component-examine-detail-stamped-by", ("paper", uid), ("stamps", commaSeparated))
);
}
}
private void OnInteractUsing(EntityUid uid, PaperComponent paperComp, InteractUsingEvent args)
{
if (_tagSystem.HasTag(args.Used, "Write"))
{
if (!TryComp<ActorComponent>(args.User, out var actor))
return;
args.PushMarkup(
Loc.GetString(
"paper-component-examine-detail-has-words"
)
);
paperComp.Mode = PaperAction.Write;
UpdateUserInterface(uid, paperComp);
_uiSystem.GetUiOrNull(uid, PaperUiKey.Key)?.Open(actor.PlayerSession);
return;
}
// If a stamp, attempt to stamp paper
if (TryComp<StampComponent>(args.Used, out var stampComp) && TryStamp(uid, stampComp.StampedName, stampComp.StampState, paperComp))
{
// successfully stamped, play popup
var stampPaperOtherMessage = Loc.GetString("paper-component-action-stamp-paper-other", ("user", args.User),("target", args.Target),("stamp", args.Used));
_popupSystem.PopupEntity(stampPaperOtherMessage, args.User, Filter.Pvs(args.User, entityManager: EntityManager).RemoveWhereAttachedEntity(puid => puid == args.User));
var stampPaperSelfMessage = Loc.GetString("paper-component-action-stamp-paper-self", ("target", args.Target),("stamp", args.Used));
_popupSystem.PopupEntity(stampPaperSelfMessage, args.User, Filter.Entities(args.User));
}
}
private void OnInputTextMessage(EntityUid uid, PaperComponent paperComp, PaperInputTextMessage args)
{
if (string.IsNullOrEmpty(args.Text))
return;
if (args.Text.Length + paperComp.Content.Length <= paperComp.ContentSize)
paperComp.Content += args.Text + '\n';
if (TryComp<AppearanceComponent>(uid, out var appearance))
appearance.SetData(PaperVisuals.Status, PaperStatus.Written);
if (TryComp<MetaDataComponent>(uid, out var meta))
meta.EntityDescription = "";
UpdateUserInterface(uid, paperComp);
}
/// <summary>
/// Accepts the name and state to be stamped onto the paper, returns true if successful.
/// </summary>
public bool TryStamp(EntityUid uid, string stampName, string stampState, PaperComponent? paperComp = null)
{
if (!Resolve(uid, ref paperComp))
return false;
if (!paperComp.StampedBy.Contains(Loc.GetString(stampName)))
{
paperComp.StampedBy.Add(Loc.GetString(stampName));
if (paperComp.StampState == null && TryComp<AppearanceComponent>(uid, out var appearance))
{
paperComp.StampState = stampState;
appearance.SetData(PaperVisuals.Stamp, paperComp.StampState);
}
}
return true;
}
public void SetContent(EntityUid uid, string content, PaperComponent? paperComp = null)
{
if (!Resolve(uid, ref paperComp))
return;
paperComp.Content = content + '\n';
UpdateUserInterface(uid, paperComp);
if (!TryComp<AppearanceComponent>(uid, out var appearance))
return;
var status = string.IsNullOrWhiteSpace(content)
? PaperStatus.Blank
: PaperStatus.Written;
appearance.SetData(PaperVisuals.Status, status);
}
public void UpdateUserInterface(EntityUid uid, PaperComponent? paperComp = null)
{
if (!Resolve(uid, ref paperComp))
return;
_uiSystem.GetUiOrNull(uid, PaperUiKey.Key)?.SetState(new PaperBoundUserInterfaceState(paperComp.Content, paperComp.Mode));
}
}
}