Skip to content

Commit 2bb257d

Browse files
committed
Add teleport protection and fix teleporting to the sender position
1 parent 5c2de6c commit 2bb257d

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

Feli.RocketMod.Teleporting/Configuration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class Configuration : IRocketPluginConfiguration
99
public float TeleportDelay { get; set; }
1010
public double TeleportCooldown { get; set; }
1111
public bool CancelWhenMove { get; set; }
12+
public bool TeleportProtection { get; set; }
13+
public double TeleportProtectionTime { get; set; }
1214
public TeleportCost TeleportCost { get; set; }
1315

1416
public void LoadDefaults()
@@ -17,6 +19,8 @@ public void LoadDefaults()
1719
TeleportDelay = 5;
1820
TeleportCooldown = 60;
1921
CancelWhenMove = false;
22+
TeleportProtection = true;
23+
TeleportProtectionTime = 5;
2024
TeleportCost = new TeleportCost()
2125
{
2226
Enabled = false,

Feli.RocketMod.Teleporting/Plugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected override void Unload()
5252
{"TpaCommand:Cancel:NotRequests", "There are no tpa requests to cancel"},
5353
{"TpaCommand:Cancel:Other", "{0} has just cancelled the tpa request"},
5454
{"TpaCommand:Cancel:Success", "Successfully canceled the tpa with {0}"},
55+
{"TpaProtection", "You must wait {0} seconds to damage {1} he is on tpa protection"},
5556
{"TpaValidation:Car:Other", "The teleport was cancelled because {0} is on a car"},
5657
{"TpaValidation:Car:Self", "The teleport was cancelled because you are on a car"},
5758
{"TpaValidation:Leave", "The teleport was cancelled because {0} left the server"},

Feli.RocketMod.Teleporting/TeleportsManager.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)