@@ -13,17 +13,19 @@ namespace Feli.RocketMod.Teleporting
13
13
{
14
14
public class TeleportsManager : IDisposable
15
15
{
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
+
23
24
public TeleportsManager ( Plugin plugin )
24
25
{
25
26
_teleportRequests = new List < Tuple < UnturnedPlayer , UnturnedPlayer > > ( ) ;
26
27
_teleportProtections = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
28
+ _playersLastCombat = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
27
29
_cooldowns = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
28
30
_plugin = plugin ;
29
31
_configuration = plugin . Configuration . Instance ;
@@ -58,6 +60,20 @@ public void Send(UnturnedPlayer sender, UnturnedPlayer target)
58
60
return ;
59
61
}
60
62
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
+
61
77
UpdateCooldown ( sender ) ;
62
78
63
79
request = new Tuple < UnturnedPlayer , UnturnedPlayer > ( sender , target ) ;
@@ -156,6 +172,23 @@ private bool ValidateRequest(Tuple<UnturnedPlayer, UnturnedPlayer> request)
156
172
return false ;
157
173
}
158
174
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
+
159
192
return true ;
160
193
}
161
194
@@ -176,19 +209,27 @@ private void OnLeave(UnturnedPlayer player)
176
209
if ( cooldown != DateTime . MinValue )
177
210
_cooldowns . Remove ( player ) ;
178
211
179
- var proctection = GetTeleportProtection ( player ) ;
212
+ var protection = GetTeleportProtection ( player ) ;
180
213
181
- if ( proctection != DateTime . MinValue )
214
+ if ( protection != DateTime . MinValue )
182
215
_teleportProtections . Remove ( player ) ;
216
+
217
+ var lastCombat = GetLastCombat ( player ) ;
218
+
219
+ if ( lastCombat != DateTime . MinValue )
220
+ _playersLastCombat . Remove ( player ) ;
183
221
}
184
222
185
223
private void OnPlayerAllowedToDamagePlayer ( Player nativeInstigator , Player nativeVictim , ref bool isAllowed )
186
224
{
225
+ var instigator = UnturnedPlayer . FromPlayer ( nativeInstigator ) ;
226
+
227
+ UpdateLastCombat ( instigator ) ;
228
+
187
229
if ( ! _configuration . TeleportProtection )
188
230
return ;
189
231
190
232
var victim = UnturnedPlayer . FromPlayer ( nativeVictim ) ;
191
- var instigator = UnturnedPlayer . FromPlayer ( nativeInstigator ) ;
192
233
193
234
if ( _teleportProtections . ContainsKey ( victim ) )
194
235
{
@@ -218,6 +259,22 @@ private void UpdateCooldown(UnturnedPlayer player)
218
259
_cooldowns . Add ( player , DateTime . Now ) ;
219
260
}
220
261
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
+
221
278
private void UpdateTeleportProtection ( UnturnedPlayer player )
222
279
{
223
280
if ( _teleportProtections . ContainsKey ( player ) )
@@ -244,6 +301,12 @@ private DateTime GetCooldown(UnturnedPlayer player)
244
301
245
302
public void Dispose ( )
246
303
{
304
+ _teleportRequests = null ;
305
+ _teleportProtections = null ;
306
+ _cooldowns = null ;
307
+ _playersLastCombat = null ;
308
+ _plugin = null ;
309
+ _configuration = null ;
247
310
DamageTool . onPlayerAllowedToDamagePlayer -= OnPlayerAllowedToDamagePlayer ;
248
311
U . Events . OnPlayerDisconnected -= OnLeave ;
249
312
}
0 commit comments