@@ -18,15 +18,18 @@ public class TeleportsManager : IDisposable
1818 private readonly Configuration _configuration ;
1919 private readonly List < Tuple < UnturnedPlayer , UnturnedPlayer > > _teleportRequests ;
2020 private readonly Dictionary < UnturnedPlayer , DateTime > _cooldowns ;
21-
21+ private readonly Dictionary < UnturnedPlayer , DateTime > _teleportProtections ;
22+
2223 public TeleportsManager ( Plugin plugin )
2324 {
2425 _teleportRequests = new List < Tuple < UnturnedPlayer , UnturnedPlayer > > ( ) ;
26+ _teleportProtections = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
2527 _cooldowns = new Dictionary < UnturnedPlayer , DateTime > ( ) ;
2628 _plugin = plugin ;
2729 _configuration = plugin . Configuration . Instance ;
2830 _messageColor = plugin . MessageColor ;
2931 U . Events . OnPlayerDisconnected += OnLeave ;
32+ DamageTool . onPlayerAllowedToDamagePlayer += OnPlayerAllowedToDamagePlayer ;
3033 }
3134
3235 public void Send ( UnturnedPlayer sender , UnturnedPlayer target )
@@ -103,7 +106,9 @@ public void Accept(UnturnedPlayer target)
103106 if ( _configuration . TeleportCost . Enabled )
104107 _plugin . EconomyProvider . IncrementBalance ( sender . Id , - _configuration . TeleportCost . TpaCost ) ;
105108
106- sender . Player . teleportToLocationUnsafe ( sender . Position , sender . Player . look . yaw ) ;
109+ UpdateTeleportProtection ( sender ) ;
110+
111+ sender . Player . teleportToLocationUnsafe ( target . Position , target . Player . look . yaw ) ;
107112 _teleportRequests . Remove ( request ) ;
108113
109114 UnturnedChat . Say ( sender , _plugin . Translate ( "TpaCommand:Accept:Teleported" , target . DisplayName ) , _messageColor , true ) ;
@@ -170,6 +175,39 @@ private void OnLeave(UnturnedPlayer player)
170175
171176 if ( cooldown != DateTime . MinValue )
172177 _cooldowns . Remove ( player ) ;
178+
179+ var proctection = GetTeleportProtection ( player ) ;
180+
181+ if ( proctection != DateTime . MinValue )
182+ _teleportProtections . Remove ( player ) ;
183+ }
184+
185+ private void OnPlayerAllowedToDamagePlayer ( Player nativeInstigator , Player nativeVictim , ref bool isAllowed )
186+ {
187+ if ( ! _configuration . TeleportProtection )
188+ return ;
189+
190+ var victim = UnturnedPlayer . FromPlayer ( nativeVictim ) ;
191+ var instigator = UnturnedPlayer . FromPlayer ( nativeInstigator ) ;
192+
193+ if ( _teleportProtections . ContainsKey ( victim ) )
194+ {
195+ var teleportProtection = GetTeleportProtection ( victim ) ;
196+
197+ var teleportProtectionTime = teleportProtection . AddSeconds ( _configuration . TeleportProtectionTime ) ;
198+
199+ if ( teleportProtection > DateTime . Now )
200+ {
201+ isAllowed = false ;
202+ var waitTime = ( teleportProtectionTime - DateTime . Now ) . TotalSeconds ;
203+
204+ UnturnedChat . Say ( instigator , _plugin . Translate ( "TpaProtection" , Math . Round ( waitTime ) , victim . DisplayName ) , true ) ;
205+ }
206+ else
207+ {
208+ _teleportProtections . Remove ( victim ) ;
209+ }
210+ }
173211 }
174212
175213 private void UpdateCooldown ( UnturnedPlayer player )
@@ -180,6 +218,22 @@ private void UpdateCooldown(UnturnedPlayer player)
180218 _cooldowns . Add ( player , DateTime . Now ) ;
181219 }
182220
221+ private void UpdateTeleportProtection ( UnturnedPlayer player )
222+ {
223+ if ( _teleportProtections . ContainsKey ( player ) )
224+ _teleportProtections [ player ] = DateTime . Now ;
225+ else
226+ _cooldowns . Add ( player , DateTime . Now ) ;
227+ }
228+
229+ private DateTime GetTeleportProtection ( UnturnedPlayer player )
230+ {
231+ if ( _teleportProtections . ContainsKey ( player ) )
232+ return _teleportProtections [ player ] ;
233+
234+ return DateTime . MinValue ;
235+ }
236+
183237 private DateTime GetCooldown ( UnturnedPlayer player )
184238 {
185239 if ( _cooldowns . ContainsKey ( player ) )
@@ -190,6 +244,7 @@ private DateTime GetCooldown(UnturnedPlayer player)
190244
191245 public void Dispose ( )
192246 {
247+ DamageTool . onPlayerAllowedToDamagePlayer -= OnPlayerAllowedToDamagePlayer ;
193248 U . Events . OnPlayerDisconnected -= OnLeave ;
194249 }
195250 }
0 commit comments