Convert NameIdentifier to use NameModifierSystem (#36736)

Convert NameIdentifier to use NameModifierSystem
This commit is contained in:
Tayrtahn
2025-04-20 00:16:59 -04:00
committed by GitHub
parent 2c54ec10c6
commit 31cf0505c9
4 changed files with 26 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
using Content.Client.Stylesheets;
using Content.Client.UserInterface.Controls;
using Content.Shared.NameIdentifier;
using Content.Shared.NameModifier.EntitySystems;
using Content.Shared.Preferences;
using Content.Shared.Silicons.Borgs;
using Content.Shared.Silicons.Borgs.Components;
@@ -15,6 +16,7 @@ namespace Content.Client.Silicons.Borgs;
public sealed partial class BorgMenu : FancyWindow
{
[Dependency] private readonly IEntityManager _entity = default!;
private readonly NameModifierSystem _nameModifier;
public Action? BrainButtonPressed;
public Action? EjectBatteryButtonPressed;
@@ -32,6 +34,8 @@ public sealed partial class BorgMenu : FancyWindow
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_nameModifier = _entity.System<NameModifierSystem>();
_lastValidName = NameLineEdit.Text;
EjectBatteryButton.OnPressed += _ => EjectBatteryButtonPressed?.Invoke();
@@ -54,9 +58,7 @@ public sealed partial class BorgMenu : FancyWindow
NameIdentifierLabel.Visible = true;
NameIdentifierLabel.Text = nameIdentifierComponent.FullIdentifier;
var fullName = _entity.GetComponent<MetaDataComponent>(Entity).EntityName;
var name = fullName.Substring(0, fullName.Length - nameIdentifierComponent.FullIdentifier.Length - 1);
NameLineEdit.Text = name;
NameLineEdit.Text = _nameModifier.GetBaseName(entity);
}
else
{

View File

@@ -1,5 +1,6 @@
using Content.Shared.GameTicking;
using Content.Shared.NameIdentifier;
using Content.Shared.NameModifier.EntitySystems;
using Robust.Shared.Collections;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
@@ -13,7 +14,7 @@ public sealed class NameIdentifierSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly NameModifierSystem _nameModifier = default!;
/// <summary>
/// Free IDs available per <see cref="NameIdentifierGroupPrototype"/>.
@@ -27,6 +28,7 @@ public sealed class NameIdentifierSystem : EntitySystem
SubscribeLocalEvent<NameIdentifierComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<NameIdentifierComponent, ComponentShutdown>(OnComponentShutdown);
SubscribeLocalEvent<NameIdentifierComponent, RefreshNameModifiersEvent>(OnRefreshNameModifiers);
SubscribeLocalEvent<RoundRestartCleanupEvent>(CleanupIds);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnReloadPrototypes);
@@ -44,6 +46,7 @@ public sealed class NameIdentifierSystem : EntitySystem
ids[randomIndex] = component.Identifier;
ids.Add(random);
}
_nameModifier.RefreshNameModifiers(uid);
}
/// <summary>
@@ -108,12 +111,22 @@ public sealed class NameIdentifierSystem : EntitySystem
? uniqueName
: $"({uniqueName})";
var meta = MetaData(uid);
// "DR-1234" as opposed to "drone (DR-1234)"
_metaData.SetEntityName(uid, group.FullName
? uniqueName
: $"{meta.EntityName} ({uniqueName})", meta);
Dirty(uid, component);
_nameModifier.RefreshNameModifiers(uid);
}
private void OnRefreshNameModifiers(Entity<NameIdentifierComponent> ent, ref RefreshNameModifiersEvent args)
{
// Don't apply the modifier if the component is being removed
if (ent.Comp.LifeStage > ComponentLifeStage.Running)
return;
if (!_prototypeManager.TryIndex<NameIdentifierGroupPrototype>(ent.Comp.Group, out var group))
return;
var format = group.FullName ? "name-identifier-format-full" : "name-identifier-format-append";
// We apply the modifier with a low priority to keep it near the base name
// "Beep (Si-4562) the zombie" instead of "Beep the zombie (Si-4562)"
args.AddModifier(format, -10, ("identifier", ent.Comp.FullIdentifier));
}
private void InitialSetupPrototypes()

View File

@@ -62,8 +62,6 @@ public sealed partial class BorgSystem
}
var name = args.Name.Trim();
if (TryComp<NameIdentifierComponent>(uid, out var identifier))
name = $"{name} {identifier.FullIdentifier}";
var metaData = MetaData(uid);

View File

@@ -0,0 +1,2 @@
name-identifier-format-append = {$baseName} {$identifier}
name-identifier-format-full = {$identifier}