Skip to content

Commit 7fce843

Browse files
committed
Refactor Set method to combine team and role base registration
1 parent ad2e909 commit 7fce843

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

UncomplicatedCustomRoles/API/Features/DisguiseTeam.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,20 @@ public class DisguiseTeam
2828
public static readonly ConcurrentDictionary<int, PlayerRoleBase> RoleBaseList = new();
2929

3030
/// <summary>
31-
/// Registers a faked <see cref="Team"/> for the given player and makes sure the team patches are active.
31+
/// Registers a faked <see cref="Team"/> together with the overridden <see cref="PlayerRoleBase"/> that is
32+
/// exposed as the player's current role. The role base is always set alongside its team, so there is no
33+
/// separate "role base only" entry point.
3234
/// </summary>
3335
/// <param name="playerId">The player id.</param>
3436
/// <param name="team">The team to fake.</param>
35-
public static void Set(int playerId, Team team)
37+
/// <param name="roleBase">The role base to expose as the player's current role.</param>
38+
public static void Set(int playerId, Team team, PlayerRoleBase roleBase)
3639
{
3740
List[playerId] = team;
41+
RoleBaseList[playerId] = roleBase;
3842
TeamPatchManager.EnsurePatched();
3943
}
4044

41-
/// <summary>
42-
/// Registers the overridden <see cref="PlayerRoleBase"/> for the given player.
43-
/// </summary>
44-
/// <param name="playerId">The player id.</param>
45-
/// <param name="roleBase">The role base to expose as the player's current role.</param>
46-
public static void SetRoleBase(int playerId, PlayerRoleBase roleBase) => RoleBaseList[playerId] = roleBase;
47-
4845
/// <summary>
4946
/// Removes every disguise data for the given player and, if no disguise is left, removes the team patches.
5047
/// </summary>
@@ -68,4 +65,4 @@ public static void Clear()
6865
TeamPatchManager.EnsureUnpatched();
6966
}
7067
}
71-
}
68+
}

UncomplicatedCustomRoles/API/Features/SummonedCustomRole.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ internal SummonedCustomRole(Player player, ICustomRole role, Triplet<string, str
167167

168168
if (Role.Team is not null && Role.Team != Role.Role.GetTeam())
169169
{
170-
DisguiseTeam.Set(Player.PlayerId, (Team)Role.Team);
171170
EvaluateRoleBase();
172171
LogManager.Debug($"EVALUATED ROLEBASE {_roleBase?.GetType().FullName} with team {_roleBase?.Team}");
173172
}
@@ -190,6 +189,9 @@ internal SummonedCustomRole(Player player, ICustomRole role, Triplet<string, str
190189
{
191190
Timing.CallDelayed(0.75f, () =>
192191
{
192+
if (!_internalValid || Player is null || !Player.IsAlive)
193+
return;
194+
193195
LogManager.Debug($"Changing the appearance of the role {Role.Id} [{Role.Name}] to {Role.RoleAppearance}");
194196

195197
if (LabApiExtensions.IsAvailable)
@@ -203,7 +205,7 @@ internal SummonedCustomRole(Player player, ICustomRole role, Triplet<string, str
203205
}
204206

205207
/// <summary>
206-
/// Try to set <see cref="_roleBase"/> in order to override the current Player.Role.Base to trick the server into thinking that the player is / is not an Human
208+
/// Try to set <see cref="RoleBase"/> in order to override the current Player.Role.Base to trick the server into thinking that the player is / is not an Human
207209
/// </summary>
208210
private void EvaluateRoleBase()
209211
{
@@ -257,15 +259,15 @@ private void EvaluateRoleBase()
257259
SpectatorModule = originalRole.SpectatorModule
258260
};
259261

260-
DisguiseTeam.SetRoleBase(Player.PlayerId, _roleBase);
262+
DisguiseTeam.Set(Player.PlayerId, Role.Team ?? Role.Role.GetTeam(), _roleBase);
261263

262264
Timing.CallDelayed(3.25f, delegate
263265
{
264266
if (!_internalValid || _roleBase is null)
265267
return;
266268

267269
_roleBase.Pooled = false;
268-
DisguiseTeam.SetRoleBase(Player.PlayerId, _roleBase);
270+
DisguiseTeam.Set(Player.PlayerId, Role.Team ?? Role.Role.GetTeam(), _roleBase);
269271
});
270272
}
271273
catch (Exception e)
@@ -648,7 +650,7 @@ public static bool TryCheckForCustomTeam(ReferenceHub player, Team teamCheck, ou
648650
/// <param name="player"></param>
649651
/// <param name="def"></param>
650652
/// <returns></returns>
651-
public static Team TryGetCusomTeam(ReferenceHub player, Team? def = null)
653+
public static Team TryGetCustomTeam(ReferenceHub player, Team? def = null)
652654
{
653655
if (TryGet(player, out SummonedCustomRole customRole) && customRole.Role.Team is not null && customRole.Role.Team != customRole.Role.Role.GetTeam())
654656
return (Team)customRole.Role.Team;

0 commit comments

Comments
 (0)