Chem Dispenser Auto-Labeling (Frontier) (#5261)

* Auto label port

* Make it so it works/comments

* Post review changes.

* Post review change part two.

* review pt 3

* Fixed client UI from not getting updated.

---------

Co-authored-by: Vanessa <vanessalouwagie@gmail.com>
This commit is contained in:
MD360 2026-01-25 18:46:12 +02:00 committed by GitHub
parent 6bb4151430
commit c8b5038ccb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 130 additions and 3 deletions

View File

@ -24,7 +24,7 @@ namespace Content.Server.Chemistry.EntitySystems
/// <seealso cref="ReagentDispenserComponent"/>
/// </summary>
[UsedImplicitly]
public sealed class ReagentDispenserSystem : EntitySystem
public sealed partial class ReagentDispenserSystem : EntitySystem // Frontier - Made Partial, Auto-Label
{
[Dependency] private readonly AudioSystem _audioSystem = default!;
[Dependency] private readonly SharedSolutionContainerSystem _solutionContainerSystem = default!;
@ -41,8 +41,8 @@ namespace Content.Server.Chemistry.EntitySystems
SubscribeLocalEvent<ReagentDispenserComponent, ComponentStartup>(SubscribeUpdateUiState);
SubscribeLocalEvent<ReagentDispenserComponent, SolutionContainerChangedEvent>(SubscribeUpdateUiState);
SubscribeLocalEvent<ReagentDispenserComponent, EntInsertedIntoContainerMessage>(SubscribeUpdateUiState, after: [typeof(SharedStorageSystem)]);
SubscribeLocalEvent<ReagentDispenserComponent, EntRemovedFromContainerMessage>(SubscribeUpdateUiState, after: [typeof(SharedStorageSystem)]);
//SubscribeLocalEvent<ReagentDispenserComponent, EntInsertedIntoContainerMessage>(SubscribeUpdateUiState, after: [typeof(SharedStorageSystem)]); // Frontier - Auto-Label
SubscribeLocalEvent<ReagentDispenserComponent, BoundUIOpenedEvent>(SubscribeUpdateUiState);
SubscribeLocalEvent<ReagentDispenserComponent, ReagentDispenserSetDispenseAmountMessage>(OnSetDispenseAmountMessage);
@ -51,8 +51,8 @@ namespace Content.Server.Chemistry.EntitySystems
SubscribeLocalEvent<ReagentDispenserComponent, ReagentDispenserClearContainerSolutionMessage>(OnClearContainerSolutionMessage);
SubscribeLocalEvent<ReagentDispenserComponent, MapInitEvent>(OnMapInit, before: new[] { typeof(ItemSlotsSystem) });
InitializeAutoLabeling(); // Frontier - Auto-Label
}
private void SubscribeUpdateUiState<T>(Entity<ReagentDispenserComponent> ent, ref T ev)
{
UpdateUiState(ent);

View File

@ -0,0 +1,22 @@
namespace Content.Server.Chemistry.Components;
/// <summary>
/// Frontier - Extends ReagentDispenserComponent.
///
/// Used primarily for the auto-labeling functionality.
/// </summary>
public sealed partial class ReagentDispenserComponent : Component
{
/// <summary>
/// Returns if the component's entity has the ability to auto-label.
/// </summary>
[DataField]
public bool CanAutoLabel = true;
/// <summary>
/// Returns if the entity has auto-labeling toggled on.
/// Will have no effect if <see cref="CanAutoLabel"/> is false.
/// </summary>
[ViewVariables]
public bool AutoLabelToggle = true;
}

View File

@ -0,0 +1,97 @@
using Content.Server.Chemistry.Components;
using Robust.Shared.Containers;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Verbs;
using Content.Shared.Examine;
using Content.Server.Popups;
using Content.Shared.Labels.EntitySystems;
using Content.Shared.Storage.EntitySystems;
namespace Content.Server.Chemistry.EntitySystems;
public sealed partial class ReagentDispenserSystem : EntitySystem
{
[Dependency] private readonly LabelSystem _label = default!;
[Dependency] private readonly PopupSystem _popup = default!;
private void InitializeAutoLabeling()
{
SubscribeLocalEvent<ReagentDispenserComponent, EntInsertedIntoContainerMessage>(OnEntInserted, after: [typeof(SharedStorageSystem)]);
SubscribeLocalEvent<ReagentDispenserComponent, GetVerbsEvent<ExamineVerb>>(OnExamineVerb);
SubscribeLocalEvent<ReagentDispenserComponent, ExaminedEvent>(OnExamined);
}
private void OnEntInserted(Entity<ReagentDispenserComponent> ent, ref EntInsertedIntoContainerMessage ev)
{
TryApplyAutoLabel(ent, ev.Entity);
SubscribeUpdateUiState(ent, ref ev);
}
private void OnExamineVerb(Entity<ReagentDispenserComponent> ent, ref GetVerbsEvent<ExamineVerb> args)
{
if (!ent.Comp.CanAutoLabel)
return;
args.Verbs.Add(new ExamineVerb()
{
Act = () =>
{
SetAutoLabel(ent, !ent.Comp.AutoLabelToggle);
},
Text = ent.Comp.AutoLabelToggle ?
Loc.GetString("reagent-dispenser-component-set-auto-label-off-verb")
: Loc.GetString("reagent-dispenser-component-set-auto-label-on-verb"),
Priority = -1, // Not important, low priority.
CloseMenu = true
});
}
private void OnExamined(Entity<ReagentDispenserComponent> ent, ref ExaminedEvent args)
{
if (!args.IsInDetailsRange || !ent.Comp.CanAutoLabel)
return;
if (ent.Comp.AutoLabelToggle)
args.PushMarkup(Loc.GetString("reagent-dispenser-component-examine-auto-label-on"));
else
args.PushMarkup(Loc.GetString("reagent-dispenser-component-examine-auto-label-off"));
}
private void SetAutoLabel(Entity<ReagentDispenserComponent> ent, bool autoLabel)
{
if (!ent.Comp.CanAutoLabel)
return;
ent.Comp.AutoLabelToggle = autoLabel;
var popupMessage = autoLabel ? Loc.GetString("reagent-dispenser-component-verb-auto-label-turn-on")
: Loc.GetString("reagent-dispenser-component-verb-auto-label-turn-off");
_popup.PopupEntity(popupMessage, ent.Owner);
}
private void TryApplyAutoLabel(Entity<ReagentDispenserComponent> dispenser, EntityUid insertedEntity)
{
if (!dispenser.Comp.CanAutoLabel)
return;
if (!dispenser.Comp.AutoLabelToggle)
return;
if (!_solutionContainerSystem.TryGetDrainableSolution(insertedEntity, out _, out var sol))
return;
if (sol.GetPrimaryReagentId() is not { } reagentProtoId)
return;
if (!_prototypeManager.TryIndex<ReagentPrototype>(reagentProtoId.Prototype, out var reagent))
return;
var reagentQuantity = sol.GetReagentQuantity(reagentProtoId);
var totalQuantity = sol.Volume;
if (reagentQuantity == totalQuantity)
_label.Label(insertedEntity, reagent.LocalizedName);
else
_label.Label(insertedEntity, Loc.GetString("reagent-dispenser-component-impure-auto-label", ("reagent", reagent.LocalizedName), ("purity", 100.0f * reagentQuantity / totalQuantity)));
}
}

View File

@ -0,0 +1,8 @@
# Frontier
reagent-dispenser-component-impure-auto-label = {$reagent} ({$purity}%)
reagent-dispenser-component-set-auto-label-on-verb = Turn on auto-labeler
reagent-dispenser-component-set-auto-label-off-verb = Turn off auto-labeler
reagent-dispenser-component-examine-auto-label-on = The auto-labeler is turned [color=darkgreen]on[/color].
reagent-dispenser-component-examine-auto-label-off = The auto-labeler is turned [color=red]off[/color].
reagent-dispenser-component-verb-auto-label-turn-on = The auto-labeler has been turned on.
reagent-dispenser-component-verb-auto-label-turn-off = The auto-labeler has been turned off.