forked from KSean12/AntiSuicide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntiSuicide.cs
121 lines (107 loc) · 4.3 KB
/
AntiSuicide.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Rocket.API;
using Rocket.API.Collections;
using Rocket.Core;
using Logger = Rocket.Core.Logging.Logger;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Events;
using Rocket.Unturned.Player;
using Rocket.Unturned.Plugins;
using SDG;
using SDG.Unturned;
using Steamworks;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
namespace AntiSuicide
{
class AntiSuicide : RocketPlugin<AntiSuicideConfiguration>
{
public static Dictionary<string, SpawnLocation> SpawnLocations = new Dictionary<string, SpawnLocation>();
public static Dictionary<string, byte> SuicideCount = new Dictionary<string, byte>();
public static AntiSuicide Instance;
protected override void Load()
{
Instance = this;
if (!Configuration.Instance.Enabled) return;
UnturnedPlayerEvents.OnPlayerRevive += OnPlayerRevive;
UnturnedPlayerEvents.OnPlayerDeath += OnPlayerDeath;
}
private void OnPlayerRevive(UnturnedPlayer player , Vector3 position, byte angle)
{
if (player.IsAdmin || R.Permissions.HasPermission(player , "anti-suicide.imune")) return;
if (!validateKick(player)) validateRelocation(player, position, angle);
}
private void OnPlayerDeath(UnturnedPlayer player, EDeathCause cause, ELimb limb, CSteamID murderer)
{
if (player.IsAdmin || player.HasPermission("anti-suicide.imune")) return;
if (cause != EDeathCause.SUICIDE && cause != EDeathCause.BREATH)
{
if (SpawnLocations.ContainsKey(player.ToString()))
SpawnLocations.Remove(player.ToString());
if (SuicideCount.ContainsKey(player.ToString()))
SuicideCount.Remove(player.ToString());
}
}
private Timer timer;
private void validateRelocation(UnturnedPlayer player, Vector3 position, float angle, bool generate = true)
{
if (SpawnLocations.ContainsKey(player.ToString()))
{
timer = new Timer(obj =>
{
teleport(player);
timer.Dispose();
timer = null;
}, null, 500, Timeout.Infinite);
}
else
{
if (generate) SpawnLocations.Add(player.ToString(), new SpawnLocation(position, angle));
}
}
private bool validateKick(UnturnedPlayer player)
{
if (SuicideCount.ContainsKey(player.ToString()))
SuicideCount[player.ToString()] += 1;
else
SuicideCount.Add(player.ToString(), 1);
if (SuicideCount[player.ToString()] >= Configuration.Instance.MaxSuicidesBeforeKick)
{
player.Kick(Translate("anti_suicide_kick_reason"));
log(Translate("anti_suicide_kick_log", player.CharacterName));
if (Configuration.Instance.ResetSuicideCountAfterKick && SuicideCount.ContainsKey(player.ToString()))
SuicideCount.Remove(player.ToString());
return true;
}
return false;
}
private void teleport(UnturnedPlayer player)
{
if (SpawnLocations.ContainsKey(player.ToString()))
{
UnturnedChat.Say(player, Translate("anti_suicide_warning"));
player.Teleport(SpawnLocations[player.ToString()].Position, SpawnLocations[player.ToString()].Angle);
}
}
private void log(string message)
{
Logger.LogWarning("#################### ");
Logger.LogWarning("## ANTI-SUICIDE ## : " + message);
Logger.LogWarning("#################### ");
}
// Translations
public override TranslationList DefaultTranslations
{
get
{
return new TranslationList(){
{"anti_suicide_kick_log","{0} was kicked for multiple suicides!"},
{"anti_suicide_kick_reason","Anti-Suicide - Please stop that!"},
{"anti_suicide_warning","Anti-Suicide - Teleporting to last Location!"}
};
}
}
}
}