Skip to content

Commit b9b7959

Browse files
Add arena spawn protection
1 parent 530c840 commit b9b7959

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Respawn Protection
2-
Gives players protection from other players damaging them after respawning.
2+
Protects players from damage after spawning. Supports survival and arena spawns.
33

44
## Features
55
- Protects players from damage for a configurable amount of time after spawning
@@ -13,6 +13,7 @@ Gives players protection from other players damaging them after respawning.
1313
- Protection can be disabled for PVE damage
1414
- Configurable effect to show when protection is active
1515
- Rich text and icon support for messages
16+
- Arena spawn protection support
1617

1718
## Commands
1819
- `/respawnprotection [player] [duration]` - Enable or disable protection for a player for a specified duration. Admin command.
@@ -26,6 +27,7 @@ Gives players protection from other players damaging them after respawning.
2627
<ProtectionDuration>10</ProtectionDuration>
2728
<EnableHomeSpawnProtection>false</EnableHomeSpawnProtection>
2829
<EnableJoinSpawnProtection>false</EnableJoinSpawnProtection>
30+
<EnableArenaSpawnProtection>true</EnableArenaSpawnProtection>
2931
<MaxMoveDistance>10</MaxMoveDistance>
3032
<ProtectFromPVE>true</ProtectFromPVE>
3133
<DisableOnEquipGun>true</DisableOnEquipGun>

RespawnProtection/Components/RespawnProtectionComponent.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,46 @@ void Start()
3333
{
3434
Player.life.onLifeUpdated += OnLifeUpdated;
3535
Player.equipment.onEquipRequested += OnEquipRequested;
36+
Player.onPlayerTeleported += OnPlayerTeleported;
3637
}
3738

3839
void OnDestroy()
3940
{
4041
Player.life.onLifeUpdated -= OnLifeUpdated;
4142
Player.equipment.onEquipRequested -= OnEquipRequested;
43+
Player.onPlayerTeleported -= OnPlayerTeleported;
44+
StopAllCoroutines();
45+
}
46+
47+
private void OnPlayerTeleported(Player player, Vector3 point)
48+
{
49+
if (!configuration.EnableArenaSpawnProtection)
50+
{
51+
return;
52+
}
53+
if (!LevelManager.isArenaMode || LevelManager.arenaState != EArenaState.SPAWN)
54+
{
55+
return;
56+
}
57+
58+
EnableProtection();
59+
if (configuration.SendProtectionEnabledMessage)
60+
{
61+
string duration = configuration.ProtectionDuration.ToString("N0");
62+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromPlayer(Player), "SpawnProtectionEnabled", duration);
63+
}
4264
}
4365

4466
public Coroutine ProtectionCoroutine { get; private set; }
4567
public Coroutine EffectCoroutine { get; private set; }
4668

4769
private void OnLifeUpdated(bool isDead)
4870
{
71+
if (LevelManager.isArenaMode)
72+
{
73+
return;
74+
}
75+
4976
if (isDead)
5077
{
5178
DisableProtection();
@@ -61,7 +88,7 @@ private void OnLifeUpdated(bool isDead)
6188
if (configuration.SendProtectionEnabledMessage)
6289
{
6390
string duration = configuration.ProtectionDuration.ToString("N0");
64-
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionEnabled", duration);
91+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromPlayer(Player), "SpawnProtectionEnabled", duration);
6592
}
6693
}
6794
}
@@ -105,12 +132,13 @@ private IEnumerator ProtectionTimer()
105132
{
106133
yield return new WaitForSeconds(configuration.ProtectionDuration);
107134

108-
DisableProtection();
109-
if (configuration.SendProtectionDisabledExpiredMessage)
135+
if (configuration.SendProtectionDisabledExpiredMessage && Player != null)
110136
{
111-
UnturnedPlayer unturnedPlayer = UnturnedPlayer.FromCSteamID(SteamID);
137+
UnturnedPlayer unturnedPlayer = UnturnedPlayer.FromPlayer(Player);
112138
pluginInstance.SendMessageToPlayer(unturnedPlayer, "SpawnProtectionDisabledExpired");
113139
}
140+
141+
DisableProtection();
114142
}
115143

116144
private IEnumerator EffectTimer()
@@ -139,16 +167,23 @@ private IEnumerator EffectTimer()
139167
}
140168
}
141169

