Cargo: pizza & bureaucracy (#5123)
* add paper label component * git mv * rename namespace * add cargo printouts * more crates * directly attach paper * comment typo
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Content.Server.Items;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Labels.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class HandLabelerComponent : Component
|
||||
{
|
||||
public override string Name => "HandLabeler";
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("assignedLabel")]
|
||||
public string AssignedLabel { get; set; } = string.Empty;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("maxLabelChars")]
|
||||
public int MaxLabelChars { get; set; } = 50;
|
||||
|
||||
[DataField("whitelist")]
|
||||
public EntityWhitelist Whitelist = new();
|
||||
}
|
||||
}
|
||||
18
Content.Server/Labels/Label/Components/LabelComponent.cs
Normal file
18
Content.Server/Labels/Label/Components/LabelComponent.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Labels.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class LabelComponent : Component
|
||||
{
|
||||
public override string Name => "Label";
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("currentLabel")]
|
||||
public string? CurrentLabel { get; set; }
|
||||
|
||||
public string? OriginalName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Labels.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// This component allows you to attach and remove a piece of paper to an entity.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class PaperLabelComponent : Component
|
||||
{
|
||||
public override string Name => "PaperLabel";
|
||||
|
||||
[DataField("labelSlot")]
|
||||
public string LabelSlot = "labelSlot";
|
||||
}
|
||||
}
|
||||
110
Content.Server/Labels/Label/HandLabelerSystem.cs
Normal file
110
Content.Server/Labels/Label/HandLabelerSystem.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Content.Server.Labels.Components;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Labels;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.IoC;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Localization;
|
||||
using Content.Shared.Popups;
|
||||
using System;
|
||||
|
||||
namespace Content.Server.Labels
|
||||
{
|
||||
/// <summary>
|
||||
/// A hand labeler system that lets an object apply labels to objects with the <see cref="LabelComponent"/> .
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class HandLabelerSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<HandLabelerComponent, AfterInteractEvent>(AfterInteractOn);
|
||||
SubscribeLocalEvent<HandLabelerComponent, UseInHandEvent>(OnUseInHand);
|
||||
// Bound UI subscriptions
|
||||
SubscribeLocalEvent<HandLabelerComponent, HandLabelerLabelChangedMessage>(OnHandLabelerLabelChanged);
|
||||
}
|
||||
|
||||
private void AfterInteractOn(EntityUid uid, HandLabelerComponent handLabeler, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Target == null || !handLabeler.Whitelist.IsValid(args.Target.Uid))
|
||||
return;
|
||||
|
||||
AddLabelTo(uid, handLabeler, args.Target, out string? result);
|
||||
if (result != null)
|
||||
handLabeler.Owner.PopupMessage(args.User, result);
|
||||
}
|
||||
|
||||
private void AddLabelTo(EntityUid uid, HandLabelerComponent? handLabeler, IEntity target, out string? result)
|
||||
{
|
||||
if (!Resolve(uid, ref handLabeler))
|
||||
{
|
||||
result = null;
|
||||
return;
|
||||
}
|
||||
|
||||
LabelComponent label = target.EnsureComponent<LabelComponent>();
|
||||
|
||||
if (label.OriginalName != null)
|
||||
target.Name = label.OriginalName;
|
||||
label.OriginalName = null;
|
||||
|
||||
if (handLabeler.AssignedLabel == string.Empty)
|
||||
{
|
||||
label.CurrentLabel = null;
|
||||
result = Loc.GetString("hand-labeler-successfully-removed");
|
||||
return;
|
||||
}
|
||||
|
||||
label.OriginalName = target.Name;
|
||||
target.Name += $" ({handLabeler.AssignedLabel})";
|
||||
label.CurrentLabel = handLabeler.AssignedLabel;
|
||||
result = Loc.GetString("hand-labeler-successfully-applied");
|
||||
}
|
||||
|
||||
private void OnUseInHand(EntityUid uid, HandLabelerComponent handLabeler, UseInHandEvent args)
|
||||
{
|
||||
if (!args.User.TryGetComponent(out ActorComponent? actor))
|
||||
return;
|
||||
|
||||
handLabeler.Owner.GetUIOrNull(HandLabelerUiKey.Key)?.Open(actor.PlayerSession);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private bool CheckInteract(ICommonSession session)
|
||||
{
|
||||
if (session.AttachedEntityUid is not { } uid
|
||||
|| !Get<ActionBlockerSystem>().CanInteract(uid)
|
||||
|| !Get<ActionBlockerSystem>().CanUse(uid))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnHandLabelerLabelChanged(EntityUid uid, HandLabelerComponent handLabeler, HandLabelerLabelChangedMessage args)
|
||||
{
|
||||
if (!CheckInteract(args.Session))
|
||||
return;
|
||||
|
||||
handLabeler.AssignedLabel = args.Label.Trim().Substring(0, Math.Min(handLabeler.MaxLabelChars, args.Label.Length));
|
||||
DirtyUI(uid, handLabeler);
|
||||
}
|
||||
|
||||
private void DirtyUI(EntityUid uid,
|
||||
HandLabelerComponent? handLabeler = null)
|
||||
{
|
||||
if (!Resolve(uid, ref handLabeler))
|
||||
return;
|
||||
|
||||
_userInterfaceSystem.TrySetUiState(uid, HandLabelerUiKey.Key,
|
||||
new HandLabelerBoundUserInterfaceState(handLabeler.AssignedLabel));
|
||||
}
|
||||
}
|
||||
}
|
||||
97
Content.Server/Labels/Label/LabelSystem.cs
Normal file
97
Content.Server/Labels/Label/LabelSystem.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
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.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Utility;
|
||||
using System;
|
||||
|
||||
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 SharedItemSlotsSystem _itemSlotsSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<LabelComponent, ExaminedEvent>(OnExamine);
|
||||
SubscribeLocalEvent<PaperLabelComponent, ComponentInit>(InitializePaperLabel);
|
||||
SubscribeLocalEvent<PaperLabelComponent, ItemSlotChangedEvent>(OnItemSlotChanged);
|
||||
SubscribeLocalEvent<PaperLabelComponent, ExaminedEvent>(OnExamined);
|
||||
}
|
||||
|
||||
private void InitializePaperLabel(EntityUid uid, PaperLabelComponent component, ComponentInit args)
|
||||
{
|
||||
if (!EntityManager.TryGetComponent(uid, out SharedAppearanceComponent appearance))
|
||||
return;
|
||||
|
||||
appearance.SetData(PaperLabelVisuals.HasLabel, false);
|
||||
}
|
||||
|
||||
private void OnExamine(EntityUid uid, LabelComponent? label, ExaminedEvent args)
|
||||
{
|
||||
if (!Resolve(uid, ref label))
|
||||
return;
|
||||
|
||||
if (label.CurrentLabel == null)
|
||||
return;
|
||||
|
||||
var message = new FormattedMessage();
|
||||
message.AddText(Loc.GetString("hand-labeler-has-label", ("label", label.CurrentLabel)));
|
||||
args.PushMessage(message);
|
||||
}
|
||||
|
||||
private void OnExamined(EntityUid uid, PaperLabelComponent comp, ExaminedEvent args)
|
||||
{
|
||||
if (!EntityManager.TryGetComponent(uid, out SharedItemSlotsComponent slots))
|
||||
return;
|
||||
|
||||
var label = _itemSlotsSystem.PeekItemInSlot(slots, comp.LabelSlot);
|
||||
|
||||
if (label == null)
|
||||
return;
|
||||
|
||||
if (!args.IsInDetailsRange)
|
||||
{
|
||||
args.PushMarkup(Loc.GetString("comp-paper-label-has-label-cant-read"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(label.Uid, out PaperComponent paper))
|
||||
// should never 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 OnItemSlotChanged(EntityUid uid, PaperLabelComponent component, ItemSlotChangedEvent args)
|
||||
{
|
||||
if (args.SlotName != component.LabelSlot)
|
||||
return;
|
||||
|
||||
if (!EntityManager.TryGetComponent(uid, out SharedAppearanceComponent appearance))
|
||||
return;
|
||||
|
||||
appearance.SetData(PaperLabelVisuals.HasLabel, args.ContainedItem != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user