Skip to content

Commit ca4456b

Browse files
authored
Merge pull request #88 from UncomplicatedCustomServer/dev-labapi
v9.5.1 - LabApi 1.1.7
2 parents 8eca59a + 844c25d commit ca4456b

23 files changed

Lines changed: 550 additions & 255 deletions

UncomplicatedCustomRoles/API/Features/Behaviour/StaminaBehaviour.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
using LabApi.Features.Wrappers;
12-
using PlayerRoles.FirstPersonControl;
1312

1413
namespace UncomplicatedCustomRoles.API.Features.Behaviour
1514
{

UncomplicatedCustomRoles/API/Features/CustomInfo.cs

Lines changed: 102 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010

1111
using LabApi.Features.Wrappers;
12+
using PlayerRoles;
13+
using Respawning.NamingRules;
1214
using UncomplicatedCustomRoles.API.Features.CustomModules;
1315
using UncomplicatedCustomRoles.API.Interfaces;
1416
using 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.\nCustomInfo: {Info}\nError: {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\nRoleName: {Role}\nError: {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.\nCustomInfo: {Info}\nError: {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.\nRoleName: {Role}\nError: {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
}

UncomplicatedCustomRoles/API/Features/CustomModules/CustomPermissions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
using System.Collections.Generic;
1212
using LabApi.Features.Permissions;
13-
using UncomplicatedCustomRoles.Manager;
1413

1514
namespace UncomplicatedCustomRoles.API.Features.CustomModules
1615
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This file is a part of the UncomplicatedCustomRoles project.
3+
*
4+
* Copyright (c) 2023-present FoxWorn3365 (Federico Cosma) <me@fcosma.it>
5+
*
6+
* This file is licensed under the GNU Affero General Public License v3.0.
7+
* You should have received a copy of the AGPL license along with this file.
8+
* If not, see <https://www.gnu.org/licenses/>.
9+
*/
10+
11+
namespace UncomplicatedCustomRoles.API.Features.CustomModules
12+
{
13+
public class NoUnitName : CustomModule
14+
{ }
15+
}

UncomplicatedCustomRoles/API/Features/CustomModules/SilentWalker.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
* If not, see <https://www.gnu.org/licenses/>.
99
*/
1010

11-
using LabApi.Events.Arguments.Interfaces;
12-
using System.Collections.Generic;
13-
1411
namespace UncomplicatedCustomRoles.API.Features.CustomModules
1512
{
1613
public class SilentWalker : CustomModule

UncomplicatedCustomRoles/API/Features/DisguiseTeam.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* This file is a part of the UncomplicatedCustomRoles project.
33
*
44
* Copyright (c) 2023-present FoxWorn3365 (Federico Cosma) <me@fcosma.it>
@@ -10,13 +10,59 @@
1010

1111
using PlayerRoles;
1212
using System.Collections.Concurrent;
13-
using System.Collections.Generic;
13+
using UncomplicatedCustomRoles.Patches;
1414

1515
namespace UncomplicatedCustomRoles.API.Features
1616
{
1717
public class DisguiseTeam
1818
{
19+
/// <summary>
20+
/// Maps a player id to the <see cref="Team"/> their real team is being faked as.
21+
/// </summary>
1922
public static readonly ConcurrentDictionary<int, Team> List = new();
20-
public static readonly Dictionary<int, PlayerRoleBase> RoleBaseList = new();
23+
24+
/// <summary>
25+
/// Maps a player id to the overridden <see cref="PlayerRoleBase"/> used to trick the server into
26+
/// treating the player as (not) an human.
27+
/// </summary>
28+
public static readonly ConcurrentDictionary<int, PlayerRoleBase> RoleBaseList = new();
29+
30+
/// <summary>
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.
34+
/// </summary>
35+
/// <param name="playerId">The player id.</param>
36+
/// <param name="team">The team to fake.</param>
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)
39+
{
40+
List[playerId] = team;
41+
RoleBaseList[playerId] = roleBase;
42+
TeamPatchManager.EnsurePatched();
43+
}
44+
45+
/// <summary>
46+
/// Removes every disguise data for the given player and, if no disguise is left, removes the team patches.
47+
/// </summary>
48+
/// <param name="playerId">The player id.</param>
49+
public static void Remove(int playerId)
50+
{
51+
List.TryRemove(playerId, out _);
52+
RoleBaseList.TryRemove(playerId, out _);
53+
54+
if (List.IsEmpty)
55+
TeamPatchManager.EnsureUnpatched();
56+
}
57+
58+
/// <summary>
59+
/// Clears every disguise data and removes the team patches. Used during plugin (re)load.
60+
/// </summary>
61+
public static void Clear()
62+
{
63+
List.Clear();
64+
RoleBaseList.Clear();
65+
TeamPatchManager.EnsureUnpatched();
66+
}
2167
}
2268
}

UncomplicatedCustomRoles/API/Features/Escape.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using LabApi.Features.Wrappers;
1212
using MEC;
1313
using System.Collections.Generic;
14-
using UncomplicatedCustomRoles.Extensions;
1514

1615
namespace UncomplicatedCustomRoles.API.Features
1716
{

0 commit comments

Comments
 (0)