From 7c0f07fd13e17c12a69b3c804170a0c852772302 Mon Sep 17 00:00:00 2001 From: MCrow Date: Sat, 2 Nov 2024 16:18:46 +0100 Subject: [PATCH] Fix rare null reference error and add hidden ProtectionEnabledMessageDelay option --- .../Components/RespawnProtectionComponent.cs | 3 ++- RespawnProtection/RespawnProtection.csproj | 2 +- .../RespawnProtectionConfiguration.cs | 2 ++ RespawnProtection/RespawnProtectionPlugin.cs | 18 ++++++++++++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/RespawnProtection/Components/RespawnProtectionComponent.cs b/RespawnProtection/Components/RespawnProtectionComponent.cs index 99cedfb..68f2792 100644 --- a/RespawnProtection/Components/RespawnProtectionComponent.cs +++ b/RespawnProtection/Components/RespawnProtectionComponent.cs @@ -108,7 +108,8 @@ private IEnumerator ProtectionTimer() DisableProtection(); if (configuration.SendProtectionDisabledExpiredMessage) { - pluginInstance.SendMessageToPlayer(UnturnedPlayer.FromCSteamID(SteamID), "SpawnProtectionDisabledExpired"); + UnturnedPlayer unturnedPlayer = UnturnedPlayer.FromCSteamID(SteamID); + pluginInstance.SendMessageToPlayer(unturnedPlayer, "SpawnProtectionDisabledExpired"); } } diff --git a/RespawnProtection/RespawnProtection.csproj b/RespawnProtection/RespawnProtection.csproj index 08f4cb3..7e932b5 100644 --- a/RespawnProtection/RespawnProtection.csproj +++ b/RespawnProtection/RespawnProtection.csproj @@ -4,7 +4,7 @@ net48 latest RestoreMonarchy.RespawnProtection - 1.2.0 + 1.2.1 diff --git a/RespawnProtection/RespawnProtectionConfiguration.cs b/RespawnProtection/RespawnProtectionConfiguration.cs index cb8d5cb..fc131a9 100644 --- a/RespawnProtection/RespawnProtectionConfiguration.cs +++ b/RespawnProtection/RespawnProtectionConfiguration.cs @@ -20,6 +20,8 @@ public class RespawnProtectionConfiguration : IRocketPluginConfiguration public float EffectTriggerRate { get; set; } public float AttackMessageRate { get; set; } public bool SendProtectionEnabledMessage { get; set; } = true; + public float ProtectionEnabledMessageDelay { get; set; } = 0f; + public bool ShouldSerializeSpawnProtectionEnabledMessageDelay() => ProtectionEnabledMessageDelay > 0; public bool SendProtectionDisabledExpiredMessage { get; set; } = true; public bool SendProtectionDisabledOtherMessage { get; set; } = true; diff --git a/RespawnProtection/RespawnProtectionPlugin.cs b/RespawnProtection/RespawnProtectionPlugin.cs index d9a2f0c..7ae776c 100644 --- a/RespawnProtection/RespawnProtectionPlugin.cs +++ b/RespawnProtection/RespawnProtectionPlugin.cs @@ -4,6 +4,7 @@ using Rocket.API.Collections; using Rocket.Core.Logging; using Rocket.Core.Plugins; +using Rocket.Core.Utils; using Rocket.Unturned; using Rocket.Unturned.Chat; using Rocket.Unturned.Player; @@ -68,7 +69,17 @@ private void OnPlayerConnected(UnturnedPlayer player) if (Configuration.Instance.SendProtectionEnabledMessage) { string duration = Configuration.Instance.ProtectionDuration.ToString("N0"); - SendMessageToPlayer(player, "SpawnProtectionEnabled", duration); + if (Configuration.Instance.ProtectionEnabledMessageDelay > 0) + { + TaskDispatcher.QueueOnMainThread(() => + { + SendMessageToPlayer(player, "SpawnProtectionEnabled", duration); + }, Configuration.Instance.ProtectionEnabledMessageDelay); + + } else + { + SendMessageToPlayer(player, "SpawnProtectionEnabled", duration); + } } } } @@ -151,7 +162,10 @@ internal void SendMessageToPlayer(IRocketPlayer player, string translationKey, p } UnturnedPlayer unturnedPlayer = (UnturnedPlayer)player; - ChatManager.serverSendMessage(msg, MessageColor, null, unturnedPlayer.SteamPlayer(), EChatMode.SAY, Configuration.Instance.MessageIconUrl, true); + if (unturnedPlayer != null) + { + ChatManager.serverSendMessage(msg, MessageColor, null, unturnedPlayer.SteamPlayer(), EChatMode.SAY, Configuration.Instance.MessageIconUrl, true); + } } } } \ No newline at end of file