Skip to content

Commit 4c6c8a4

Browse files
Add support for rich text and MessageIconUrl
1 parent e85cce2 commit 4c6c8a4

File tree

6 files changed

+56
-47
lines changed

6 files changed

+56
-47
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ Gives players protection from other players damaging them after respawning.
1212
- Protection can be disabled for home spawns, so players can't abuse it to protect their base
1313
- Protection can be disabled for PVE damage
1414
- Configurable effect to show when protection is active
15+
- Rich text and icon support for messages
1516

1617
## Configuration
1718
```xml
1819
<?xml version="1.0" encoding="utf-8"?>
1920
<RespawnProtectionConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2021
<MessageColor>yellow</MessageColor>
22+
<MessageIconUrl>https://i.imgur.com/Di3NWF0.png</MessageIconUrl>
2123
<ProtectionDuration>10</ProtectionDuration>
2224
<EnableHomeSpawnProtection>false</EnableHomeSpawnProtection>
2325
<EnableJoinSpawnProtection>false</EnableJoinSpawnProtection>
@@ -41,18 +43,18 @@ Gives players protection from other players damaging them after respawning.
4143
```xml
4244
<?xml version="1.0" encoding="utf-8"?>
4345
<Translations xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
44-
<Translation Id="SpawnProtectionEnabled" Value="Spawn protection enabled for {0} seconds." />
46+
<Translation Id="SpawnProtectionEnabled" Value="Spawn protection enabled for [[b]]{0}[[/b]] seconds." />
4547
<Translation Id="SpawnProtectionDisabledExpired" Value="Your spawn protection has expired." />
4648
<Translation Id="SpawnProtectionDisabledOnMove" Value="Spawn protection disabled because you moved too far." />
4749
<Translation Id="SpawnProtectionDisabledOnEquipGun" Value="Spawn protection disabled because you equipped a gun." />
4850
<Translation Id="SpawnProtectionDisabledOnEquipMelee" Value="Spawn protection disabled because you equipped a melee weapon." />
4951
<Translation Id="SpawnProtectionDisabledOnEquipThrowable" Value="Spawn protection disabled because you equipped a throwable." />
5052
<Translation Id="SpawnProtectionDisabledOnAttack" Value="Spawn protection disabled because you attacked." />
5153
<Translation Id="SpawnProtectionDisabledWithCommand" Value="Spawn protection disabled by command." />
52-
<Translation Id="PlayerHasProtection" Value="You can't hurt {0} because they have spawn protection." />
54+
<Translation Id="PlayerHasProtection" Value="You can't hurt [[b]]{0}[[/b]] because they have spawn protection." />
5355
<Translation Id="SpawnProtectionCommandFormat" Value="You must specify player name." />
5456
<Translation Id="PlayerNotFound" Value="Player not found." />
55-
<Translation Id="SpawnProtectionCommandDisabled" Value="Spawn protection disabled for {0}." />
56-
<Translation Id="SpawnProtectionCommandEnabled" Value="Spawn protection enabled for {0} for {1} seconds." />
57+
<Translation Id="SpawnProtectionCommandDisabled" Value="Spawn protection disabled for [[b]]{0}[[/b]]." />
58+
<Translation Id="SpawnProtectionCommandEnabled" Value="Spawn protection enabled for [[b]]{0}[[/b]] for [[b]]{1}[[/b]] seconds." />
5759
</Translations>
5860
```
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using RestoreMonarchy.RespawnProtection.Components;
22
using Rocket.API;
3-
using Rocket.Unturned.Chat;
43
using Rocket.Unturned.Player;
54
using System.Collections.Generic;
65

@@ -14,14 +13,14 @@ public void Execute(IRocketPlayer caller, string[] command)
1413
{
1514
if (command.Length < 1)
1615
{
17-
UnturnedChat.Say(caller, pluginInstance.Translate("SpawnProtectionCommandFormat"), pluginInstance.MessageColor);
16+
pluginInstance.SendMessageToPlayer(caller, "SpawnProtectionCommandFormat");
1817
return;
1918
}
2019

2120
UnturnedPlayer target = UnturnedPlayer.FromName(command[0]);
2221
if (target == null)
2322
{
24-
UnturnedChat.Say(caller, pluginInstance.Translate("PlayerNotFound"), pluginInstance.MessageColor);
23+
pluginInstance.SendMessageToPlayer(caller, "PlayerNotFound");
2524
return;
2625
}
2726

@@ -36,34 +35,27 @@ public void Execute(IRocketPlayer caller, string[] command)
3635
component.DisableProtection();
3736
if (pluginInstance.Configuration.Instance.SendProtectionDisabledExpiredMessage)
3837
{
39-
UnturnedChat.Say(target, pluginInstance.Translate("SpawnProtectionDisabledWithCommand"), pluginInstance.MessageColor);
38+
pluginInstance.SendMessageToPlayer(target, "SpawnProtectionDisabledWithCommand");
4039
}
41-
42-
UnturnedChat.Say(caller, pluginInstance.Translate("SpawnProtectionCommandDisabled", target.DisplayName), pluginInstance.MessageColor);
40+
pluginInstance.SendMessageToPlayer(caller, "SpawnProtectionCommandDisabled", target.DisplayName);
4341
}
4442
else
4543
{
4644
component.EnableProtection();
4745
string duration = pluginInstance.Configuration.Instance.ProtectionDuration.ToString("N0");
4846
if (pluginInstance.Configuration.Instance.SendProtectionEnabledMessage)
4947
{
50-
UnturnedChat.Say(target, pluginInstance.Translate("SpawnProtectionEnabled", duration), pluginInstance.MessageColor);
48+
pluginInstance.SendMessageToPlayer(target, "SpawnProtectionEnabled", duration);
5149
}
52-
53-
UnturnedChat.Say(caller, pluginInstance.Translate("SpawnProtectionCommandEnabled", target.DisplayName, duration), pluginInstance.MessageColor);
50+
pluginInstance.SendMessageToPlayer(caller, "SpawnProtectionCommandEnabled", target.DisplayName, duration);
5451
}
5552
}
5653

5754
public AllowedCaller AllowedCaller => AllowedCaller.Both;
58-
5955
public string Name => "respawnprotection";
60-
6156
public string Help => "";
62-
6357
public string Syntax => "[player] [duration]";
64-
6558
public List<string> Aliases => new();
66-
6759
public List<string> Permissions => new();
6860
}
69-
}
61+
}

RespawnProtection/Components/RespawnProtectionComponent.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using RestoreMonarchy.RespawnProtection.Models;
2-
using Rocket.Unturned.Chat;
2+
using Rocket.Unturned.Player;
33
using SDG.Unturned;
44
using Steamworks;
55
using System;
@@ -32,7 +32,7 @@ void Awake()
3232
void Start()
3333
{
3434
Player.life.onLifeUpdated += OnLifeUpdated;
35-
Player.equipment.onEquipRequested += OnEquipRequested;
35+
Player.equipment.onEquipRequested += OnEquipRequested;
3636
}
3737

3838
void OnDestroy()
@@ -61,7 +61,7 @@ private void OnLifeUpdated(bool isDead)
6161
if (configuration.SendProtectionEnabledMessage)
6262
{
6363
string duration = configuration.ProtectionDuration.ToString("N0");
64-
UnturnedChat.Say(SteamID, pluginInstance.Translate("SpawnProtectionEnabled", duration), pluginInstance.MessageColor);
64+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionEnabled", duration);
6565
}
6666
}
6767
}
@@ -71,7 +71,7 @@ public void EnableProtection()
7171
if (IsProtected)
7272
{
7373
return;
74-
}
74+
}
7575

7676
IsProtected = true;
7777
respawnPosition = Player.transform.position;
@@ -108,8 +108,8 @@ private IEnumerator ProtectionTimer()
108108
DisableProtection();
109109
if (configuration.SendProtectionDisabledExpiredMessage)
110110
{
111-
UnturnedChat.Say(SteamID, pluginInstance.Translate("SpawnProtectionDisabledExpired"), pluginInstance.MessageColor);
112-
}
111+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledExpired");
112+
}
113113
}
114114

115115
private IEnumerator EffectTimer()
@@ -147,11 +147,8 @@ void FixedUpdate()
147147
DisableProtection();
148148
if (configuration.SendProtectionDisabledOtherMessage)
149149
{
150-
if (configuration.SendProtectionDisabledOtherMessage)
151-
{
152-
UnturnedChat.Say(SteamID, pluginInstance.Translate("SpawnProtectionDisabledOnMove"), pluginInstance.MessageColor);
153-
}
154-
}
150+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnMove");
151+
}
155152
}
156153
}
157154
}
@@ -165,26 +162,26 @@ private void OnEquipRequested(PlayerEquipment equipment, ItemJar jar, ItemAsset
165162
DisableProtection();
166163
if (configuration.SendProtectionDisabledOtherMessage)
167164
{
168-
UnturnedChat.Say(SteamID, pluginInstance.Translate("SpawnProtectionDisabledOnEquipGun"), pluginInstance.MessageColor);
169-
}
165+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnEquipGun");
166+
}
170167
}
171168

172169
if (configuration.DisableOnEquipMelee && asset.type == EItemType.MELEE)
173170
{
174171
DisableProtection();
175172
if (configuration.SendProtectionDisabledOtherMessage)
176173
{
177-
UnturnedChat.Say(SteamID, pluginInstance.Translate("SpawnProtectionDisabledOnEquipMelee"), pluginInstance.MessageColor);
178-
}
174+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnEquipMelee");
175+
}
179176
}
180177

181178
if (configuration.DisableOnEquipThrowable && asset.type == EItemType.THROWABLE)
182179
{
183180
DisableProtection();
184181
if (configuration.SendProtectionDisabledOtherMessage)
185182
{
186-
UnturnedChat.Say(SteamID, pluginInstance.Translate("SpawnProtectionDisabledOnEquipThrowable"), pluginInstance.MessageColor);
187-
}
183+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnEquipThrowable");
184+
}
188185
}
189186
}
190187
}

RespawnProtection/RespawnProtection.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.RespawnProtection</RootNamespace>
7-
<Version>1.1.1</Version>
7+
<Version>1.2.0</Version>
88
</PropertyGroup>
99

1010
<ItemGroup>

RespawnProtection/RespawnProtectionConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace RestoreMonarchy.RespawnProtection
55
public class RespawnProtectionConfiguration : IRocketPluginConfiguration
66
{
77
public string MessageColor { get; set; } = "yellow";
8+
public string MessageIconUrl { get; set; } = "https://i.imgur.com/Di3NWF0.png";
89
public float ProtectionDuration { get; set; }
910
public bool EnableHomeSpawnProtection { get; set; }
1011
public bool EnableJoinSpawnProtection { get; set; }
@@ -25,6 +26,7 @@ public class RespawnProtectionConfiguration : IRocketPluginConfiguration
2526
public void LoadDefaults()
2627
{
2728
MessageColor = "yellow";
29+
MessageIconUrl = "https://i.imgur.com/Di3NWF0.png";
2830
ProtectionDuration = 10;
2931
EnableHomeSpawnProtection = false;
3032
EnableJoinSpawnProtection = false;

RespawnProtection/RespawnProtectionPlugin.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using RestoreMonarchy.RespawnProtection.Components;
22
using RestoreMonarchy.RespawnProtection.Models;
3+
using Rocket.API;
34
using Rocket.API.Collections;
45
using Rocket.Core.Logging;
56
using Rocket.Core.Plugins;
@@ -43,19 +44,19 @@ protected override void Unload()
4344

4445
public override TranslationList DefaultTranslations => new()
4546
{
46-
{ "SpawnProtectionEnabled", "Spawn protection enabled for {0} seconds." },
47+
{ "SpawnProtectionEnabled", "Spawn protection enabled for [[b]]{0}[[/b]] seconds." },
4748
{ "SpawnProtectionDisabledExpired", "Your spawn protection has expired." },
4849
{ "SpawnProtectionDisabledOnMove", "Spawn protection disabled because you moved too far." },
4950
{ "SpawnProtectionDisabledOnEquipGun", "Spawn protection disabled because you equipped a gun." },
5051
{ "SpawnProtectionDisabledOnEquipMelee", "Spawn protection disabled because you equipped a melee weapon." },
5152
{ "SpawnProtectionDisabledOnEquipThrowable", "Spawn protection disabled because you equipped a throwable." },
5253
{ "SpawnProtectionDisabledOnAttack", "Spawn protection disabled because you attacked." },
5354
{ "SpawnProtectionDisabledWithCommand", "Spawn protection disabled by command." },
54-
{ "PlayerHasProtection", "You can't hurt {0} because they have spawn protection." },
55+
{ "PlayerHasProtection", "You can't hurt [[b]]{0}[[/b]] because they have spawn protection." },
5556
{ "SpawnProtectionCommandFormat", "You must specify player name." },
5657
{ "PlayerNotFound", "Player not found." },
57-
{ "SpawnProtectionCommandDisabled", "Spawn protection disabled for {0}." },
58-
{ "SpawnProtectionCommandEnabled", "Spawn protection enabled for {0} for {1} seconds." }
58+
{ "SpawnProtectionCommandDisabled", "Spawn protection disabled for [[b]]{0}[[/b]]." },
59+
{ "SpawnProtectionCommandEnabled", "Spawn protection enabled for [[b]]{0}[[/b]] for [[b]]{1}[[/b]] seconds." }
5960
};
6061

6162
private void OnPlayerConnected(UnturnedPlayer player)
@@ -67,7 +68,7 @@ private void OnPlayerConnected(UnturnedPlayer player)
6768
if (Configuration.Instance.SendProtectionEnabledMessage)
6869
{
6970
string duration = Configuration.Instance.ProtectionDuration.ToString("N0");
70-
UnturnedChat.Say(player, Translate("SpawnProtectionEnabled", duration), MessageColor);
71+
SendMessageToPlayer(player, "SpawnProtectionEnabled", duration);
7172
}
7273
}
7374
}
@@ -99,7 +100,8 @@ private void DamagedPlayerRequested(ref DamagePlayerParameters parameters, ref b
99100
{
100101
shouldAllow = false;
101102
}
102-
} else
103+
}
104+
else
103105
{
104106
if (!component.LastAttackMessages.Any(x => x.Player == killer && (DateTime.Now - x.DateTime) <= TimeSpan.FromSeconds(Configuration.Instance.AttackMessageRate)))
105107
{
@@ -109,7 +111,7 @@ private void DamagedPlayerRequested(ref DamagePlayerParameters parameters, ref b
109111
DateTime = DateTime.Now
110112
});
111113

112-
UnturnedChat.Say(parameters.killer, Translate("PlayerHasProtection", parameters.player.channel.owner.playerID.characterName), MessageColor);
114+
SendMessageToPlayer(UnturnedPlayer.FromPlayer(killer), "PlayerHasProtection", parameters.player.channel.owner.playerID.characterName);
113115
}
114116
shouldAllow = false;
115117
}
@@ -123,10 +125,10 @@ private void DamagedPlayerRequested(ref DamagePlayerParameters parameters, ref b
123125
killerComponent.DisableProtection();
124126
if (Configuration.Instance.SendProtectionDisabledOtherMessage)
125127
{
126-
UnturnedChat.Say(parameters.killer, Translate("SpawnProtectionDisabledOnAttack"), MessageColor);
127-
}
128+
SendMessageToPlayer(UnturnedPlayer.FromPlayer(killer), "SpawnProtectionDisabledOnAttack");
129+
}
128130
}
129-
}
131+
}
130132
}
131133

132134
private void OnSelectRespawnPoint(PlayerLife sender, bool wantsToSpawnAtHome, ref UnityEngine.Vector3 position, ref float yaw)
@@ -137,5 +139,19 @@ private void OnSelectRespawnPoint(PlayerLife sender, bool wantsToSpawnAtHome, re
137139
component.IsHomeSpawn = wantsToSpawnAtHome;
138140
}
139141
}
142+
143+
internal void SendMessageToPlayer(IRocketPlayer player, string translationKey, params object[] placeholder)
144+
{
145+
string msg = Translate(translationKey, placeholder);
146+
msg = msg.Replace("[[", "<").Replace("]]", ">");
147+
if (player is ConsolePlayer)
148+
{
149+
Logger.Log(msg);
150+
return;
151+
}
152+
153+
UnturnedPlayer unturnedPlayer = (UnturnedPlayer)player;
154+
ChatManager.serverSendMessage(msg, MessageColor, null, unturnedPlayer.SteamPlayer(), EChatMode.SAY, Configuration.Instance.MessageIconUrl, true);
155+
}
140156
}
141157
}

0 commit comments

Comments
 (0)