-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopCommand.cs
More file actions
259 lines (214 loc) · 9.32 KB
/
PopCommand.cs
File metadata and controls
259 lines (214 loc) · 9.32 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ConVar;
namespace Oxide.Plugins
{
[Info("PopCommand", "HandyS11", "1.0.2")]
[Description("Displays the population information of your server")]
public class PopCommand : RustPlugin
{
#region Configuration
private Configuration _configuration;
private sealed class Configuration
{
[JsonProperty(PropertyName = "Use permission for lambda players")]
public bool UsePermissionForPlayers { get; set; }
[JsonProperty(PropertyName = "Broadcast to every player on /pop")]
public bool DoBroadcast { get; set; }
[JsonProperty(PropertyName = "Chat default avatar")]
public ulong ChatAvatar { get; set; }
[JsonProperty(PropertyName = "Display Options")]
public DisplayOptions DisplayOption { get; set; }
}
public class DisplayOptions
{
[JsonProperty(PropertyName = "Show player count")]
public bool ShowPlayerCount { get; set; }
[JsonProperty(PropertyName = "Show server slots")]
public bool ShowServerSlots { get; set; }
[JsonProperty(PropertyName = "Show sleepers")]
public bool ShowSleepers { get; set; }
[JsonProperty(PropertyName = "Show joining players")]
public bool ShowJoiningPlayers { get; set; }
[JsonProperty(PropertyName = "Show players in queue")]
public bool ShowPlayersInQueue { get; set; }
}
private Configuration GetDefaultConfig()
{
return new Configuration
{
UsePermissionForPlayers = false,
DoBroadcast = false,
ChatAvatar = 0,
DisplayOption = new DisplayOptions
{
ShowPlayerCount = true,
ShowServerSlots = true,
ShowSleepers = false,
ShowJoiningPlayers = false,
ShowPlayersInQueue = false,
},
};
}
protected override void LoadConfig()
{
base.LoadConfig();
_configuration = Config.ReadObject<Configuration>();
SaveConfig();
}
protected override void LoadDefaultConfig()
{
_configuration = GetDefaultConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(_configuration, true);
}
#endregion
#region Permissions
private static class Permission
{
public const string PopCommand = "popcommand.pop";
public const string PopCommandAdmin = "popcommand.apop";
}
# endregion
#region Hooks
private void Init()
{
permission.RegisterPermission(Permission.PopCommand, this);
permission.RegisterPermission(Permission.PopCommandAdmin, this);
}
private void OnPlayerChat(BasePlayer player, string message, Chat.ChatChannel channel)
{
switch (message)
{
case "!pop":
PopPlayer(player, "pop");
break;
case "!apop":
PopAdmin(player, "apop");
break;
}
}
#endregion
#region Functions
private void SendChatMessage(BasePlayer player, string message)
{
Player.Message(player, message, _configuration.ChatAvatar);
}
private void SendMessageToAll(string message)
{
Server.Broadcast(message, _configuration.ChatAvatar);
}
private bool HasPermission(BasePlayer player, string permissionName)
{
return permission.UserHasPermission(player.UserIDString, permissionName);
}
private IEnumerable<int> GetData()
{
var data = new List<int>();
if (_configuration.DisplayOption.ShowPlayerCount)
data.Add(BasePlayer.activePlayerList.Count);
if (_configuration.DisplayOption.ShowServerSlots)
data.Add(ConVar.Server.maxplayers);
if (_configuration.DisplayOption.ShowSleepers)
data.Add(BasePlayer.sleepingPlayerList.Count);
if (_configuration.DisplayOption.ShowJoiningPlayers)
data.Add(ServerMgr.Instance.connectionQueue.joining.Count);
if (_configuration.DisplayOption.ShowPlayersInQueue)
data.Add(ServerMgr.Instance.connectionQueue.queue.Count);
return data;
}
private bool VerifyPlaceholders(string template, int expectedCount)
{
var actualCount = Regex.Matches(template, @"\{\d+\}").Count;
return actualCount <= expectedCount;
}
#endregion
#region Command
private static class Command
{
public const string PopPlayer = "pop";
public const string PopAdmin = "apop";
}
[ChatCommand(Command.PopPlayer)]
private void PopPlayer(BasePlayer player, string command, string[] args = null)
{
if (_configuration.UsePermissionForPlayers && !HasPermission(player, Permission.PopCommand))
{
SendChatMessage(player, GetMessage(MessageKey.PopPermissionDeny, player.UserIDString));
return;
}
if (_configuration.DoBroadcast)
{
SendMessageToAll(GetMessage(MessageKey.PopMessage, player.UserIDString, GetData().Cast<object>().ToArray()));
return;
}
SendChatMessage(player, GetMessage(MessageKey.PopMessage, player.UserIDString, GetData().Cast<object>().ToArray()));
}
[ChatCommand(Command.PopAdmin)]
private void PopAdmin(BasePlayer player, string command, string[] args = null)
{
if (!player.IsAdmin || !HasPermission(player, Permission.PopCommandAdmin))
{
SendChatMessage(player, GetMessage(MessageKey.PopAdminPermissionDeny, player.UserIDString));
return;
}
SendChatMessage(player, GetMessage(MessageKey.PopMessageAdmin, player.UserIDString,
BasePlayer.activePlayerList.Count,
ConVar.Server.maxplayers,
BasePlayer.sleepingPlayerList.Count,
ServerMgr.Instance.connectionQueue.joining.Count,
ServerMgr.Instance.connectionQueue.queue.Count
));
}
#endregion
#region Localization
private static class MessageKey
{
public const string PopMessage = "PopCommand.ChatMessage";
public const string PopMessageAdmin = "PopCommand.AdminMessage";
public const string PopPermissionDeny = "PopCommand.PermissionDeny";
public const string PopAdminPermissionDeny = "PopCommand.AdminPermissionDeny";
public const string PopError = "PopCommand.Error";
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
[MessageKey.PopMessage] = "Players online: <color=orange>{0}</color>/<color=orange>{1}</color>",
[MessageKey.PopMessageAdmin] = "Players online: <color=orange>{0}</color>/<color=orange>{1}</color> | Sleeping: <color=orange>{2}</color> | Joining: <color=orange>{3}</color> | Queued: <color=orange>{4}</color>",
[MessageKey.PopPermissionDeny] = "You are not allowed to run this command!",
[MessageKey.PopAdminPermissionDeny] = "Only administrators can run this command!",
[MessageKey.PopError] = "The config/lang file contains some errors!",
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
[MessageKey.PopMessage] = "Joueurs en ligne : <color=green>{0}</color>/<color=red>{1}</color>",
[MessageKey.PopMessageAdmin] = "Joueurs en ligne : <color=orange>{0}</color>/<color=orange>{1}</color> | Endormi : <color=orange>{2}</color> | En train de rejoindre : <color=orange>{3}</color> | Dans la queue : <color=orange>{4}</color>",
[MessageKey.PopPermissionDeny] = "Vous n'êtes pas autorisé à utiliser cette commande !",
[MessageKey.PopAdminPermissionDeny] = "Seul les administrateurs peuvent utiliser cette commande !",
[MessageKey.PopError] = "Le fichier de configuration et/ou de traduction contient des erreurs !",
}, this, "fr");
}
private string GetMessage(string messageKey, string playerId = null, params object[] data)
{
try
{
var template = lang.GetMessage(messageKey, this, playerId);
if (VerifyPlaceholders(template, data.Length)) return string.Format(template, data);
Puts("Wrong number of params compared to the string");
return lang.GetMessage(MessageKey.PopError, this, playerId);
}
catch (Exception exception)
{
PrintError(exception.ToString());
throw;
}
}
#endregion
}
}