Skip to content

Commit e955653

Browse files
Fix null reference error when building barricade or structures and add rich text and MessageIconUrl option to configuration
1 parent a556bce commit e955653

File tree

5 files changed

+109
-81
lines changed

5 files changed

+109
-81
lines changed

BuildingRestrictions/BuildingRestrictions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net48</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
<RootNamespace>RestoreMonarchy.BuildingRestrictions</RootNamespace>
7-
<Version>1.1.0</Version>
7+
<Version>1.1.1</Version>
88
</PropertyGroup>
99

1010
<ItemGroup>

BuildingRestrictions/BuildingRestrictionsConfiguration.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class BuildingRestrictionsConfiguration : IRocketPluginConfiguration
1010
public bool Debug { get; set; }
1111
public bool ShouldSerializeDebug() => Debug;
1212
public string MessageColor { get; set; }
13+
public string MessageIconUrl { get; set; }
1314
public bool EnableMaxBuildings { get; set; }
1415
public int MaxBuildings { get; set; }
1516
public bool EnableMaxBarricades { get; set; }
@@ -24,16 +25,17 @@ public class BuildingRestrictionsConfiguration : IRocketPluginConfiguration
2425
public float MaxStructureHeight { get; set; } = 100;
2526

2627
[XmlArrayItem("Barricade")]
27-
public BuildingRestriction[] Barricades { get; set; }
28+
public BuildingRestriction[] Barricades { get; set; } = [];
2829
[XmlArrayItem("Structure")]
29-
public BuildingRestriction[] Structures { get; set; }
30+
public BuildingRestriction[] Structures { get; set; } = [];
3031
[XmlArrayItem("Multiplier")]
31-
public PermissionMultiplier[] Multipliers { get; set; }
32+
public PermissionMultiplier[] Multipliers { get; set; } = [];
3233