170+
private float lastCheckTime = 0f;
142171
void FixedUpdate()
143172
{
144173
if (IsProtected && configuration.DisableOnMove)
145174
{
146-
if (Vector3.Distance(respawnPosition, Player.transform.position) > configuration.MaxMoveDistance)
175+
// check every 0.25 seconds
176+
if (Time.time - lastCheckTime >= 0.25f)
147177
{
148-
DisableProtection();
149-
if (configuration.SendProtectionDisabledOtherMessage)
178+
lastCheckTime = Time.time;
179+
180+
if (Vector3.Distance(respawnPosition, Player.transform.position) > configuration.MaxMoveDistance)
150181
{
151-
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnMove");
182+
DisableProtection();
183+
if (configuration.SendProtectionDisabledOtherMessage)
184+
{
185+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromPlayer(Player), "SpawnProtectionDisabledOnMove");
186+
}
152187
}
153188
}
154189
}
@@ -163,7 +198,7 @@ private void OnEquipRequested(PlayerEquipment equipment, ItemJar jar, ItemAsset
163198
DisableProtection();
164199
if (configuration.SendProtectionDisabledOtherMessage)
165200
{
166-
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnEquipGun");
201+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromPlayer(Player), "SpawnProtectionDisabledOnEquipGun");
167202
}
168203
}
169204

@@ -172,7 +207,7 @@ private void OnEquipRequested(PlayerEquipment equipment, ItemJar jar, ItemAsset
172207
DisableProtection();
173208
if (configuration.SendProtectionDisabledOtherMessage)
174209
{
175-
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnEquipMelee");
210+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromPlayer(Player), "SpawnProtectionDisabledOnEquipMelee");
176211
}
177212
}
178213

@@ -181,7 +216,7 @@ private void OnEquipRequested(PlayerEquipment equipment, ItemJar jar, ItemAsset
181216
DisableProtection();
182217
if (configuration.SendProtectionDisabledOtherMessage)
183218
{
184-
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledOnEquipThrowable");
219+
pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromPlayer(Player), "SpawnProtectionDisabledOnEquipThrowable");
185220
}
186221
}
187222
}

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.2.1</Version>
7+
<Version>1.3.0</Version>
88
</PropertyGroup>
99

1010
<ItemGroup>

RespawnProtection/RespawnProtectionConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class RespawnProtectionConfiguration : IRocketPluginConfiguration
99
public float ProtectionDuration { get; set; }
1010
public bool EnableHomeSpawnProtection { get; set; }
1111
public bool EnableJoinSpawnProtection { get; set; }
12+
public bool EnableArenaSpawnProtection { get; set; } = true;
1213
public float MaxMoveDistance { get; set; }
1314
public bool ProtectFromPVE { get; set; }
1415
public bool DisableOnEquipGun { get; set; }
@@ -31,6 +32,7 @@ public void LoadDefaults()
3132
ProtectionDuration = 10;
3233
EnableHomeSpawnProtection = false;
3334
EnableJoinSpawnProtection = false;
35+
EnableArenaSpawnProtection = true;
3436
MaxMoveDistance = 10f;
3537
ProtectFromPVE = true;
3638
DisableOnEquipGun = true;

RespawnProtection/RespawnProtectionPlugin.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class RespawnProtectionPlugin : RocketPlugin<RespawnProtectionConfigurati
2222
protected override void Load()
2323
{
2424
Instance = this;
25-
MessageColor = UnturnedChat.GetColorFromName(Configuration.Instance.MessageColor, UnityEngine.Color.yellow);
25+
MessageColor = UnturnedChat.GetColorFromName(Configuration.Instance.MessageColor, UnityEngine.Color.green);
2626

2727
U.Events.OnPlayerConnected += OnPlayerConnected;
2828
U.Events.OnPlayerDisconnected += OnPlayerDisconnected;
@@ -153,6 +153,11 @@ private void OnSelectRespawnPoint(PlayerLife sender, bool wantsToSpawnAtHome, re
153153

154154
internal void SendMessageToPlayer(IRocketPlayer player, string translationKey, params object[] placeholder)
155155
{
156+
if (player == null)
157+
{
158+
return;
159+
}
160+
156161
string msg = Translate(translationKey, placeholder);
157162
msg = msg.Replace("[[", "<").Replace("]]", ">");
158163
if (player is ConsolePlayer)

0 commit comments

Comments
 (0)