forked from CAWCAWCAW/Votekick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVKMain.cs
442 lines (410 loc) · 18.6 KB
/
VKMain.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
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
441
442
/*
* Using this as an example of a TSHOCK Plugin. I will be modifying and/or
* adding features. This should not be considered a working vote kick plugin
* for TSHOCK anymore.
*
* Original code by CAWCAWCAW.
* Modified for test purposes by Rob Filippi (GitFlip).
*
*
*/
using System;
using System.IO;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using TShockAPI;
using TShockAPI.Extensions;
using Terraria;
using Newtonsoft.Json;
using TerrariaApi;
using TerrariaApi.Server;
using TShockAPI.DB;
namespace VoteKick
{
[ApiVersion(1, 16)]
public class Votekick : TerrariaPlugin
{
private static VoteKickTimer _timers;
public override Version Version
{
get { return new Version("1.2"); }
}
public override string Name
{
get { return "Votekick"; }
}
public override string Author
{
get { return "CAWCAWCAW"; }
}
public override string Description
{
get { return "Vote to kick, mute, or ban players"; }
}
public Votekick(Main game)
: base(game)
{
}
public override void Initialize()
{
ReadConfig();
ServerApi.Hooks.NetGetData.Register(this, GetData, 10);
GetDataHandlers.InitGetDataHandler();
_timers = new VoteKickTimer();
_timers.Start();
TShockAPI.Commands.ChatCommands.Add(new Command("caw.votekick", Vote, "votekick", "vk"));
TShockAPI.Commands.ChatCommands.Add(new Command("caw.reloadvotekick", Reload_Config, "reloadvotekick", "rvk"));
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.NetGetData.Deregister(this, GetData);
}
base.Dispose(disposing);
}
private void GetData(GetDataEventArgs e)
{
PacketTypes type = e.MsgID;
var player = TShock.Players[e.Msg.whoAmI];
if (player == null)
{
e.Handled = true;
return;
}
if (!player.ConnectionAlive)
{
e.Handled = true;
return;
}
using (var data = new MemoryStream(e.Msg.readBuffer, e.Index, e.Length))
{
try
{
if (GetDataHandlers.HandlerGetData(type, player, data))
e.Handled = true;
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
}
public static bool VoteKickRunning = false;
public static bool VoteMuteRunning = false;
public static bool VoteBanRunning = false;
public static Poll poll = new Poll();
public class Poll
{
public List<TSPlayer> voters;
public List<TSPlayer> votedyes;
public List<TSPlayer> votedno;
public TSPlayer votedplayer;
public Poll()
{
voters = new List<TSPlayer>();
votedyes = new List<TSPlayer>();
votedno = new List<TSPlayer>();
}
}
public static void Vote(CommandArgs args)
{
if (args.Parameters.Count == 0)
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /votekick [kick/mute/ban/voteyes/voteno/info/cancel]");
return;
}
switch (args.Parameters[0])
{
case "voteyes":
if (!poll.voters.Contains(args.Player) && (VoteKickRunning || VoteMuteRunning || VoteBanRunning))
{
args.Player.SendSuccessMessage("You have voted yes in the vote kick.");
poll.voters.Add(args.Player);
poll.votedyes.Add(args.Player);
}
else if (poll.voters.Contains(args.Player))
{
args.Player.SendErrorMessage("You have already voted for this vote!");
}
else
{
args.Player.SendInfoMessage("A vote is not running at this time.");
}
break;
case "voteno":
if (!poll.voters.Contains(args.Player) && (VoteKickRunning || VoteMuteRunning || VoteBanRunning))
{
args.Player.SendSuccessMessage("You have voted no in the vote kick.");
poll.voters.Add(args.Player);
poll.votedno.Add(args.Player);
}
else if (poll.voters.Contains(args.Player))
{
args.Player.SendErrorMessage("You have already voted for this vote!");
}
else
{
args.Player.SendInfoMessage("A vote is not running at this time.");
}
break;
case "kick":
if (config.CanPlayersVoteKick)
{
if (TShock.Utils.ActivePlayers() >= Votekick.config.AmountofPlayersForVotesToTakeEffect)
{
if (args.Parameters.Count > 1 && !VoteKickRunning && !VoteMuteRunning && !VoteBanRunning)
{
string playerstring = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1));
var players = TShock.Utils.FindPlayer(playerstring);
var plyr = players[0];
if (players.Count == 0)
{
args.Player.SendErrorMessage("No player matched your query '{0}'", playerstring);
}
else if (players.Count > 1)
{
TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
}
TSPlayer.All.SendWarningMessage(args.Player.Name + " has started a votekick against " + plyr.Name);
TSPlayer.All.SendSuccessMessage("[VoteKick] To vote type in /votekick <voteyes/voteno>");
VoteKickRunning = true;
poll.votedplayer = plyr;
_timers.Start();
}
else if (VoteKickRunning || VoteMuteRunning || VoteBanRunning)
{
args.Player.SendErrorMessage("A player has already started a vote on " + poll.votedplayer.Name);
}
else
{
args.Player.SendErrorMessage("Error! Please use /votekick kick <playername>");
}
}
else
{
args.Player.SendErrorMessage("There is not enough players to enable a vote! {0}/{1} need to be on the server.", TShock.Utils.ActivePlayers(), config.AmountofPlayersForVotesToTakeEffect);
}
}
else
{
args.Player.SendErrorMessage("The vote ban feature has been turned off by the server owner.");
}
break;
case "mute":
if (config.CanPlayersVoteMute)
{
if (TShock.Utils.ActivePlayers() >= Votekick.config.AmountofPlayersForVotesToTakeEffect)
{
if (args.Parameters.Count > 1 && !VoteKickRunning && !VoteMuteRunning && !VoteBanRunning)
{
string playerstring = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1));
var players = TShock.Utils.FindPlayer(playerstring);
var plyr = players[0];
if (players.Count == 0)
{
args.Player.SendErrorMessage("No player matched your query '{0}'", playerstring);
}
else if (players.Count > 1)
{
TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
}
TSPlayer.All.SendWarningMessage(args.Player.Name + " has started a vote to mute " + plyr.Name);
TSPlayer.All.SendSuccessMessage("[VoteMute] To vote type in /votekick <voteyes/voteno>");
VoteMuteRunning = true;
poll.votedplayer = plyr;
_timers.Start();
}
else if (VoteMuteRunning || VoteKickRunning || VoteBanRunning)
{
args.Player.SendErrorMessage("A player has already started a vote on " + poll.votedplayer.Name);
}
else
{
args.Player.SendErrorMessage("Error! Please use /votekick mute <playername>");
}
}
else
{
args.Player.SendErrorMessage("There is not enough players to enable a vote! {0}/{1} need to be on the server.", TShock.Utils.ActivePlayers(), config.AmountofPlayersForVotesToTakeEffect);
}
}
else
{
args.Player.SendErrorMessage("The vote mute feature has been turned off by the server owner.");
}
break;
case "ban":
if (config.CanPlayersVoteBan)
{
if (TShock.Utils.ActivePlayers() >= Votekick.config.AmountofPlayersForVotesToTakeEffect)
{
if (args.Parameters.Count > 1 && !VoteKickRunning && !VoteMuteRunning && !VoteBanRunning)
{
string playerstring = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1));
var players = TShock.Utils.FindPlayer(playerstring);
var plyr = players[0];
if (players.Count == 0)
{
args.Player.SendErrorMessage("No player matched your query '{0}'", playerstring);
}
else if (players.Count > 1)
{
TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
}
TSPlayer.All.SendWarningMessage(args.Player.Name + " has started a vote to ban " + plyr.Name + " for {0} days.", config.BanTimeInDays);
TSPlayer.All.SendSuccessMessage("[VoteBan] To vote type in /votekick <voteyes/voteno>");
VoteBanRunning = true;
poll.votedplayer = plyr;
_timers.Start();
}
else if (VoteMuteRunning || VoteKickRunning || VoteBanRunning)
{
args.Player.SendErrorMessage("A player has already started a vote on " + poll.votedplayer.Name);
}
else
{
args.Player.SendErrorMessage("Error! Please use /votekick ban <playername>");
}
}
else
{
args.Player.SendErrorMessage("There is not enough players to enable a vote! {0}/{1} need to be on the server.", TShock.Utils.ActivePlayers(), config.AmountofPlayersForVotesToTakeEffect);
}
}
else
{
args.Player.SendErrorMessage("The vote ban feature has been turned off by the server owner.");
}
break;
case "info":
if (VoteKickRunning)
{
args.Player.SendInfoMessage("Total Players: {0} Player to be kicked: {1}, Votes Yes: {2}, Votes No: {3}", TShock.Utils.ActivePlayers(), poll.votedplayer.Name, poll.votedyes.Count, poll.votedno.Count);
}
if (VoteMuteRunning)
{
args.Player.SendInfoMessage("Total Players: {0} Player to be muted: {1}, Votes Yes: {2}, Votes No: {3}", TShock.Utils.ActivePlayers(), poll.votedplayer.Name, poll.votedyes.Count, poll.votedno.Count);
}
if (VoteBanRunning)
{
args.Player.SendInfoMessage("Total Players: {0} Player to be ban: {1}, Votes Yes: {2}, Votes No: {3}", TShock.Utils.ActivePlayers(), poll.votedplayer.Name, poll.votedyes.Count, poll.votedno.Count);
}
else
args.Player.SendErrorMessage("There is no vote running at this time.");
break;
case "cancel":
if (args.Player.Group.HasPermission("caw.cancelvotekick"))
{
if (VoteKickRunning || VoteMuteRunning || VoteBanRunning)
{
poll.voters.Clear();
poll.votedno.Clear();
poll.votedyes.Clear();
poll.votedplayer = null;
VoteKickRunning = false;
VoteMuteRunning = false;
VoteBanRunning = false;
Votekick._timers.VKTimer.Stop();
Votekick._timers.VKTimerNotify.Stop();
TSPlayer.All.SendInfoMessage(args.Player.Name + " has canceled the vote against " + poll.votedplayer.Name);
}
else
{
args.Player.SendErrorMessage("There is no vote running that you can cancel.");
}
}
else
{
args.Player.SendErrorMessage("You do not have permission to use that command!");
}
break;
}
}
public static Config config;
private static void CreateConfig()
{
string filepath = Path.Combine(TShock.SavePath, "Votekick.json");
try
{
using (var stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (var sr = new StreamWriter(stream))
{
config = new Config();
var configString = JsonConvert.SerializeObject(config, Formatting.Indented);
sr.Write(configString);
}
stream.Close();
}
}
catch (Exception ex)
{
Log.ConsoleError(ex.Message);
}
}
private static bool ReadConfig()
{
string filepath = Path.Combine(TShock.SavePath, "Votekick.json");
try
{
if (File.Exists(filepath))
{
using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var sr = new StreamReader(stream))
{
var configString = sr.ReadToEnd();
config = JsonConvert.DeserializeObject<Config>(configString);
}
stream.Close();
}
return true;
}
else
{
Log.ConsoleError("Votekick config not found. Creating new one...");
CreateConfig();
return false;
}
}
catch (Exception ex)
{
Log.ConsoleError(ex.Message);
}
return false;
}
public class Config
{
public double PercentofPlayersVoteYesToKick = 75;
public double PercentofPlayersVoteYesToMute = 75;
public double PercentofPlayersVoteYesToBan = 75;
public int AmountofPlayersForVotesToTakeEffect = 2;
public string seconds = "Vote time below is in seconds.";
public double VoteTime = 10;
public int BanTimeInDays = 2;
public bool CanPlayersVoteKick = true;
public string KickMessage = "You have been vote kicked from the server.";
public bool CanPlayersVoteMute = true;
public string MuteMessage = "You have been muted by a server vote.";
public bool CanPlayersVoteBan = true;
public string BanMessage = "Ban by vote from the server.";
public string WestServer = "";
public int WestPosition = -1;
public string EastServer = "";
public int EastPosition = -1;
}
private void Reload_Config(CommandArgs args)
{
if (ReadConfig())
{
args.Player.SendMessage("Votekick config reloaded sucessfully.", Color.Yellow);
}
else
{
args.Player.SendErrorMessage("Votekick config reloaded unsucessfully. Check logs for details.");
}
}
}
}