forked from LossyDragon/Rocket-Plugin-Jail-Time
/
JailTime.cs
506 lines (436 loc) · 18.7 KB
/
JailTime.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using Steamworks;
using System;
using System.Collections.Generic;
using UnityEngine;
using Rocket.Unturned.Chat;
using Rocket.API;
using Rocket.Unturned.Events;
using Rocket.Unturned.Player;
using Rocket.API.Collections;
using Rocket.Core.Plugins;
using SDG.Unturned;
using Rocket.Unturned;
using Rocket.Core.Logging;
using System.Linq;
namespace ApokPT.RocketPlugins
{
public class JailTime : RocketPlugin<JailTimeConfiguration>
{
private Dictionary<string, Cell> cells = new Dictionary<string, Cell>();
private Dictionary<string, Sentence> players = new Dictionary<string, Sentence>();
// Singleton
public static JailTime Instance;
//Load
protected override void Load()
{
Instance = this;
Logger.LogWarning(JailTime.Instance.Translate("jailtime_console_display"));
if (JailTime.Instance.Configuration.Instance.Enabled)
{
UnturnedPlayerEvents.OnPlayerRevive += Events_OnPlayerRevive;
U.Events.OnPlayerConnected += Events_OnPlayerConnected;
}
//This function will load cells from configuration.
injectConfiCells();
}
//Rocket Unload.
protected override void Unload()
{
//This function will unload cells from configuration.
uninjectConfiCells();
UnturnedPlayerEvents.OnPlayerRevive -= Events_OnPlayerRevive;
U.Events.OnPlayerConnected -= Events_OnPlayerConnected;
}
//Load cells from configuration file.
private void injectConfiCells()
{
foreach (CellLoc cell in Configuration.Instance.Cells)
{
setJail(null, cell.Name.ToLower(), new Vector3(Convert.ToSingle(cell.X), Convert.ToSingle(cell.Y), Convert.ToSingle(cell.Z)));
}
}
//(Rocket unload) Unload players in jail to last location and flush the dictionaries.
private void uninjectConfiCells()
{
//Should Sentence be removed during Unload...
if (JailTime.Instance.Configuration.Instance.ReleasePlayerOnUnload)
{
List<string> keys = new List<string>(players.Keys);
foreach (string key in keys)
{
removePlayer(null, key);
}
//Flush both dictionaries, and local List<>.
cells.Clear();
players.Clear();
keys.Clear();
}
}
//Permissions (Overridden)
public List<string> Permissions
{
get
{
return new List<string>() {};
}
}
//Events for On Player connected
private void Events_OnPlayerConnected(UnturnedPlayer player)
{
if (player.IsAdmin || player.HasPermission("jail.immune")) return;
if (players.ContainsKey(player.ToString()))
{
if (JailTime.Instance.Configuration.Instance.BanOnReconnect)
{
//removePlayerFromJail(player, players[player.ToString()]);
//players.Remove(player.ToString());
//Send player over to Player Component (JailTimePlayer) to handle ban on reconnect.
//Debug.
Logger.Log("Sending " + player.SteamName + " to ban on reconnect.");
Logger.Log("Ping: " + player.Ping);
JailTimePlayer jailtimeplayer = player.GetComponent<JailTimePlayer>();
jailtimeplayer.SetBan(player);
players.Remove(player.ToString());
}
else
{
if (!(players[player.ToString()].End <= DateTime.Now))
{
movePlayerToJail_OnPlayerRevive_Connect(player, players[player.ToString()].Cell);
UnturnedChat.Say(player, JailTime.Instance.Translate("jailtime_player_back_msg"));
}
}
}
}
//Event for On Player Revive (Death or Suicide).
private void Events_OnPlayerRevive(UnturnedPlayer player, Vector3 position, byte angle)
{
if (player.IsAdmin || player.HasPermission("jail.immune")) return;
if (players.ContainsKey(player.ToString()))
{
movePlayerToJail_OnPlayerRevive_Connect(player, players[player.ToString()].Cell);
UnturnedChat.Say(player, JailTime.Instance.Translate("jailtime_player_back_msg"));
}
}
//This function will handle OnPlayerRevive/Connected to re-strip(if enabled) player and sending back to jail without dupping clothes.
private void movePlayerToJail_OnPlayerRevive_Connect(UnturnedPlayer player, Cell jail)
{
if (Instance.Configuration.Instance.StripInventory)
{
InvClear(player);
}
player.Teleport(jail.Location, player.Rotation);
}
//Fixed Update
public void FixedUpdate()
{
if (this.State == PluginState.Loaded && players != null && players.Count != 0)
{
foreach (KeyValuePair<string, Sentence> pl in players)
{
if (pl.Value.End <= DateTime.Now)
{
try
{
removePlayer(null, pl.Key);
break;
}
catch { }
}
try
{
UnturnedPlayer player = UnturnedPlayer.FromCSteamID(new CSteamID(Convert.ToUInt64(pl.Key)));
if (player != null && Vector3.Distance(player.Position, pl.Value.Cell.Location) > Configuration.Instance.WalkDistance)
{
if (Configuration.Instance.KillInsteadOfTeleport)
{
player.Damage(255, player.Position, EDeathCause.PUNCH, ELimb.SKULL, player.CSteamID);
}
else
{
player.Teleport(pl.Value.Cell.Location, player.Rotation);
}
}
}
catch { }
}
}
}
//Private Methods
//Get cells by name.
private Cell getCellbyName(string jailName)
{
return cells.ContainsKey(jailName) ? cells[jailName] : null;
}
//Get random cell.
private Cell getRandomCell()
{
if (cells.Count >= 0)
{
List<string> keys = new List<string>(cells.Keys);
System.Random rand = new System.Random();
return cells[keys[rand.Next(cells.Count)]];
}
return null;
}
//Player Methods
//Adding a player
internal void addPlayer(IRocketPlayer caller, string playerName, string jailName = "", uint jailTime = 0)
{
Cell jail;
UnturnedPlayer target = UnturnedPlayer.FromName(playerName);
if (jailTime == 0) jailTime = Configuration.Instance.JailTimeInSeconds;
if (target == null)
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_notfound", jailName));
return;
}
else if (players.ContainsKey(target.ToString()))
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_in_jail", target.CharacterName));
return;
}
else
{
if (target.IsAdmin || target.HasPermission("jail.immune") || target.HasPermission("jail"))
{
UnturnedChat.Say(target, JailTime.Instance.Translate("jailtime_player_immune"));
return;
}
else if (cells.Count == 0)
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_notset", jailName));
return;
}
else if (jailName == "")
{
jail = getRandomCell();
}
else
{
jail = getCellbyName(jailName);
}
if (jail == null)
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_notfound", jailName));
return;
}
players.Add(target.ToString(), new Sentence(jail, jailTime, target.Position));
movePlayerToJail(target, jail);
UnturnedChat.Say(target, JailTime.Instance.Translate("jailtime_player_arrest_msg", jailTime));
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_arrested", target.CharacterName, jail.Name));
}
}
//Removing a player
internal void removePlayer(UnturnedPlayer caller, string playerName)
{
UnturnedPlayer target;
try
{
target = UnturnedPlayer.FromCSteamID(new CSteamID(Convert.ToUInt64(playerName)));
}
catch
{
target = UnturnedPlayer.FromName(playerName);
}
if (target != null && players.ContainsKey(target.ToString()))
{
removePlayerFromJail(target, players[target.ToString()]);
UnturnedChat.Say(target, JailTime.Instance.Translate("jailtime_player_release_msg"));
if (caller != null)
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_released", target.CharacterName));
players.Remove(target.ToString());
}
else
{
if (caller != null)
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_notfound", playerName));
return;
}
}
//List players
internal void listPlayers(IRocketPlayer caller)
{
if (players.Count == 0)
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_list_clear"));
return;
}
else
{
string playersString = "";
foreach (KeyValuePair<string, Sentence> player in players)
{
try { playersString += UnturnedPlayer.FromName(player.Key).CharacterName + " (" + player.Value.Cell.Name + "), "; }
catch { }
}
if (playersString != "") playersString = playersString.Remove(playersString.Length - 2) + ".";
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_player_list", playersString));
return;
}
}
// Jail Methods
//Set Jail
internal void setJail(UnturnedPlayer caller, string jailName, Vector3 location)
{
if (caller != null)
{
if (cells.ContainsKey(jailName.ToLower()))
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_exists", jailName));
return;
}
else
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_set", jailName));
}
Configuration.Instance.Cells.Add(new CellLoc(jailName, location.x, location.y, location.z));
Configuration.Save();
}
cells.Add(jailName.ToLower(), new Cell(jailName, location));
}
//Unset Jail
internal void unsetJail(IRocketPlayer caller, string jailName)
{
if (!cells.ContainsKey(jailName.ToLower()))
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_notfound", jailName));
return;
}
else
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_unset", jailName));
cells.Remove(jailName.ToLower());
foreach (CellLoc cell in Configuration.Instance.Cells)
{
if (cell.Name.ToLower() == jailName.ToLower())
{
Configuration.Instance.Cells.Remove(cell);
Configuration.Save();
return;
}
}
}
}
//TP to a cell
internal void teleportToCell(UnturnedPlayer caller, string jailName)
{
if (!cells.ContainsKey(jailName.ToLower()))
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_notfound", jailName));
return;
}
else
{
caller.Teleport(cells[jailName.ToLower()].Location, caller.Rotation);
}
}
//List Jails
internal void listJails(IRocketPlayer caller)
{
if (cells.Count == 0)
{
UnturnedChat.Say(caller, JailTime.Instance.Translate("jailtime_jail_notset"));
return;
}
else
{
string jailsString = "";
foreach (KeyValuePair<string, Cell> jail in cells)
{
jailsString += jail.Value.Name + ", ";
}
if (jailsString != "") jailsString = jailsString.Remove(jailsString.Length - 2) + ".";
UnturnedChat.Say(caller, Instance.Translate("jailtime_jail_list", jailsString));
return;
}
}
//Arrest Methods//
//Move selected player to jail.
private void movePlayerToJail(UnturnedPlayer player, Cell jail)
{
//If StripWeapons is TRUE, Remove player's inventory.
if (Instance.Configuration.Instance.StripInventory)
{
InvClear(player);
//if GiveClothes is TRUE, jail clothes will be given (to wear).
if (JailTime.Instance.Configuration.Instance.GiveClothes)
{
player.GiveItem(303, 1);
player.GiveItem(304, 1);
}
}
player.Teleport(jail.Location, player.Rotation);
}
//Remove player from jail
private void removePlayerFromJail(UnturnedPlayer player, Sentence sentence)
{
player.Teleport(sentence.Location, player.Rotation);
}
//Method to attempt inventory clear. | Credit doozzik https://github.com/doozzik/DropManager
//Inventory removal works, but if a player has a gun in slots 1&2, the model of said item stays visible.
private void InvClear(UnturnedPlayer player)
{
for (byte page = 0; page < PlayerInventory.PAGES; page++)
{
byte itemCount = player.Player.inventory.getItemCount(page);
for (byte index = 0; index < itemCount; index++)
{
try
{
player.Player.inventory.removeItem(page, index);
index--;
itemCount--;
}
catch (Exception e)
{
Logger.LogWarning(JailTime.Instance.Translate("jailtime_inv_playerInvError") + player.CharacterName);
Logger.LogWarning(JailTime.Instance.Translate("jailtime_inv_fullError") + e.Message);
}
}
}
}
// Translations
public override TranslationList DefaultTranslations
{
get
{
return new TranslationList(){
//Some log outputs.
{"jailtime_console_display", "JailTime by ApokPT. Updated by Lossy. " },
{"jailtime_inv_playerInvError", "JailTime: Cant clear inventory for player. "},
{"jailtime_inv_fullError", "JailTime: Full problem description: "},
//Jail information
{"jailtime_jail_notset","No cells set, please use /jail set [name] first! "},
{"jailtime_jail_notfound","No cell named {0} found! "},
{"jailtime_jail_set","New cell named {0} created where you stand! "},
{"jailtime_jail_exists","Cell named {0} already exists! "},
{"jailtime_jail_unset","Cell named {0} deleted! "},
{"jailtime_jail_list","Jail Cells: {0} "},
//Jail player information.
{"jailtime_player_immune","That player cannot be arrested! "},
{"jailtime_player_in_jail","Player {0} already in jail! "},
{"jailtime_player_arrested","Player {0} was arrested in {1} cell! "},
{"jailtime_player_released","Player {0} released from jail! "},
{"jailtime_player_list","Players: {0} "},
{"jailtime_player_list_clear","Jail cells are getting dusty! "},
{"jailtime_player_notfound","No player found named {0}! "},
{"jailtime_player_arrest_msg","You have been arrested for {0} seconds! "},
{"jailtime_player_release_msg","You have been released! "},
{"jailtime_player_back_msg","Get back in your cell! "},
//Command Help
{"jailtime_help","/jail commands: add, remove, set, unset, list, teleport "},
{"jailtime_help_add","use /jail add <player> <time> <cell> - to arrest a player, if no <cell> uses a random one "},
{"jailtime_help_remove","use /jail remove <player> - to release a player "},
{"jailtime_help_list","use /jail list players or /jail list cells "},
{"jailtime_help_set","use /jail set <cell> - to set a new jail cell "},
{"jailtime_help_unset","use /jail unset <cell> - to delete a jail cell "},
{"jailtime_help_teleport","use /jail teleport <cell> - to teleport to a cell "},
//Bans
{"jailtime_ban","You have been banned for disconnecting while in Jail! "},
{"jailtime_ban_time","You have been banned temporarily for disconnecting while in Jail! "}
};
}
}
}
}