-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatPrefix.cs
More file actions
440 lines (375 loc) · 16.3 KB
/
ChatPrefix.cs
File metadata and controls
440 lines (375 loc) · 16.3 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System;
using System.Collections.Generic;
using System.Linq;
using ConVar;
using Facepunch;
using Facepunch.Math;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using UnityEngine;
// ToDo: better betterchat support
namespace Oxide.Plugins
{
[Info("Chat Prefix", "Gonzi", "1.2.1")]
[Description("Chat Prefix per Permission")]
public class ChatPrefix : RustPlugin
{
#region Fields
[PluginReference] Plugin ColouredChat;
[PluginReference] Plugin Quests;
[PluginReference] Plugin BetterChat;
private Dictionary<ulong, PlayerPrefix> playerPrefixData = new Dictionary<ulong, PlayerPrefix>();
private class PlayerPrefix
{
public string Prefix { get; set; }
public string Color { get; set; }
public bool active { get; set; }
}
private class PrefixConfig
{
public bool Disabled { get; set; }
public int Priority { get; set; }
public string Prefix { get; set; }
public string Color { get; set; }
public string Permission { get; set; }
public string GroupName { get; set; }
}
private static ConfigData config;
private class ConfigData
{
public bool debug = false;
[JsonProperty("Use Groupname instead of Permission")]
public bool useGroup = false;
public Dictionary<string, PrefixConfig> Prefixes { get; set; }
}
#endregion Fields
#region Configuration
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<ConfigData>();
if (config == null)
{
LoadDefaultConfig();
SaveConfig();
}
}
catch (Exception e)
{
Puts("{0} Exception caught.", e);
PrintError("The configuration file is corrupted! Using Default Config!");
LoadDefaultConfig();
}
RegPerm("reload");
}
protected override void LoadDefaultConfig() => config = DefaultConfig();
protected override void SaveConfig() => Config.WriteObject(config);
private object GetConfig(string menu, string datavalue, object defaultValue)
{
var data = Config[menu] as Dictionary<string, object>;
if (data == null)
{
data = new Dictionary<string, object>();
Config[menu] = data;
}
object value;
if (!data.TryGetValue(datavalue, out value))
{
value = defaultValue;
data[datavalue] = value;
}
return value;
}
ConfigData DefaultConfig()
{
var DefaultConfig = new ConfigData
{
Prefixes = new Dictionary<string, PrefixConfig>
{
{
"admin", new PrefixConfig
{
Disabled = false,
Priority = 1,
Prefix = "[ADMIN]",
Color = "#FF0000",
Permission = "admin",
GroupName = "admin"
}
},
{
"mod", new PrefixConfig
{
Disabled = false,
Priority = 2,
Prefix = "[MOD]",
Color = "#0000FF",
Permission = "mod",
GroupName = "mod"
}
},
{
"vip", new PrefixConfig
{
Disabled = false,
Priority = 3,
Prefix = "[VIP]",
Color = "#ffb400",
Permission = "vip",
GroupName = "vip"
}
}
}
};
return DefaultConfig;
}
#endregion Configuration
#region Hooks
// ColouredChat integration
private string ColChat_GetColName(IPlayer player) => Interface.Oxide.CallHook("API_GetColouredName", player) as string;
private string ColChat_GetColMessage(IPlayer player, string message) => Interface.Oxide.CallHook("API_GetColouredMessage", player, message) as string;
// end ColouredChat
private void OnPlayerConnected(BasePlayer player) => ReloadPrefix(player);
private void OnUserPermissionGranted(string id, string permName)
{
if (config.debug) Puts($"Player '{id}' granted permission: {permName}");
playerPrefixData.Clear();
foreach (BasePlayer bplayer in BasePlayer.activePlayerList)
{
ReloadPrefix(bplayer);
}
return;
}
private void OnUserPermissionRevoked(string id, string permName)
{
if (config.debug) Puts($"Player '{id}' revoked permission: {permName}");
playerPrefixData.Clear();
foreach (BasePlayer bplayer in BasePlayer.activePlayerList)
{
ReloadPrefix(bplayer);
}
}
private void OnGroupPermissionGranted(string name, string perm)
{
if (config.debug) Puts($"Group '{name}' granted permission: {perm}");
playerPrefixData.Clear();
foreach (BasePlayer bplayer in BasePlayer.activePlayerList)
{
ReloadPrefix(bplayer);
}
}
private void OnGroupPermissionRevoked(string name, string perm)
{
if (config.debug) Puts($"Group '{name}' revoked permission: {perm}");
playerPrefixData.Clear();
foreach (BasePlayer bplayer in BasePlayer.activePlayerList)
{
ReloadPrefix(bplayer);
}
}
private void OnUserGroupAdded(string id, string groupName)
{
if (config.debug) Puts($"Player '{id}' added to group: {groupName}");
var p = BasePlayer.activePlayerList.Where(pl => pl.UserIDString == id && pl.IsValid() == true).FirstOrDefault();
if (!p) return;
ReloadPrefix(p);
}
private void OnUserGroupRemoved(string id, string groupName)
{
if (config.debug) Puts($"Player '{id}' removed from group: {groupName}");
var p = BasePlayer.activePlayerList.Where(pl => pl.UserIDString == id && pl.IsValid() == true).FirstOrDefault();
if (!p) return;
ReloadPrefix(p);
}
private object OnPlayerChat(BasePlayer player, string message, Chat.ChatChannel channel)
{
if (config.debug) Puts("OnPlayerChat");
// Plugin Quests - prevents to send input to globalchat while quest creation is active
if (QuestsActv() && Quests.Call<bool>("API_GetNotChatOutput", player))
{
Puts("Quests Plugin API requests return of msg! - no chat output!");
return true;
}
PlayerPrefix pP;
playerPrefixData.TryGetValue(player.userID, out pP);
if (pP == null) return null;
if (ColouredChatActv())
{
var name = Interface.Oxide.CallHook("API_GetColouredName", player.IPlayer) as string;
var cmsg = ColChat_GetColMessage(player.IPlayer, message);
Puts(Interface.Oxide.CallHook("API_GetColouredMessage", player.IPlayer, message) as string);
if (pP.active) SendChatMessage(player, "<color=" + pP.Color + ">", pP.Prefix, name + "</color>" + ":", cmsg, channel);
else SendChatMessage(player, null, null, name + ":", cmsg, channel);
return true;
}
else
{
if (pP.active) SendChatMessage(player, "<color=" + pP.Color + ">", pP.Prefix, player.displayName + "</color>" + ":", message, channel);
else return null;
}
return true;
}
// cancel message to prevent duplicate messages - // ToDo: Add better support (Username etc.)
private object OnBetterChat(Dictionary<string, object> dict)
{
var player = (dict["Player"] as IPlayer).Object as BasePlayer;
if (player == null) return null;
dict["CancelOption"] = 2;
return dict;
}
// always return to prevent double messages
private object OnColouredChat(Dictionary<string, object> dict)
{
return false;
}
#endregion Hooks
#region Util
private object SendChatMessage(BasePlayer player, string pColor, string prefix, string displayname, string message, Chat.ChatChannel channel)
{
// if (Chat.serverlog && player.IsValid())
// {
// object[] logMsgArr = new object[] { ConsoleColor.DarkYellow, null, null, null };
// logMsgArr[1] = string.Concat(new object[] { "[", channel, "] ", player.displayName.EscapeRichText(), ": " });
// logMsgArr[2] = ConsoleColor.DarkGreen;
// logMsgArr[3] = new System.Text.RegularExpressions.Regex("<[^>]*>").Replace(string.Join(" ", message), "");
// ServerConsole.PrintColoured(logMsgArr);
// }
RCon.Broadcast(RCon.LogType.Chat, new Chat.ChatEntry
{
Channel = channel,
Message = new System.Text.RegularExpressions.Regex("<[^>]*>").Replace(string.Join(" ", message), ""),
UserId = player.IPlayer.Id,
Username = player.displayName,
Color = pColor,
Time = Epoch.Current
});
switch ((int)channel)
{
// global chat
case 0:
if (config.debug) Puts("default / global chat");
var gMsg = ArrayPool.Get(3);
gMsg[0] = (int)channel;
gMsg[1] = player.UserIDString;
foreach (BasePlayer p in BasePlayer.activePlayerList.Where(p => p.IsValid() == true))
{
gMsg[2] = $"{pColor}{Lang(prefix, p.UserIDString)} {displayname} {message}";
p.SendConsoleCommand("chat.add", gMsg);
if (config.debug) Puts("sended GLOBAL message (" + message + ") to " + p.displayName);
}
ArrayPool.Free(gMsg);
break;
// team channel
case 1:
if (config.debug) Puts("team chat");
var tMsg = ArrayPool.Get(3);
tMsg[0] = (int)channel;
tMsg[1] = player.UserIDString;
foreach (BasePlayer p in BasePlayer.activePlayerList.Where(p => p.Team != null && player.Team != null && p.Team.teamID == player.Team.teamID && p.IsValid() == true))
{
tMsg[2] = $"{pColor}{Lang(prefix, p.UserIDString)} {displayname} {message}";
p.SendConsoleCommand("chat.add", tMsg);
if (config.debug) Puts("sended GLOBAL message (" + message + ") to " + p.displayName);
}
ArrayPool.Free(tMsg);
break;
default:
break;
}
return true;
}
private void RegPerm(string name)
{
if (permission.PermissionExists("chatprefix." + name, this)) return;
permission.RegisterPermission("chatprefix." + name, this);
if (config.debug) Puts("Registered permission: chatprefix." + name);
return;
}
private object ReloadPrefix(BasePlayer player)
{
if (config.debug) Puts("ReloadPrefix for " + player.displayName);
if (!player.IsValid()) return false;
playerPrefixData.Remove(player.userID);
bool foundPrefixForPly = false;
foreach (var p in config.Prefixes.OrderBy(x => x.Value.Priority))
{
if (foundPrefixForPly) return true;
if (permission.UserHasPermission(player.UserIDString, p.Value.Permission) && !config.useGroup && !p.Value.Disabled || permission.UserHasGroup(player.UserIDString, p.Value.GroupName) && config.useGroup && !p.Value.Disabled)
{
foundPrefixForPly = true;
if (config.debug) Puts("Found Prefix for " + player.displayName + " Prefix: " + p.Value.Prefix + " Prefixcolor" + p.Value.Color + " Permission:" + p.Value.Permission + " GroupName:" + p.Value.GroupName);
playerPrefixData.Add(player.userID, new PlayerPrefix { Prefix = p.Value.Prefix, Color = p.Value.Color, active = true });
}
}
if (!foundPrefixForPly) playerPrefixData.Add(player.userID, new PlayerPrefix { Prefix = null, Color = null, active = false });
return true;
}
private bool QuestsActv() => (Quests != null && Quests.IsLoaded);
private bool ColouredChatActv() => (ColouredChat != null && ColouredChat.IsLoaded);
private bool BetterChatActv() => (BetterChat != null && BetterChat.IsLoaded);
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
var messages = new Dictionary<string, string>();
foreach (var pCfg in config.Prefixes.OrderBy(x => x.Value.Priority))
{
messages.Add(pCfg.Value.Prefix, pCfg.Value.Prefix);
RegPerm(pCfg.Value.Permission);
}
messages.Add("xPrefixesReloaded", "Prefix for {0} players was reloaded!");
lang.RegisterMessages(messages, this, "en");
playerPrefixData.Clear();
foreach (BasePlayer bplayer in BasePlayer.activePlayerList)
{
ReloadPrefix(bplayer);
}
}
private string Lang(string key, string id = null, params object[] args)
{
if (key == null) return null;
return string.Format(lang.GetMessage(key, this, id), args);
}
#endregion
#region API
private string API_GetPrefixedMessageForPlayer(BasePlayer player, string messageText)
{
PlayerPrefix pP;
playerPrefixData.TryGetValue(player.userID, out pP);
if (pP == null || !pP.active) return null;
if (ColouredChat)
{
if (pP.active) return "<color=" + pP.Color + ">" + pP.Prefix + " " + ColChat_GetColName(player.IPlayer) + "</color>:" + ColChat_GetColMessage(player.IPlayer, messageText);
else return null;
}
else
{
if (pP.active) return "<color=" + pP.Color + ">" + pP.Prefix + " " + player.displayName + "</color>:" + messageText;
else return null;
}
}
private bool API_ReloadPrefixForPlayer(BasePlayer player)
{
if (player != null)
{
ReloadPrefix(player);
return true;
}
else return false;
}
private bool API_ReloadPrefixForAllPlayers()
{
playerPrefixData.Clear();
foreach (BasePlayer bplayer in BasePlayer.activePlayerList)
{
ReloadPrefix(bplayer);
}
return true;
}
#endregion
}
}