forked from Calytic/oxideplugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAFK.cs
More file actions
166 lines (129 loc) · 5.66 KB
/
AFK.cs
File metadata and controls
166 lines (129 loc) · 5.66 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
TODO:
- Add automatic admin exclusion
*/
using System;
using System.Collections.Generic;
using Oxide.Core.Libraries.Covalence;
namespace Oxide.Plugins
{
[Info("AFK", "Wulf/lukespragg", "1.0.0")]
[Description("Kicks players that are AFK (away from keyboard) for too long.")]
class AFK : CovalencePlugin
{
// Do NOT edit this file, instead edit AFK.json in oxide/config and AFK.en.json in the oxide/lang directory,
// or create a new language file for another language using the 'en' file as a default.
#region Configuration
int AfkLimitMinutes => GetConfig("AfkLimitMinutes", 10);
bool KickAfkPlayers => GetConfig("KickAfkPlayers", true);
//bool WarnBeforeKick => GetConfig("WarnBeforeKick", true);
protected override void LoadDefaultConfig()
{
Config["AfkLimitMinutes"] = AfkLimitMinutes;
Config["KickAfkPlayers"] = KickAfkPlayers;
//Config["WarnBeforeKick"] = WarnBeforeKick;
SaveConfig();
}
#endregion
#region Localization
void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"KickedForAfk", "You were kicked for being AFK for {0} minutes"},
//{"NoLongerAfk", "You are no longer AFK"},
//{"YouWentAfk", "You went AFK"}
}, this);
}
#endregion
#region Initialization
void Loaded()
{
#if !HURTWORLD && !REIGNOFKINGS && !RUST && !RUSTLEGACY
throw new NotSupportedException("This plugin does not support this game");
#endif
LoadDefaultConfig();
LoadDefaultMessages();
permission.RegisterPermission("afk.excluded", this);
}
void OnServerInitialized()
{
foreach (var player in players.Online)
{
lastPosition[player.BasePlayer.UniqueID] = player.Character.GetPosition();
AfkCheck(player.BasePlayer.UniqueID);
}
}
#endregion
#region AFK Checking
readonly Hash<string, GenericPosition> lastPosition = new Hash<string, GenericPosition>();
readonly Dictionary<string, Timer> afkTimer = new Dictionary<string, Timer>();
void AfkCheck(string userId)
{
if (HasPermission(userId, "afk.excluded")) return;
afkTimer.Add(userId, timer.Repeat(AfkLimitMinutes*60, 0, () =>
{
var player = players.GetOnlinePlayer(userId);
if (!IsPlayerAfk(player)) return;
//player.Message(GetMessage("YouWentAfk", userId));
if (KickAfkPlayers)
{
// TODO: Send timed message/warning to player before kick
player.Kick(string.Format(GetMessage("KickedForAfk", userId), AfkLimitMinutes));
}
}));
}
void ResetPlayer(string userId)
{
if (afkTimer.ContainsKey(userId))
{
afkTimer[userId].Destroy();
afkTimer.Remove(userId);
}
if (lastPosition.ContainsKey(userId)) lastPosition.Remove(userId);
}
bool IsPlayerAfk(ILivePlayer player)
{
var position = player.Character.GetPosition();
if (lastPosition[player.BasePlayer.UniqueID].Equals(position)) return true;
lastPosition[player.BasePlayer.UniqueID] = position;
return false;
}
void Unload()
{
foreach (var player in players.Online) ResetPlayer(player.BasePlayer.UniqueID);
}
#endregion
#region Game Hooks
#if HURTWORLD
void OnPlayerInit(PlayerSession session) => AfkCheck(session.SteamId.ToString());
void OnPlayerDisconnected(PlayerSession session) => ResetPlayer(session.SteamId.ToString());
bool IsPlayerAfk(PlayerSession session) => IsPlayerAfk(players.GetOnlinePlayer(session.SteamId.ToString()));
#endif
#if REIGNOFKINGS
void OnPlayerSpawn(CodeHatch.Networking.Events.Players.PlayerFirstSpawnEvent evt) => AfkCheck(evt.PlayerId.ToString());
void OnPlayerDisconnected(CodeHatch.Engine.Networking.Player player) => ResetPlayer(player.Id.ToString());
bool IsPlayerAfk(CodeHatch.Engine.Networking.Player player) => IsPlayerAfk(players.GetOnlinePlayer(player.Id.ToString()));
#endif
#if RUST
void OnPlayerInit(BasePlayer player) => AfkCheck(player.UserIDString());
void OnPlayerDisconnected(BasePlayer player) => ResetPlayer(player.UserIDString);
bool IsPlayerAfk(BasePlayer player) => IsPlayerAfk(players.GetOnlinePlayer(player.UserIDString));
#endif
#if RUSTLEGACY
void OnPlayerSpawn(PlayerClient client) => AfkCheck(client.netUser.userID.ToString());
void OnPlayerDisconnected(uLink.NetworkPlayer player) => ResetPlayer(player.GetLocalData<NetUser>()?.userID.ToString());
bool IsPlayerAfk(NetUser netUser) => IsPlayerAfk(players.GetOnlinePlayer(netUser.userID.ToString()));
#endif
#endregion
#region Helper Methods
T GetConfig<T>(string name, T defaultValue)
{
if (Config[name] == null) return defaultValue;
return (T)Convert.ChangeType(Config[name], typeof(T));
}
string GetMessage(string key, string userId = null) => lang.GetMessage(key, this, userId);
bool HasPermission(string userId, string perm) => permission.UserHasPermission(userId, perm);
#endregion
}
}