-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrueWelcomer.cs
260 lines (202 loc) · 9.95 KB
/
TrueWelcomer.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
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
using Oxide.Core;
using UnityEngine;
using Newtonsoft.Json;
using Oxide.Core.Configuration;
using System.Collections.Generic;
namespace Oxide.Plugins {
[Info("True Welcomer", "ItzNxthaniel", "1.1.2")]
[Description("This plugin makes it easy to welcome new users and welcome back users rejoining! Also supports Disconnect Messages.")]
public class TrueWelcomer : RustPlugin {
#region Vars/Fields
private static List<string> _onlinePlayers = new List<string>();
#endregion
#region Config
private Configuration _config;
protected override void LoadConfig() {
base.LoadConfig();
_config = Config.ReadObject<Configuration>();
SaveConfig();
}
protected override void SaveConfig() =>
Config.WriteObject(_config);
protected override void LoadDefaultConfig() {
_config = new Configuration {
DebugMode = false,
ShowJoinMsgs = true,
ShowWelcomeMessages = true, ShowLeaveMsgs = true,
ClearOnWipe = true,
SteamIconID = 0,
HidePlayersWithAuthlevel = false,
AuthLevelToHide = 0,
HidePlayersWithPermission = false
};
SaveConfig();
}
private enum AuthLevelEnum {
Both = 0,
AuthLevel1 = 1,
AuthLevel2 = 2
}
private class Configuration {
[JsonProperty("Debug Mode")]
internal bool DebugMode;
[JsonProperty("Show Join Messages")]
internal bool ShowJoinMsgs;
[JsonProperty("Show Welcome Messages")]
internal bool ShowWelcomeMessages;
[JsonProperty("Show Leave Messages")]
internal bool ShowLeaveMsgs;
[JsonProperty("Clears the Data List on wipe")]
internal bool ClearOnWipe;
[JsonProperty("Steam User Icon ID")]
internal ulong SteamIconID;
[JsonProperty("Hide Players with AuthLevel")]
internal bool HidePlayersWithAuthlevel;
[JsonProperty("AuthLevel to Hide. 0 - Both, 1 - AuthLevel1, 2 - AuthLevel2 ")]
internal AuthLevelEnum AuthLevelToHide;
[JsonProperty("Hide Players With Permission")]
internal bool HidePlayersWithPermission;
}
#endregion
#region Language
protected override void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string> {
["OnWelcome"] = "Welcome, <color=#ff7675>{0}</color>, to the server!",
["OnJoin"] = "Welcome back, <color=#ff7675>{0}</color>, to the server!",
["OnLeave"] = "Goodbye, <color=#ff7675>{0}</color>!",
["NoPermission"] = "You do not have permission to run this command!",
["NowHiding"] = "Users will no longer be alerted when you join or leave the server.",
["NowShowing"] = "Users will now be alerted when you join or leave the server.",
["ServerConfigAlert"] = "Due to the Server's Config, your preference will be ignored."
},
this);
}
private void Broadcast(BasePlayer player, string messageKey, params object[] args) {
if (player == null) return;
var message = GetMessage(messageKey, player.UserIDString, args);
if (_config.DebugMode) Puts(message);
Server.Broadcast(message, null, _config.SteamIconID);
}
private string GetMessage(string key, string id, params object[] args) =>
string.Format(lang.GetMessage(key, this, id), args);
#endregion
#region Data
private DynamicConfigFile _cachedPlayers = Interface.Oxide.DataFileSystem.GetDatafile("TrueWelcomer");
#endregion
#region Hooks
private void Init() {
permission.RegisterPermission("truewelcomer.admin", this);
permission.RegisterPermission("truewelcomer.canSetPreference", this);
permission.RegisterPermission("truewelcomer.hideUser", this);
foreach (BasePlayer player in BasePlayer.activePlayerList) {
_onlinePlayers.Add(player.UserIDString);
}
}
private void OnNewSave() {
if (!_config.ClearOnWipe) return;
_cachedPlayers.Clear();
_cachedPlayers.Save();
}
private bool ShouldAnnounce(BasePlayer player) {
if (_config.HidePlayersWithPermission && permission.UserHasPermission(player.UserIDString, "truewelcomer.hideUser")) return false;
if (!_config.HidePlayersWithAuthlevel) return true;
ServerUsers.User user = ServerUsers.Get(player.userID);
switch(_config.AuthLevelToHide) {
case AuthLevelEnum.Both when user.group is ServerUsers.UserGroup.Moderator or ServerUsers.UserGroup.Owner:
case AuthLevelEnum.AuthLevel1 when user.group is ServerUsers.UserGroup.Moderator:
case AuthLevelEnum.AuthLevel2 when user.group is ServerUsers.UserGroup.Owner: return false;
default: return true;
}
}
private void OnPlayerDisconnected(BasePlayer player) {
if (player == null) return;
string uid = player.UserIDString;
_onlinePlayers.Remove(uid);
if (ShouldAnnounce(player))
Broadcast(player, "OnLeave", player.displayName);
}
private void OnPlayerSleepEnded(BasePlayer player) {
if (player == null || _onlinePlayers.Contains(player.UserIDString)) return;
bool isOnline = false;
foreach (BasePlayer p in BasePlayer.activePlayerList) {
if (p.UserIDString == player.UserIDString) isOnline = true;
}
string keyToUse = "OnJoin";
if (isOnline) {
_onlinePlayers.Add(player.UserIDString);
if (_cachedPlayers[player.UserIDString] != null) keyToUse = "OnJoin";
else {
keyToUse = "OnWelcome";
_cachedPlayers[player.UserIDString] = player.displayName;
_cachedPlayers.Save();
}
}
if (ShouldAnnounce(player))
Broadcast(player, keyToUse, player.displayName);
}
#endregion
#region Commands
[Command("hidewelcome")]
private void HideWelcomeCommand(BasePlayer player) {
if (!permission.UserHasPermission(player.UserIDString, "truewelcomer.canSetPreference")) {
player.ChatMessage(GetMessage("NoPermission", player.UserIDString));
return;
}
string finalMsg;
if (permission.UserHasPermission(player.UserIDString, "truewelcomer.hideUser")) {
permission.RevokeUserPermission(player.UserIDString, "truewelcomer.hideUser");
finalMsg = GetMessage("NowShowing", player.UserIDString);
} else {
permission.GrantUserPermission(player.UserIDString, "truewelcomer.hideUser", this);
finalMsg = GetMessage("NowHiding", player.UserIDString);
}
finalMsg += _config.HidePlayersWithPermission ? "" : $"\n{GetMessage("ServerConfigAlert", player.UserIDString)}";
player.ChatMessage(finalMsg);
}
[ConsoleCommand("tw.data_refresh")]
private void RefreshDataCommand(ConsoleSystem.Arg arg) {
BasePlayer player2 = arg.Player();
if (player2 != null && !permission.UserHasPermission(player2.UserIDString, "truewelcomer.admin")) {
arg.ReplyWith("You are lacking permission, to run this command.");
return;
}
string msg = "";
int num = 0;
foreach (BasePlayer player in BasePlayer.activePlayerList) {
if (_cachedPlayers[player.UserIDString] != null) num += 0;
else {
_cachedPlayers[player.UserIDString] = player.displayName;
_cachedPlayers.Save();
num += 1;
msg += $"Player {player.displayName} has been added.\n";
}
}
foreach (BasePlayer player in BasePlayer.sleepingPlayerList) {
if (_cachedPlayers[player.UserIDString] != null) num += 0;
else {
_cachedPlayers[player.UserIDString] = player.displayName;
_cachedPlayers.Save();
num += 1;
msg += $"Player {player.displayName} has been added.\n";
}
}
if (num == 0) arg.ReplyWith("No players have been added.");
else {
string message = $"{num} players have been added to the Data List.\n{msg}";
arg.ReplyWith(message);
}
}
[ConsoleCommand("tw.data_reset")]
private void ResetDataCommand(ConsoleSystem.Arg arg) {
BasePlayer player2 = arg.Player();
if (player2 != null && !permission.UserHasPermission(player2.UserIDString, "truewelcomer.admin")) {
arg.ReplyWith("You are lacking permission, to run this command.");
return;
}
_cachedPlayers.Clear();
_cachedPlayers.Save();
arg.ReplyWith("The Data File for True Welcomer has been reset!");
}
#endregion
}
}