99 */
1010
1111using LabApi . Features . Wrappers ;
12+ using PlayerRoles ;
13+ using Respawning . NamingRules ;
1214using UncomplicatedCustomRoles . API . Features . CustomModules ;
1315using UncomplicatedCustomRoles . API . Interfaces ;
1416using UncomplicatedCustomRoles . Extensions ;
@@ -18,44 +20,42 @@ namespace UncomplicatedCustomRoles.API.Features
1820{
1921 public class CustomInfo
2022 {
21- private string _nickname ;
22- private string _role ;
23- private string _info ;
24-
2523 public string Nickname
2624 {
27- get => _nickname ;
25+ get ;
2826 set
2927 {
30- _nickname = value ;
28+ field = value ;
3129 if ( _lastOwner is not null )
3230 UpdateInfo ( _lastOwner ) ;
3331 }
3432 }
3533
3634 public string Role
3735 {
38- get => _role ;
36+ get ;
3937 set
4038 {
41- _role = value ;
39+ field = value ;
4240 if ( _lastOwner is not null )
4341 UpdateInfo ( _lastOwner ) ;
4442 }
4543 }
4644
4745 public string Info
4846 {
49- get => _info ;
47+ get ;
5048 set
5149 {
52- _info = value ;
50+ field = value ;
5351 if ( _lastOwner is not null )
5452 UpdateInfo ( _lastOwner ) ;
5553 }
5654 }
5755
5856 private Player _lastOwner ;
57+
58+ internal static bool SuppressExternalSync { get ; set ; }
5959
6060 public CustomInfo ( string nickname , string role , string info )
6161 {
@@ -86,75 +86,110 @@ public void UpdateInfo(Player player)
8686 {
8787 _lastOwner = player ;
8888
89- player . InfoArea |= PlayerInfoArea . CustomInfo ;
90- player . InfoArea &= ~ PlayerInfoArea . Role ;
91- player . InfoArea &= ~ PlayerInfoArea . Nickname ;
92- player . InfoArea &= ~ PlayerInfoArea . UnitName ;
93-
94- string rawCustomInfo = "<color=#FFFFFF></color>%custominfo%%nickname%%rolename%" ;
95- string rawNickname = Nickname ;
96- string rawInfo = Info ;
97-
98- if ( ! NicknameSync . ValidateCustomInfo ( Info , out string customInfoError ) && ! string . IsNullOrEmpty ( Info ) )
89+ bool previousSuppress = SuppressExternalSync ;
90+ SuppressExternalSync = true ;
91+ try
9992 {
100- LogManager . Error ( $ "CustomInfo is not correct, therefore the CustomInfo of player { player . PlayerId } won't be changed.\n CustomInfo: { Info } \n Error: { customInfoError } ") ;
101- rawCustomInfo = rawCustomInfo . Replace ( "%custominfo%" , "" ) ;
102- }
93+ player . InfoArea |= PlayerInfoArea . CustomInfo ;
94+ player . InfoArea &= ~ PlayerInfoArea . Role ;
95+ player . InfoArea &= ~ PlayerInfoArea . Nickname ;
96+ player . InfoArea &= ~ PlayerInfoArea . UnitName ;
10397
104- if ( ! NicknameSync . ValidateCustomInfo ( Role , out string roleNameError ) && ! string . IsNullOrEmpty ( Role ) )
105- {
106- LogManager . Error ( $ "RoleName is not correct, therefore the CustomInfo of player { player . PlayerId } won't be changed\n RoleName: { Role } \n Error: { roleNameError } ") ;
107- return ;
108- }
98+ string rawCustomInfo = "<color=#FFFFFF></color>%custominfo%%nickname%%rolename%" ;
99+ string rawNickname = Nickname ;
100+ string rawInfo = Info ;
101+ string rawRole = Role ;
109102
110- if ( player . TryGetSummonedInstance ( out SummonedCustomRole summonedCustomRole ) )
111- {
112- rawInfo = PlaceholderManager . ApplyPlaceholders ( rawInfo , player , summonedCustomRole . Role ) ;
103+ if ( ! NicknameSync . ValidateCustomInfo ( Info , out string customInfoError ) && ! string . IsNullOrEmpty ( Info ) )
104+ {
105+ LogManager . Error ( $ "CustomInfo is not correct, therefore the custom info part of player { player . PlayerId } won't be shown.\n CustomInfo: { Info } \n Error: { customInfoError } ") ;
106+ rawCustomInfo = rawCustomInfo . Replace ( "%custominfo%" , "" ) ;
107+ rawInfo = string . Empty ;
108+ }
113109
114- if ( summonedCustomRole . TryGetModule ( out CustomInfoOrder customInfoOrderModule ) )
115- rawCustomInfo = $ "<color=#FFFFFF></color>{ customInfoOrderModule . Order } ";
110+ if ( ! NicknameSync . ValidateCustomInfo ( Role , out string roleNameError ) && ! string . IsNullOrEmpty ( Role ) )
111+ {
112+ LogManager . Error ( $ "RoleName is not correct, therefore the role name part of player { player . PlayerId } won't be shown.\n RoleName: { Role } \n Error: { roleNameError } ") ;
113+ rawCustomInfo = rawCustomInfo . Replace ( "%rolename%" , "" ) ;
114+ rawRole = string . Empty ;
115+ }
116116
117- if ( summonedCustomRole . TryGetModule ( out ColorfulNickname colorfulNickname ) )
117+ if ( player . TryGetSummonedInstance ( out SummonedCustomRole summonedCustomRole ) )
118118 {
119- LogManager . Debug ( $ "Applying ColorfulNickname module to player { player . PlayerId } with color { colorfulNickname . Color } and nickname { Nickname } " ) ;
120- if ( string . IsNullOrEmpty ( colorfulNickname . Color ) )
121- return ;
122- string nick = Nickname . Replace ( "<color=#855439>* </color>" , "" ) ;
123- string color = colorfulNickname . Color . StartsWith ( "#" ) ? colorfulNickname . Color : $ "# { colorfulNickname . Color } " ;
124- if ( ! Misc . AcceptedColours . Contains ( color . Replace ( "#" , "" ) ) )
119+ rawInfo = PlaceholderManager . ApplyPlaceholders ( rawInfo , player , summonedCustomRole . Role ) ;
120+
121+ if ( summonedCustomRole . TryGetModule ( out CustomInfoOrder customInfoOrderModule ) )
122+ rawCustomInfo = $ "<color=#FFFFFF> </color>{ customInfoOrderModule . Order } " ;
123+
124+ if ( summonedCustomRole . TryGetModule ( out ColorfulNickname colorfulNickname ) )
125125 {
126- LogManager . Warn ( $ "The color { color } is not acceptable by the game in ColorfulNicknames! Please use a valid hex color code.") ;
127- return ;
126+ LogManager . Debug ( $ "Applying ColorfulNickname module to player { player . PlayerId } with color { colorfulNickname . Color } and nickname { Nickname } ") ;
127+
128+ if ( string . IsNullOrEmpty ( colorfulNickname . Color ) )
129+ {
130+ LogManager . Warn ( $ "The ColorfulNickname module of player { player . PlayerId } has no color set, skipping the colouring.") ;
131+ }
132+ else
133+ {
134+ string nick = Nickname ? . Replace ( "<color=#855439>*</color>" , "" ) ?? string . Empty ;
135+ if ( string . IsNullOrEmpty ( nick ) )
136+ nick = player . Nickname ;
137+ string color = colorfulNickname . Color . StartsWith ( "#" ) ? colorfulNickname . Color : $ "#{ colorfulNickname . Color } ";
138+ if ( ! Misc . AcceptedColours . Contains ( color . Replace ( "#" , "" ) ) )
139+ LogManager . Warn ( $ "The color { color } is not acceptable by the game in ColorfulNicknames! Please use a valid hex color code.") ;
140+ else
141+ rawNickname = $ "<color={ color } >{ nick } </color>";
142+ }
128143 }
129- rawNickname = $ "<color={ color } >{ nick } </color>";
144+
145+ Team roleTeam = summonedCustomRole . Role . Role . GetTeam ( ) ;
146+ if ( DisguiseTeam . List . TryGetValue ( player . PlayerId , out Team fakeTeam ) )
147+ roleTeam = fakeTeam ;
148+
149+ if ( ! string . IsNullOrEmpty ( rawRole ) && ! summonedCustomRole . HasModule < NoUnitName > ( )
150+ && roleTeam is Team . FoundationForces
151+ && NamingRulesManager . TryGetNamingRule ( roleTeam , out UnitNamingRule unitNamingRule )
152+ && ! string . IsNullOrEmpty ( unitNamingRule . LastGeneratedName ) )
153+ rawRole = $ "{ rawRole } ({ unitNamingRule . LastGeneratedName } )";
130154 }
131- }
132- else
133- {
134- rawInfo = PlaceholderManager . ApplyPlaceholders ( rawInfo , player , null ) ;
135- }
136-
137- if ( string . IsNullOrEmpty ( rawInfo ) )
138- rawCustomInfo = rawCustomInfo . Replace ( "%custominfo%" , "" ) ;
139-
140- if ( string . IsNullOrEmpty ( rawNickname ) )
141- rawNickname = player . Nickname ;
142-
143- player . CustomInfo = rawCustomInfo . Replace ( "%%" , "%\n %" ) . BulkReplace ( new ( )
144- {
155+ else
145156 {
146- "custominfo" ,
147- rawInfo
148- } ,
157+ rawInfo = PlaceholderManager . ApplyPlaceholders ( rawInfo , player , null ) ;
158+ }
159+
160+ if ( string . IsNullOrEmpty ( rawInfo ) )
161+ rawCustomInfo = rawCustomInfo . Replace ( "%custominfo%" , "" ) ;
162+
163+ if ( string . IsNullOrEmpty ( rawNickname ) )
164+ rawNickname = player . Nickname ;
165+
166+ if ( string . IsNullOrEmpty ( rawInfo ) && string . IsNullOrEmpty ( rawRole ) && string . IsNullOrEmpty ( player . Nickname ) )
149167 {
150- "nickname" ,
151- rawNickname
152- } ,
168+ player . InfoArea |= PlayerInfoArea . Nickname | PlayerInfoArea . Role | PlayerInfoArea . UnitName ;
169+ player . CustomInfo = string . Empty ;
170+ return ;
171+ }
172+
173+ player . CustomInfo = rawCustomInfo . Replace ( "%%" , "%\n %" ) . BulkReplace ( new ( )
153174 {
154- "rolename" ,
155- Role
156- } ,
157- } , "%<val>%" ) ;
175+ {
176+ "custominfo" ,
177+ rawInfo
178+ } ,
179+ {
180+ "nickname" ,
181+ rawNickname
182+ } ,
183+ {
184+ "rolename" ,
185+ rawRole
186+ } ,
187+ } , "%<val>%" ) ;
188+ }
189+ finally
190+ {
191+ SuppressExternalSync = previousSuppress ;
192+ }
158193 }
159194 }
160195}
0 commit comments