forked from lmanik/MessagePlugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PluginMain.cs
356 lines (294 loc) · 12.7 KB
/
PluginMain.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
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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Drawing;
using TerrariaApi.Server;
using Terraria;
using TShockAPI.Hooks;
using MySql.Data.MySqlClient;
using TShockAPI;
using TShockAPI.DB;
using System.ComponentModel;
namespace MessagePlugin {
[ApiVersion(1, 22)]
public class MessagePlugin : TerrariaPlugin {
public static List<Message> Messages = new List<Message>();
public override string Name {
get { return "MessagePlugin2"; }
}
public override string Author {
get { return "Created by Lmanik - Renovated by Colin."; }
}
public override string Description {
get { return ""; }
}
public override Version Version {
get { return new Version(1, 1); }
}
public override void Initialize() {
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
ServerApi.Hooks.NetGreetPlayer.Register(this, OnGreetPlayer);
PlayerHooks.PlayerPostLogin += OnPostLogin;
}
protected override void Dispose(bool disposing) {
if (disposing) {
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
ServerApi.Hooks.NetGreetPlayer.Deregister(this, OnGreetPlayer);
PlayerHooks.PlayerPostLogin -= OnPostLogin;
}
base.Dispose(disposing);
}
public MessagePlugin(Main game)
: base(game) {
}
public void OnInitialize(EventArgs e) {
//init Message Plugin db
MDb.InitMessageDB();
//set group
bool msg = false;
foreach (Group group in TShock.Groups.groups) {
if (group.HasPermission("msg.use"))
msg = true;
}
List<string> permlist = new List<string>();
if (!msg)
permlist.Add("msg.use");
TShock.Groups.AddPermissions("trustedadmin", permlist);
Commands.ChatCommands.Add(new Command("msg.use", Msg, "msg"));
}
public void OnGreetPlayer(GreetPlayerEventArgs e) {
if (TShock.Players[e.Who].Group.HasPermission("msg.use")) {
string name = TShock.Players[e.Who].Name;
int count = GetUnreadEmailsByName(name);
TShock.Players[e.Who].SendInfoMessage("You have " + count + " unread messages.");
}
}
public void OnPostLogin(PlayerPostLoginEventArgs e) {
if (TShock.Players[e.Player.Index].Group.HasPermission("msg.use")) {
string name = TShock.Players[e.Player.Index].Name;
int count = GetUnreadEmailsByName(name);
TShock.Players[e.Player.Index].SendMessage("You have " + count + " unread messages.", Color.Fuchsia);
}
}
// Return unread emails
public static int GetUnreadEmailsByName(string name) {
if (name != null) {
List<Message> m = MDb.GetMessagesType(name, false);
return m.Count;
}
return 0;
}
// Save message to db
public static void SendMessage(string to, string from, string text) {
MDb.AddMessage(from, to, text);
List<TSPlayer> players = TShock.Utils.FindPlayer(to);
if (players.Count == 1) {
if (players[0].Group.HasPermission("msg.use"))
players[0].SendInfoMessage("You have a new message from " + from);
}
}
//help message
public static void Help(CommandArgs args) {
args.Player.SendMessage("To send a message use: /msg <player> <message>", Color.Aqua);
args.Player.SendMessage("To list unread messages use: /msg inbox <page number>", Color.Aqua);
args.Player.SendMessage("To list all messages use: /msg list <page number>", Color.Aqua);
args.Player.SendMessage("To read a specific message use: /msg read <id>", Color.Aqua);
args.Player.SendMessage("To delete a message use: /msg del <id>", Color.Aqua);
}
// Run Message command
public static void Msg(CommandArgs args) {
string cmd = "help";
if (args.Parameters.Count > 0) {
cmd = args.Parameters[0].ToLower();
}
switch (cmd) {
case "help": {
//return help
Help(args);
break;
}
//list of all unread messages
case "inbox": {
// Fetch all unread messages
List<Message> messages = MDb.GetMessagesType(args.Player.Name, false);
//How many messages per page
const int pagelimit = 5;
//How many messages per line
const int perline = 1;
//Pages start at 0 but are displayed and parsed at 1
int page = 0;
if (args.Parameters.Count > 1) {
if (!int.TryParse(args.Parameters[1], out page) || page < 1) {
args.Player.SendErrorMessage(string.Format("Invalid page number ({0})", page));
return;
}
page--; //Substract 1 as pages are parsed starting at 1 and not 0
}
if (messages.Count == 0) {
args.Player.SendErrorMessage("You don't have any messages.");
return;
}
//Check if they are trying to access a page that doesn't exist.
int pagecount = messages.Count / pagelimit;
if (page > pagecount) {
args.Player.SendErrorMessage(string.Format("Page number exceeds pages ({0}/{1})", page + 1, pagecount + 1));
return;
}
//Display the current page and the number of pages.
args.Player.SendSuccessMessage(string.Format("Inbox ({0}/{1}):", page + 1, pagecount + 1));
//Add up to pagelimit names to a list
var messageslist = new List<string>();
for (int i = (page * pagelimit); (i < ((page * pagelimit) + pagelimit)) && i < messages.Count; i++) {
messageslist.Add("[" + messages[i].ID + "]" + " " + messages[i].MailFrom + " (" + messages[i].Date + ") [" + (messages[i].Seen ? "Seen" : "Unseen") + "]");
}
//convert the list to an array for joining
var lines = messageslist.ToArray();
for (int i = 0; i < lines.Length; i += perline) {
args.Player.SendInfoMessage(string.Join(", ", lines, i, Math.Min(lines.Length - i, perline)));
}
if (page < pagecount) {
args.Player.SendInfoMessage(string.Format("Type /msg inbox {0} for more unread messages.", (page + 2)));
}
//remove all messages
int count = Messages.Count;
Messages.RemoveRange(0, count);
break;
}
//list of all messages
case "list": {
// Fetch all messages
List<Message> messages = MDb.GetMessages(args.Player.Name);
//How many messages per page
const int pagelimit = 5;
//How many messages per line
const int perline = 1;
//Pages start at 0 but are displayed and parsed at 1
int page = 0;
if (args.Parameters.Count > 1) {
if (!int.TryParse(args.Parameters[1], out page) || page < 1) {
args.Player.SendErrorMessage(string.Format("Invalid page number ({0})", page));
return;
}
page--; //Substract 1 as pages are parsed starting at 1 and not 0
}
if (messages.Count == 0) {
args.Player.SendErrorMessage("You don't have any messages.");
return;
}
//Check if they are trying to access a page that doesn't exist.
int pagecount = messages.Count / pagelimit;
if (page > pagecount) {
args.Player.SendErrorMessage(string.Format("Page number exceeds pages ({0}/{1})", page + 1, pagecount + 1));
return;
}
//Display the current page and the number of pages.
args.Player.SendSuccessMessage(string.Format("List messages ({0}/{1}):", page + 1, pagecount + 1));
//Add up to pagelimit names to a list
var messageslist = new List<string>();
for (int i = (page * pagelimit); (i < ((page * pagelimit) + pagelimit)) && i < messages.Count; i++) {
messageslist.Add("[" + messages[i].ID + "]" + " " + messages[i].MailFrom + " (" + messages[i].Date + ") [" + (messages[i].Seen ? "Seen'" : "Unseen") + "]");
}
//convert the list to an array for joining
var lines = messageslist.ToArray();
for (int i = 0; i < lines.Length; i += perline) {
args.Player.SendInfoMessage(string.Join(", ", lines, i, Math.Min(lines.Length - i, perline)));
}
if (page < pagecount) {
args.Player.SendInfoMessage(string.Format("Type /msg list {0} for more messages.", (page + 2)));
}
//remove all messages
int count = Messages.Count;
Messages.RemoveRange(0, count);
break;
}
//read a specific message
case "read": {
if (args.Parameters.Count > 1) {
int id = int.Parse(args.Parameters[1]);
Message msg = MDb.GetMessage(id, args.Player.Name);
if (msg != null) {
args.Player.SendMessage(msg.ID + ": On " + msg.Date + ", " + msg.MailFrom + " wrote:", Color.Aqua);
args.Player.SendMessage(msg.MailText, Color.White);
//set message to read
MDb.MessageRead(msg.ID);
}
else {
args.Player.SendErrorMessage("Message with the ID \"" + args.Parameters[1].ToString() + "\" does not exist.");
}
}
else {
args.Player.SendErrorMessage("You must specify an ID.");
}
break;
}
//delete specify messages
case "del": {
if (args.Parameters.Count > 1) {
//switch args [id, unread, read, all]
switch (args.Parameters[1].ToString()) {
case "all": {
if (MDb.DelAllMessages(args.Player.Name)) {
args.Player.SendErrorMessage("All messages were deleted.");
}
else {
args.Player.SendErrorMessage("You don't have any messages.");
}
break;
}
case "read": {
if (MDb.DelMessagesType(args.Player.Name, true)) {
args.Player.SendErrorMessage("All read messages were deleted.");
}
else {
args.Player.SendErrorMessage("You don't have any read messages.");
}
break;
}
case "unread": {
if (MDb.DelMessagesType(args.Player.Name, false)) {
args.Player.SendErrorMessage("All unread messages were deleted.");
}
else {
args.Player.SendErrorMessage("You don't have any unread messages.");
}
break;
}
default: {
int id = int.Parse(args.Parameters[1]);
if (MDb.DelMessage(id, args.Player.Name)) {
args.Player.SendErrorMessage("Message with ID \"" + args.Parameters[1].ToString() + "\" was deleted.");
}
else {
args.Player.SendErrorMessage("Message with ID \"" + args.Parameters[1].ToString() + "\" does not exist.");
}
break;
}
}
}
else {
args.Player.SendErrorMessage("You must set second parameter [id, all, unseen, seen]");
}
break;
}
//send message
default: {
if (args.Parameters.Count > 1) {
User userPlr = TShock.Users.GetUserByName(args.Parameters[0]);
if (userPlr == null) {
args.Player.SendErrorMessage("Player " + args.Parameters[0] + " could not be found.");
} else {
string mailTo = args.Parameters[0];
SendMessage(mailTo, args.Player.Name, String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1)));
args.Player.SendSuccessMessage("You sent a message to " + mailTo);
}
}
else {
//return help
Help(args);
}
break;
}
}
}
}
}