11using LabApi . Events . Arguments . PlayerEvents ;
2- using LabApi . Events . CustomHandlers ;
2+ using LabApi . Events . Handlers ;
33using LabApi . Features . Wrappers ;
44using MEC ;
55using PlayerRoles ;
6+ using SER . Code . Extensions ;
7+ using SER . Code . Helpers ;
68using Random = System . Random ;
79
810namespace SER . Code . MethodSystem . Methods . CustomRoleMethods . Structures ;
911
10- public class CRole : CustomEventsHandler
12+ public class CRole
1113{
1214 public CRole ( )
1315 {
14- CustomHandlersManager . RegisterEventsHandler ( this ) ;
16+ Log . Signal ( $ "role { GetType ( ) . AccurateName } init") ;
17+ PlayerEvents . Death += OnDeath ;
18+ PlayerEvents . ChangingRole += OnPlayerChangingRole ;
19+ ServerEvents . RoundStarted += OnServerRoundStarted ;
20+ }
21+
22+ public void Unload ( )
23+ {
24+ Log . Signal ( $ "role { GetType ( ) . AccurateName } deconstr") ;
25+ PlayerEvents . Death -= OnDeath ;
26+ PlayerEvents . ChangingRole -= OnPlayerChangingRole ;
27+ ServerEvents . RoundStarted -= OnServerRoundStarted ;
1528 }
1629
1730 public enum CustomRoleEvent
@@ -63,9 +76,10 @@ public static void ResetAll()
6376 {
6477 LifeIdAssignedRoles . Clear ( ) ;
6578 LastRoles . Clear ( ) ;
66- RegisteredRoles . Clear ( ) ;
6779 PlayersInProcessOfReceivingCRole . Clear ( ) ;
6880 EventHandlers . Clear ( ) ;
81+ RegisteredRoles . Values . ForEachItem ( role => role . Unload ( ) ) ;
82+ RegisteredRoles . Clear ( ) ;
6983 }
7084
7185 public void AssignPlayer ( Player plr )
@@ -124,7 +138,7 @@ private void RunHandlers(CustomRoleEvent @event, Player plr)
124138 }
125139 }
126140
127- public override void OnServerRoundStarted ( )
141+ public void OnServerRoundStarted ( )
128142 {
129143 Timing . CallDelayed ( Timing . WaitForOneFrame , delegate
130144 {
@@ -202,7 +216,7 @@ private static void HandleRoleRoundStart(CRole role)
202216 }
203217 }
204218
205- public static void OnDeath ( PlayerDeathEventArgs ev )
219+ public void OnDeath ( PlayerDeathEventArgs ev )
206220 {
207221 if ( ev . Player is not { } plr ) return ;
208222 if ( ! LifeIdAssignedRoles . TryGetValue ( plr . LifeId , out var role ) ) return ;
@@ -211,21 +225,41 @@ public static void OnDeath(PlayerDeathEventArgs ev)
211225 role . RemovePlayer ( plr ) ;
212226 }
213227
214- public override void OnPlayerChangingRole ( PlayerChangingRoleEventArgs ev )
228+ public void OnPlayerChangingRole ( PlayerChangingRoleEventArgs ev )
215229 {
216230 if ( ev . Player is not { } plr ) return ;
217231
218232 if ( LifeIdAssignedRoles . TryGetValue ( plr . LifeId , out var role ) )
219233 {
220234 role . RemovePlayer ( plr ) ;
235+ Log . Debug ( $ "removing role { Id } from { plr . DisplayName } ") ;
221236 }
222237
223- if ( PlayersInProcessOfReceivingCRole . Contains ( plr ) ) return ;
224- if ( SpawnSystem is not ChanceSpawn chanceSpawn ) return ;
225- if ( ev . NewRole != chanceSpawn . RoleToReplace ) return ;
226- if ( chanceSpawn . SpawnChance <= new Random ( ) . NextDouble ( ) ) return ;
238+ if ( SpawnSystem is not ChanceSpawn chanceSpawn )
239+ {
240+ Log . Debug ( $ "{ Id } is not a chance spawn system") ;
241+ return ;
242+ }
243+
244+ if ( PlayersInProcessOfReceivingCRole . Contains ( plr ) )
245+ {
246+ Log . Debug ( $ "player { plr . DisplayName } is already in process of receiving role (checking role: { Id } )") ;
247+ return ;
248+ }
249+
250+ if ( ev . NewRole != chanceSpawn . RoleToReplace )
251+ {
252+ Log . Debug ( $ "{ Id } role cannot work for { plr . DisplayName } , invalid role") ;
253+ return ;
254+ }
255+ if ( chanceSpawn . SpawnChance <= new Random ( ) . NextDouble ( ) )
256+ {
257+ Log . Debug ( $ "player { plr . DisplayName } failed chance spawn") ;
258+ return ;
259+ }
227260
228261 ev . IsAllowed = false ;
229262 AssignPlayer ( plr ) ;
263+ Log . Debug ( $ "player { plr . DisplayName } successfully received role { Id } ") ;
230264 }
231265}
0 commit comments