3334
public void LoadDefaults()
3435
{
3536
Debug = false;
3637
MessageColor = "yellow";
38+
MessageIconUrl = "https://i.imgur.com/LlEcfBg.png";
3739
EnableMaxBuildings = false;
3840
MaxBuildings = 200;
3941
EnableMaxBarricades = false;

BuildingRestrictions/BuildingRestrictionsPlugin.cs

Lines changed: 91 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ protected override void Unload()
6767

6868
public override TranslationList DefaultTranslations => new()
6969
{
70-
{ "BuildingsRestriction", "You can't build {0} because you have reached the limit of max {1} buildings." },
71-
{ "BarricadesRestriction", "You can't build {0} because you have reached the limit of max {1} barricades." },
72-
{ "StructuresRestriction", "You can't build {0} because you have reached the limit of max {1} structures." },
73-
{ "SpecificRestriction", "You can't build {0} because you have reached the limit of max {1} {2}." },
74-
{ "SpecificRestrictionInfo", "You have placed {0}/{1} {2}." },
75-
{ "PlayerBuildingStatsYou", "You have placed {0}{1} barricades and {2}{3} structures, so in total {4}{5} buildings." },
76-
{ "PlayerBuildingStats", "{0} have placed {1}{2} barricades and {3}{4} structures, so in total {5}{6} buildings." },
77-
{ "BuildingStats", "{0} players have built {1} barricades and {2} structures, so in total {3} buildings." },
78-
{ "PlayerNotFound", "Player {0} not found." },
70+
{ "BuildingsRestriction", "You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1}[[/b]] buildings." },
71+
{ "BarricadesRestriction", "You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1}[[/b]] barricades." },
72+
{ "StructuresRestriction", "You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1}[[/b]] structures." },
73+
{ "SpecificRestriction", "You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1} {2}[[/b]]." },
74+
{ "SpecificRestrictionInfo", "You have placed [[b]]{0}/{1} {2}[[/b]]." },
75+
{ "PlayerBuildingStatsYou", "You have placed [[b]]{0}{1}[[/b]] barricades and [[b]]{2}{3}[[/b]] structures, so in total [[b]]{4}{5}[[/b]] buildings." },
76+
{ "PlayerBuildingStats", "[[b]]{0}[[/b]] have placed [[b]]{1}{2}[[/b]] barricades and [[b]]{3}{4}[[/b]] structures, so in total [[b]]{5}{6}[[/b]] buildings." },
77+
{ "BuildingStats", "[[b]]{0}[[/b]] players have built [[b]]{1}[[/b]] barricades and [[b]]{2}[[/b]] structures, so in total [[b]]{3}[[/b]] buildings." },
78+
{ "PlayerNotFound", "Player [[b]]{0}[[/b]] not found." },
7979
{ "BuildingStatsOtherNoPermission", "You don't have permission to check other player building stats." },
80-
{ "MaxBarricadeHeightRestriction", "You can't build {0} because it's higher than max {1}m height above the ground." },
81-
{ "MaxStructureHeightRestriction", "You can't build {0} because it's higher than max {1}m height above the ground." }
80+
{ "MaxBarricadeHeightRestriction", "You can't build [[b]]{0}[[/b]] because it's higher than max [[b]]{1}m[[/b]] height above the ground." },
81+
{ "MaxStructureHeightRestriction", "You can't build [[b]]{0}[[/b]] because it's higher than max [[b]]{1}m[[/b]] height above the ground." }
8282
};
8383

8484
private void OnLevelLoaded(int level)
@@ -143,7 +143,7 @@ private void LogDebug(string message)
143143
public decimal GetPlayerBuildingsMultiplier(IRocketPlayer player)
144144
{
145145
decimal multiplier = 1;
146-
if (Configuration.Instance.Multipliers.Length > 0)
146+
if (Configuration.Instance.Multipliers != null && Configuration.Instance.Multipliers.Length > 0)
147147
{
148148
foreach (PermissionMultiplier permissionMultiplier in Configuration.Instance.Multipliers.OrderByDescending(x => x.Multiplier))
149149
{
@@ -191,8 +191,6 @@ private void OnDeployBarricadeRequested(Barricade barricade, ItemBarricadeAsset
191191
decimal multiplier = GetPlayerBuildingsMultiplier(unturnedPlayer);
192192
PlayerBuildings playerBuildings = Database.GetPlayerBuildings(owner);
193193

194-
int itemIdCount = playerBuildings?.Barricades.Count(x => x.ItemId == barricade.asset.id) ?? 0;
195-
int buildCount = playerBuildings?.Barricades.Count(x => x.Build == barricade.asset.build) ?? 0;
196194
string itemName = barricade.asset.itemName;
197195

198196
if (Configuration.Instance.EnableMaxBuildings)
@@ -221,35 +219,42 @@ private void OnDeployBarricadeRequested(Barricade barricade, ItemBarricadeAsset
221219
}
222220
}
223221

224-
BuildingRestriction itemIdRestriction = Configuration.Instance.Barricades.FirstOrDefault(x => x.ItemId != 0 && x.ItemId == barricade.asset.id);
225-
if (itemIdRestriction != null)
222+
if (Configuration.Instance.Barricades != null && Configuration.Instance.Barricades.Any())
226223
{
227-
int max = (int)(itemIdRestriction.Max * multiplier);
228-
if (max <= itemIdCount)
229-
{
230-
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, itemIdRestriction.Name);
231-
shouldAllow = false;
232-
} else
233-
{
234-
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", itemIdCount + 1, max, itemIdRestriction.Name);
235-
}
236-
return;
237-
}
224+
int itemIdCount = playerBuildings?.Barricades.Count(x => x.ItemId == barricade.asset.id) ?? 0;
225+
int buildCount = playerBuildings?.Barricades.Count(x => x.Build == barricade.asset.build) ?? 0;
238226

239-
BuildingRestriction buildRestriction = Configuration.Instance.Barricades.FirstOrDefault(x => x.Build != null && x.Build.Equals(barricade.asset.build.ToString()));
240-
if (buildRestriction != null)
241-
{
242-
int max = (int)(buildRestriction.Max * multiplier);
243-
if (max <= buildCount)
227+
BuildingRestriction itemIdRestriction = Configuration.Instance.Barricades.FirstOrDefault(x => x.ItemId != 0 && x.ItemId == barricade.asset.id);
228+
if (itemIdRestriction != null)
244229
{
245-
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, buildRestriction.Name);
246-
shouldAllow = false;
230+
int max = (int)(itemIdRestriction.Max * multiplier);
231+
if (max <= itemIdCount)
232+
{
233+
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, itemIdRestriction.Name);
234+
shouldAllow = false;
235+
}
236+
else
237+
{
238+
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", itemIdCount + 1, max, itemIdRestriction.Name);
239+
}
240+
return;
247241
}
248-
else
242+
243+
BuildingRestriction buildRestriction = Configuration.Instance.Barricades.FirstOrDefault(x => x.Build != null && x.Build.Equals(barricade.asset.build.ToString()));
244+
if (buildRestriction != null)
249245
{
250-
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", buildCount + 1, max, buildRestriction.Name);
246+
int max = (int)(buildRestriction.Max * multiplier);
247+
if (max <= buildCount)
248+
{
249+
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, buildRestriction.Name);
250+
shouldAllow = false;
251+
}
252+
else
253+
{
254+
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", buildCount + 1, max, buildRestriction.Name);
255+
}
256+
return;
251257
}
252-
return;
253258
}
254259
}
255260

