@@ -20,7 +20,7 @@ public sealed class NameIdentifierSystem : EntitySystem
2020 /// Free IDs available per <see cref="NameIdentifierGroupPrototype"/>.
2121 /// </summary>
2222 [ ViewVariables ]
23- public readonly Dictionary < string , List < int > > CurrentIds = new ( ) ;
23+ public readonly Dictionary < string , List < int > > CurrentIds = [ ] ;
2424
2525 public override void Initialize ( )
2626 {
@@ -35,18 +35,22 @@ public override void Initialize()
3535 InitialSetupPrototypes ( ) ;
3636 }
3737
38- private void OnComponentShutdown ( EntityUid uid , NameIdentifierComponent component , ComponentShutdown args )
38+ private void OnComponentShutdown ( Entity < NameIdentifierComponent > ent , ref ComponentShutdown args )
3939 {
40- if ( CurrentIds . TryGetValue ( component . Group , out var ids ) )
40+ if ( ent . Comp . Group is null )
41+ return ;
42+
43+ if ( CurrentIds . TryGetValue ( ent . Comp . Group , out var ids ) && ids . Count > 0 )
4144 {
4245 // Avoid inserting the value right back at the end or shuffling in place:
4346 // just pick a random spot to put it and then move that one to the end.
4447 var randomIndex = _robustRandom . Next ( ids . Count ) ;
4548 var random = ids [ randomIndex ] ;
46- ids [ randomIndex ] = component . Identifier ;
49+ ids [ randomIndex ] = ent . Comp . Identifier ;
4750 ids . Add ( random ) ;
4851 }
49- _nameModifier . RefreshNameModifiers ( uid ) ;
52+
53+ _nameModifier . RefreshNameModifiers ( ent . Owner ) ;
5054 }
5155
5256 /// <summary>
@@ -83,46 +87,53 @@ public string GenerateUniqueName(EntityUid uid, NameIdentifierGroupPrototype pro
8387 : $ "{ randomVal } ";
8488 }
8589
86- private void OnMapInit ( EntityUid uid , NameIdentifierComponent component , MapInitEvent args )
90+ private void OnMapInit ( Entity < NameIdentifierComponent > ent , ref MapInitEvent args )
8791 {
88- if ( ! _prototypeManager . TryIndex < NameIdentifierGroupPrototype > ( component . Group , out var group ) )
92+ if ( ent . Comp . Group is null )
93+ return ;
94+
95+ if ( ! _prototypeManager . TryIndex ( ent . Comp . Group , out var group ) )
8996 return ;
9097
9198 int id ;
9299 string uniqueName ;
93100
94101 // If it has an existing valid identifier then use that, otherwise generate a new one.
95- if ( component . Identifier != - 1 &&
96- CurrentIds . TryGetValue ( component . Group , out var ids ) &&
97- ids . Remove ( component . Identifier ) )
102+ if ( ent . Comp . Identifier != - 1 &&
103+ CurrentIds . TryGetValue ( ent . Comp . Group , out var ids ) &&
104+ ids . Remove ( ent . Comp . Identifier ) )
98105 {
99- id = component . Identifier ;
106+ id = ent . Comp . Identifier ;
100107 uniqueName = group . Prefix is not null
101108 ? $ "{ group . Prefix } -{ id } "
102109 : $ "{ id } ";
103110 }
104111 else
105112 {
106- uniqueName = GenerateUniqueName ( uid , group , out id ) ;
107- component . Identifier = id ;
113+ uniqueName = GenerateUniqueName ( ent , group , out id ) ;
114+ ent . Comp . Identifier = id ;
108115 }
109116
110- component . FullIdentifier = group . FullName
117+ ent . Comp . FullIdentifier = group . FullName
111118 ? uniqueName
112119 : $ "({ uniqueName } )";
113120
114- Dirty ( uid , component ) ;
115- _nameModifier . RefreshNameModifiers ( uid ) ;
121+ Dirty ( ent ) ;
122+ _nameModifier . RefreshNameModifiers ( ent . Owner ) ;
116123 }
117124
118125 private void OnRefreshNameModifiers ( Entity < NameIdentifierComponent > ent , ref RefreshNameModifiersEvent args )
119126 {
127+ if ( ent . Comp . Group is null )
128+ return ;
129+
120130 // Don't apply the modifier if the component is being removed
121131 if ( ent . Comp . LifeStage > ComponentLifeStage . Running )
122132 return ;
123133
124- if ( ! _prototypeManager . TryIndex < NameIdentifierGroupPrototype > ( ent . Comp . Group , out var group ) )
134+ if ( ! _prototypeManager . TryIndex ( ent . Comp . Group , out var group ) )
125135 return ;
136+
126137 var format = group . FullName ? "name-identifier-format-full" : "name-identifier-format-append" ;
127138 // We apply the modifier with a low priority to keep it near the base name
128139 // "Beep (Si-4562) the zombie" instead of "Beep the zombie (Si-4562)"
@@ -188,13 +199,13 @@ private void OnReloadPrototypes(PrototypesReloadedEventArgs ev)
188199
189200 foreach ( var proto in set . Modified . Values )
190201 {
191- var name_proto = ( NameIdentifierGroupPrototype ) proto ;
202+ var name_proto = ( NameIdentifierGroupPrototype ) proto ;
192203
193204 // Only bother adding new ones.
194205 if ( CurrentIds . ContainsKey ( proto . ID ) )
195206 continue ;
196207
197- var ids = GetOrCreateIdList ( name_proto ) ;
208+ var ids = GetOrCreateIdList ( name_proto ) ;
198209 FillGroup ( name_proto , ids ) ;
199210 }
200211 }
0 commit comments