@@ -13,17 +13,19 @@ namespace Feli.RocketMod.Teleporting
1313{
1414 public class TeleportsManager : IDisposable
1515 {
16- private readonly Plugin _plugin ;
17- private readonly Color _messageColor ;
18- private readonly Configuration _configuration ;
19- private readonly List < Tuple < UnturnedPlayer , UnturnedPlayer > > _teleportRequests ;
20- private readonly Dictionary < UnturnedPlayer , DateTime > _cooldowns ;
21- private readonly Dictionary < UnturnedPlayer , DateTime > _teleportProtections ;
22-
16+ private Plugin _plugin ;
17+ private Color _messageColor ;
18+ private Configuration _configuration ;
19+ private List < Tuple < UnturnedPlayer , UnturnedPlayer > > _teleportRequests ;
20+ private Dictionary < UnturnedPlayer , DateTime > _cooldowns ;
21+ private Dictionary < UnturnedPlayer , DateTime > _teleportProtections ;
22+ private Dictionary < UnturnedPlayer , DateTime > _playersLastCombat ;
23+
2324 public TeleportsManager ( Plugin plugin )
2425 {
2526 _teleportRequests = new List < Tuple < UnturnedPlayer , UnturnedPlayer > > ( ) ;
2627 _teleportProtections = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
28+ _playersLastCombat = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
2729 _cooldowns = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
2830 _plugin = plugin ;
2931 _configuration = plugin . Configuration . Instance ;
@@ -58,6 +60,20 @@ public void Send(UnturnedPlayer sender, UnturnedPlayer target)
5860 return ;
5961 }
6062
63+ if ( ! _configuration . TeleportCombatAllowed )
64+ {
65+ var combat = GetLastCombat ( sender ) ;
66+
67+ var combatTime = combat . AddSeconds ( _configuration . TeleportCombatTime ) ;
68+
69+ if ( combatTime > DateTime . Now )
70+ {
71+ var waitTime = ( combatTime - DateTime . Now ) . TotalSeconds ;
72+ UnturnedChat . Say ( sender , _plugin . Translate ( "TpaCommand:Send:Combat" , Math . Round ( waitTime ) ) , true ) ;
73+ return ;
74+ }
75+ }
76+
6177 UpdateCooldown ( sender ) ;
6278
6379 request = new Tuple < UnturnedPlayer , UnturnedPlayer > ( sender , target ) ;
@@ -156,6 +172,23 @@ private bool ValidateRequest(Tuple<UnturnedPlayer, UnturnedPlayer> request)
156172 return false ;
157173 }
158174
175+ if ( ! _configuration . TeleportCombatAllowed )
176+ {
177+ var combat = GetLastCombat ( sender ) ;
178+
179+ var combatTime = combat . AddSeconds ( _configuration . TeleportCombatTime ) ;
180+
181+ if ( combatTime > DateTime . Now )
182+ {
183+ var waitTime = ( combatTime - DateTime . Now ) . TotalSeconds ;
184+
185+ UnturnedChat . Say ( sender , _plugin . Translate ( "TpaValidation:Combat:Sender" , Math . Round ( waitTime ) ) , true ) ;
186+ UnturnedChat . Say ( target , _plugin . Translate ( "TpaValidation:Combat:Target" , sender . DisplayName ) , true ) ;
187+
188+ return false ;
189+ }
190+ }
191+
159192 return true ;
160193 }
161194
@@ -176,19 +209,27 @@ private void OnLeave(UnturnedPlayer player)
176209 if ( cooldown != DateTime . MinValue )
177210 _cooldowns . Remove ( player ) ;
178211
179- var proctection = GetTeleportProtection ( player ) ;
212+ var protection = GetTeleportProtection ( player ) ;
180213
181- if ( proctection != DateTime . MinValue )
214+ if ( protection != DateTime . MinValue )
182215 _teleportProtections . Remove ( player ) ;
216+
217+ var lastCombat = GetLastCombat ( player ) ;
218+
219+ if ( lastCombat != DateTime . MinValue )
220+ _playersLastCombat . Remove ( player ) ;
183221 }
184222
185223 private void OnPlayerAllowedToDamagePlayer ( Player nativeInstigator , Player nativeVictim , ref bool isAllowed )
186224 {
225+ var instigator = UnturnedPlayer . FromPlayer ( nativeInstigator ) ;
226+
227+ UpdateLastCombat ( instigator ) ;
228+
187229 if ( ! _configuration . TeleportProtection )
188230 return ;
189231
190232 var victim = UnturnedPlayer . FromPlayer ( nativeVictim ) ;
191- var instigator = UnturnedPlayer . FromPlayer ( nativeInstigator ) ;
192233
193234 if ( _teleportProtections . ContainsKey ( victim ) )
194235 {
@@ -218,6 +259,22 @@ private void UpdateCooldown(UnturnedPlayer player)
218259 _cooldowns . Add ( player , DateTime . Now ) ;
219260 }
220261
262+ private void UpdateLastCombat ( UnturnedPlayer player )
263+ {
264+ if ( _playersLastCombat . ContainsKey ( player ) )
265+ _playersLastCombat [ player ] = DateTime . Now ;
266+ else
267+ _playersLastCombat . Add ( player , DateTime . Now ) ;
268+ }
269+
270+ private DateTime GetLastCombat ( UnturnedPlayer player )
271+ {
272+ if ( _playersLastCombat . ContainsKey ( player ) )
273+ return _playersLastCombat [ player ] ;
274+
275+ return DateTime . MinValue ;
276+ }
277+
221278 private void UpdateTeleportProtection ( UnturnedPlayer player )
222279 {
223280 if ( _teleportProtections . ContainsKey ( player ) )
@@ -244,6 +301,12 @@ private DateTime GetCooldown(UnturnedPlayer player)
244301
245302 public void Dispose ( )
246303 {
304+ _teleportRequests = null ;
305+ _teleportProtections = null ;
306+ _cooldowns = null ;
307+ _playersLastCombat = null ;
308+ _plugin = null ;
309+ _configuration = null ;
247310 DamageTool . onPlayerAllowedToDamagePlayer -= OnPlayerAllowedToDamagePlayer ;
248311 U . Events . OnPlayerDisconnected -= OnLeave ;
249312 }
0 commit comments