@@ -290,7 +295,7 @@ private void OnDeployStructureRequested(Structure structure, ItemStructureAsset
290295

291296
if (Configuration.Instance.EnableMaxBuildings)
292297
{
293-
int buildingsCount = ((playerBuildings.Structures?.Count ?? 0) + (playerBuildings?.Barricades.Count ?? 0));
298+
int buildingsCount = ((playerBuildings?.Structures.Count ?? 0) + (playerBuildings?.Barricades.Count ?? 0));
294299
int maxBuildings = (int)(Configuration.Instance.MaxBuildings * multiplier);
295300

296301
if (maxBuildings <= buildingsCount)
@@ -314,38 +319,42 @@ private void OnDeployStructureRequested(Structure structure, ItemStructureAsset
314319
}
315320
}
316321

317-
int itemIdCount = playerBuildings?.Structures.Count(x => x.ItemId == structure.asset.id) ?? 0;
318-
int constructCount = playerBuildings?.Structures.Count(x => x.Construct == structure.asset.construct) ?? 0;
319-
320-
BuildingRestriction itemIdRestriction = Configuration.Instance.Structures.FirstOrDefault(x => x.ItemId != 0 && x.ItemId == structure.asset.id);
321-
if (itemIdRestriction != null)
322+
if (Configuration.Instance.Structures != null && Configuration.Instance.Structures.Any())
322323
{
323-
int max = (int)(itemIdRestriction.Max * multiplier);
324-
if (max <= itemIdCount)
325-
{
326-
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, itemIdRestriction.Name);
327-
shouldAllow = false;
328-
} else
329-
{
330-
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", itemIdCount + 1, max, itemIdRestriction.Name);
331-
}
332-
return;
333-
}
324+
int itemIdCount = playerBuildings?.Structures.Count(x => x.ItemId == structure.asset.id) ?? 0;
325+
int constructCount = playerBuildings?.Structures.Count(x => x.Construct == structure.asset.construct) ?? 0;
334326

335-
BuildingRestriction structRestriction = Configuration.Instance.Structures.FirstOrDefault(x => x.Construct != null && x.Construct.Equals(structure.asset.construct.ToString(), StringComparison.CurrentCultureIgnoreCase));
336-
if (structRestriction != null)
337-
{
338-
int max = (int)(structRestriction.Max * multiplier);
339-
if (max <= constructCount)
327+
BuildingRestriction itemIdRestriction = Configuration.Instance.Structures.FirstOrDefault(x => x.ItemId != 0 && x.ItemId == structure.asset.id);
328+
if (itemIdRestriction != null)
340329
{
341-
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, structRestriction.Name);
342-
shouldAllow = false;
330+
int max = (int)(itemIdRestriction.Max * multiplier);
331+
if (max <= itemIdCount)
332+
{
333+
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, itemIdRestriction.Name);
334+
shouldAllow = false;
335+
}
336+
else
337+
{
338+
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", itemIdCount + 1, max, itemIdRestriction.Name);
339+
}
340+
return;
343341
}
344-
else
342+
343+
BuildingRestriction structRestriction = Configuration.Instance.Structures.FirstOrDefault(x => x.Construct != null && x.Construct.Equals(structure.asset.construct.ToString(), StringComparison.CurrentCultureIgnoreCase));
344+
if (structRestriction != null)
345345
{
346-
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", constructCount + 1, max, structRestriction.Name);
346+
int max = (int)(structRestriction.Max * multiplier);
347+
if (max <= constructCount)
348+
{
349+
SendMessageToPlayer(unturnedPlayer, "SpecificRestriction", itemName, max, structRestriction.Name);
350+
shouldAllow = false;
351+
}
352+
else
353+
{
354+
SendMessageToPlayer(unturnedPlayer, "SpecificRestrictionInfo", constructCount + 1, max, structRestriction.Name);
355+
}
356+
return;
347357
}
348-
return;
349358
}
350359
}
351360

@@ -375,8 +384,24 @@ private void OnStructureSpawned(StructureRegion region, StructureDrop drop)
375384

376385
internal void SendMessageToPlayer(IRocketPlayer player, string translationKey, params object[] placeholder)
377386
{
378-
string message = Translate(translationKey, placeholder);
379-
UnturnedChat.Say(player, message, MessageColor, true);
387+
if (player == null)
388+
{
389+
return;
390+
}
391+
392+
string msg = Translate(translationKey, placeholder);
393+
msg = msg.Replace("[[", "<").Replace("]]", ">");
394+
if (player is ConsolePlayer)
395+
{
396+
Logger.Log(msg);
397+
return;
398+
}
399+
400+
UnturnedPlayer unturnedPlayer = (UnturnedPlayer)player;
401+
if (unturnedPlayer != null)
402+
{
403+
ChatManager.serverSendMessage(msg, MessageColor, null, unturnedPlayer.SteamPlayer(), EChatMode.SAY, Configuration.Instance.MessageIconUrl, true);
404+
}
380405
}
381406
}
382407
}

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ FORTIFICATION, BARRICADE, DOOR, GATE, BED, LADDER, STORAGE, FARM, TORCH, CAMPFIR
2929
<?xml version="1.0" encoding="utf-8"?>
3030
<BuildingRestrictionsConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3131
<MessageColor>yellow</MessageColor>
32+
<MessageIconUrl>https://i.imgur.com/LlEcfBg.png</MessageIconUrl>
3233
<EnableMaxBuildings>false</EnableMaxBuildings>
3334
<MaxBuildings>200</MaxBuildings>
3435
<EnableMaxBarricades>false</EnableMaxBarricades>
@@ -59,17 +60,17 @@ FORTIFICATION, BARRICADE, DOOR, GATE, BED, LADDER, STORAGE, FARM, TORCH, CAMPFIR
5960
```xml
6061
<?xml version="1.0" encoding="utf-8"?>
6162
<Translations xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
62-
<Translation Id="BuildingsRestriction" Value="You can't build {0} because you have reached the limit of max {1} buildings." />
63-
<Translation Id="BarricadesRestriction" Value="You can't build {0} because you have reached the limit of max {1} barricades." />
64-
<Translation Id="StructuresRestriction" Value="You can't build {0} because you have reached the limit of max {1} structures." />
65-
<Translation Id="SpecificRestriction" Value="You can't build {0} because you have reached the limit of max {1} {2}." />
66-
<Translation Id="SpecificRestrictionInfo" Value="You have placed {0}/{1} {2}." />
67-
<Translation Id="PlayerBuildingStatsYou" Value="You have placed {0}{1} barricades and {2}{3} structures, so in total {4}{5} buildings." />
68-
<Translation Id="PlayerBuildingStats" Value="{0} have placed {1}{2} barricades and {3}{4} structures, so in total {5}{6} buildings." />
69-
<Translation Id="BuildingStats" Value="{0} players have built {1} barricades and {2} structures, so in total {3} buildings." />
70-
<Translation Id="PlayerNotFound" Value="Player {0} not found." />
63+
<Translation Id="BuildingsRestriction" Value="You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1}[[/b]] buildings." />
64+
<Translation Id="BarricadesRestriction" Value="You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1}[[/b]] barricades." />
65+
<Translation Id="StructuresRestriction" Value="You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1}[[/b]] structures." />
66+
<Translation Id="SpecificRestriction" Value="You can't build [[b]]{0}[[/b]] because you have reached the limit of max [[b]]{1} {2}[[/b]]." />
67+
<Translation Id="SpecificRestrictionInfo" Value="You have placed [[b]]{0}/{1} {2}[[/b]]." />
68+
<Translation Id="PlayerBuildingStatsYou" Value="You have placed [[b]]{0}{1}[[/b]] barricades and [[b]]{2}{3}[[/b]] structures, so in total [[b]]{4}{5}[[/b]] buildings." />
69+
<Translation Id="PlayerBuildingStats" Value="[[b]]{0}[[/b]] have placed [[b]]{1}{2}[[/b]] barricades and [[b]]{3}{4}[[/b]] structures, so in total [[b]]{5}{6}[[/b]] buildings." />
70+
<Translation Id="BuildingStats" Value="[[b]]{0}[[/b]] players have built [[b]]{1}[[/b]] barricades and [[b]]{2}[[/b]] structures, so in total [[b]]{3}[[/b]] buildings." />
71+
<Translation Id="PlayerNotFound" Value="Player [[b]]{0}[[/b]] not found." />
7172
<Translation Id="BuildingStatsOtherNoPermission" Value="You don't have permission to check other player building stats." />
72-
<Translation Id="MaxBarricadeHeightRestriction" Value="You can't build {0} because it's higher than max {1}m height above the ground." />
73-
<Translation Id="MaxStructureHeightRestriction" Value="You can't build {0} because it's higher than max {1}m height above the ground." />
73+
<Translation Id="MaxBarricadeHeightRestriction" Value="You can't build [[b]]{0}[[/b]] because it's higher than max [[b]]{1}m[[/b]] height above the ground." />
74+
<Translation Id="MaxStructureHeightRestriction" Value="You can't build [[b]]{0}[[/b]] because it's higher than max [[b]]{1}m[[/b]] height above the ground." />
7475
</Translations>
7576
```

thumbnail.png

106 KB
Loading

0 commit comments

Comments
 (0)