forked from ProjectFairnessLabs/PF-vMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainServer.cs
986 lines (922 loc) · 42.1 KB
/
MainServer.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;
using Newtonsoft.Json;
using static vMenuServer.DebugLog;
using static vMenuShared.ConfigManager;
using vMenuShared;
namespace vMenuServer
{
public static class DebugLog
{
public enum LogLevel
{
error = 1,
success = 2,
info = 4,
warning = 3,
none = 0
}
/// <summary>
/// Global log data function, only logs when debugging is enabled.
/// </summary>
/// <param name="data"></param>
public static void Log(dynamic data, LogLevel level = LogLevel.none)
{
if (MainServer.DebugMode || level == LogLevel.error || level == LogLevel.warning)
{
string prefix = "[vMenu] ";
if (level == LogLevel.error)
{
prefix = "^1[vMenu] [ERROR]^7 ";
}
else if (level == LogLevel.info)
{
prefix = "^5[vMenu] [INFO]^7 ";
}
else if (level == LogLevel.success)
{
prefix = "^2[vMenu] [SUCCESS]^7 ";
}
else if (level == LogLevel.warning)
{
prefix = "^3[vMenu] [WARNING]^7 ";
}
Debug.WriteLine($"{prefix}[DEBUG LOG] {data.ToString()}");
}
}
}
public class MainServer : BaseScript
{
#region vars
// Debug shows more information when doing certain things. Leave it off to improve performance!
public static bool DebugMode = GetResourceMetadata(GetCurrentResourceName(), "server_debug_mode", 0) == "true";
public static string Version { get { return GetResourceMetadata(GetCurrentResourceName(), "version", 0); } }
// Time
private int CurrentHours
{
get { return MathUtil.Clamp(GetSettingsInt(Setting.vmenu_current_hour), 0, 23); }
set { SetConvarReplicated(Setting.vmenu_current_hour.ToString(), MathUtil.Clamp(value, 0, 23).ToString()); }
}
private int CurrentMinutes
{
get { return MathUtil.Clamp(GetSettingsInt(Setting.vmenu_current_minute), 0, 59); }
set { SetConvarReplicated(Setting.vmenu_current_minute.ToString(), MathUtil.Clamp(value, 0, 59).ToString()); }
}
private int MinuteClockSpeed
{
get
{
var value = GetSettingsInt(Setting.vmenu_ingame_minute_duration);
if (value < 100) value = 2000;
return value;
}
}
private bool FreezeTime
{
get { return GetSettingsBool(Setting.vmenu_freeze_time); }
set { SetConvarReplicated(Setting.vmenu_freeze_time.ToString(), value.ToString().ToLower()); }
}
private bool IsServerTimeSynced { get { return GetSettingsBool(Setting.vmenu_sync_to_machine_time); } }
// Weather
private string CurrentWeather
{
get
{
var value = GetSettingsString(Setting.vmenu_current_weather, "CLEAR");
if (!WeatherTypes.Contains(value.ToUpper()))
{
return "CLEAR";
}
return value;
}
set
{
if (string.IsNullOrEmpty(value) || !WeatherTypes.Contains(value.ToUpper()))
{
SetConvarReplicated(Setting.vmenu_current_weather.ToString(), "CLEAR");
}
SetConvarReplicated(Setting.vmenu_current_weather.ToString(), value.ToUpper());
}
}
private bool DynamicWeatherEnabled
{
get { return GetSettingsBool(Setting.vmenu_enable_dynamic_weather); }
set { SetConvarReplicated(Setting.vmenu_enable_dynamic_weather.ToString(), value.ToString().ToLower()); }
}
private bool ManualSnowEnabled
{
get { return GetSettingsBool(Setting.vmenu_enable_snow); }
set { SetConvarReplicated(Setting.vmenu_enable_snow.ToString(), value.ToString().ToLower()); }
}
private bool BlackoutEnabled
{
get { return GetSettingsBool(Setting.vmenu_blackout_enabled); }
set { SetConvarReplicated(Setting.vmenu_blackout_enabled.ToString(), value.ToString().ToLower()); }
}
private int DynamicWeatherMinutes
{
get { return Math.Max(GetSettingsInt(Setting.vmenu_dynamic_weather_timer), 1); }
}
private long lastWeatherChange = 0;
private readonly List<string> CloudTypes = new List<string>()
{
"Cloudy 01",
"RAIN",
"horizonband1",
"horizonband2",
"Puffs",
"Wispy",
"Horizon",
"Stormy 01",
"Clear 01",
"Snowy 01",
"Contrails",
"altostratus",
"Nimbus",
"Cirrus",
"cirrocumulus",
"stratoscumulus",
"horizonband3",
"Stripey",
"horsey",
"shower",
};
private readonly List<string> WeatherTypes = new List<string>()
{
"EXTRASUNNY",
"CLEAR",
"NEUTRAL",
"SMOG",
"FOGGY",
"CLOUDS",
"OVERCAST",
"CLEARING",
"RAIN",
"THUNDER",
"BLIZZARD",
"SNOW",
"SNOWLIGHT",
"XMAS",
"HALLOWEEN"
};
#endregion
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
public MainServer()
{
// name check
if (GetCurrentResourceName() != "vMenu")
{
Exception InvalidNameException = new Exception("\r\n\r\n^1[vMenu] INSTALLATION ERROR!\r\nThe name of the resource is not valid. " +
"Please change the folder name from '^3" + GetCurrentResourceName() + "^1' to '^2vMenu^1' (case sensitive) instead!\r\n\r\n\r\n^7");
try
{
throw InvalidNameException;
}
catch (Exception e)
{
Debug.Write(e.Message);
}
}
else
{
// Add event handlers.
EventHandlers.Add("vMenu:GetPlayerIdentifiers", new Action<int, NetworkCallbackDelegate>((TargetPlayer, CallbackFunction) =>
{
List<string> data = new List<string>();
Players[TargetPlayer].Identifiers.ToList().ForEach(e =>
{
if (!e.Contains("ip:"))
data.Add(e);
});
CallbackFunction(JsonConvert.SerializeObject(data));
}));
EventHandlers.Add("vMenu:RequestPermissions", new Action<Player>(PermissionsManager.SetPermissionsForPlayer));
EventHandlers.Add("vMenu:RequestServerState", new Action<Player>(RequestServerStateFromPlayer));
// check addons file for errors
string addons = LoadResourceFile(GetCurrentResourceName(), "config/addons.json") ?? "{}";
try
{
JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(addons);
// If the above crashes, then the json is invalid and it'll throw warnings in the console.
}
catch (JsonReaderException ex)
{
Debug.WriteLine($"\n\n^1[vMenu] [ERROR] ^7Your addons.json file contains a problem! Error details: {ex.Message}\n\n");
}
// check if permissions are setup (correctly)
if (!GetSettingsBool(Setting.vmenu_use_permissions))
{
Debug.WriteLine("^3[vMenu] [WARNING] vMenu is set up to ignore permissions!\nIf you did this on purpose then you can ignore this warning.\nIf you did not set this on purpose, then you must have made a mistake while setting up vMenu.\nPlease read the vMenu documentation (^5https://docs.vespura.com/vmenu^3).\nMost likely you are not executing the permissions.cfg (correctly).^7");
}
Tick += PlayersFirstTick;
// Start the loops
if (GetSettingsBool(Setting.vmenu_enable_weather_sync))
Tick += WeatherLoop;
if (GetSettingsBool(Setting.vmenu_enable_time_sync))
Tick += TimeLoop;
}
}
#endregion
#region command handler
[Command("vmenuserver", Restricted = true)]
private void ServerCommandHandler(int source, List<object> args, string rawCommand)
{
if (args != null)
{
if (args.Count > 0)
{
if (args[0].ToString().ToLower() == "debug")
{
DebugMode = !DebugMode;
if (source < 1)
{
Debug.WriteLine($"Debug mode is now set to: {DebugMode}.");
}
else
{
Players[source].TriggerEvent("chatMessage", $"vMenu Debug mode is now set to: {DebugMode}.");
}
return;
}
else if (args[0].ToString().ToLower() == "unban" && (source < 1))
{
if (args.Count() > 1 && !string.IsNullOrEmpty(args[1].ToString()))
{
var uuid = args[1].ToString().Trim();
var bans = BanManager.GetBanList();
var banRecord = bans.Find(b => { return b.uuid.ToString() == uuid; });
if (banRecord != null)
{
BanManager.RemoveBan(banRecord);
Debug.WriteLine("Player has been successfully unbanned.");
}
else
{
Debug.WriteLine($"Could not find a banned player with the provided uuid '{uuid}'.");
}
}
else
{
Debug.WriteLine("You did not specify a player to unban, you must enter the FULL playername. Usage: vmenuserver unban \"playername\"");
}
return;
}
else if (args[0].ToString().ToLower() == "weather")
{
if (args.Count < 2 || string.IsNullOrEmpty(args[1].ToString()))
{
Debug.WriteLine("[vMenu] Invalid command syntax. Use 'vmenuserver weather <weatherType>' instead.");
}
else
{
string wtype = args[1].ToString().ToUpper();
if (WeatherTypes.Contains(wtype))
{
TriggerEvent("vMenu:UpdateServerWeather", wtype, BlackoutEnabled, DynamicWeatherEnabled, ManualSnowEnabled);
Debug.WriteLine($"[vMenu] Weather is now set to: {wtype}");
}
else if (wtype.ToLower() == "dynamic")
{
if (args.Count == 3 && !string.IsNullOrEmpty(args[2].ToString()))
{
if ((args[2].ToString().ToLower() ?? $"{DynamicWeatherEnabled}") == "true")
{
TriggerEvent("vMenu:UpdateServerWeather", CurrentWeather, BlackoutEnabled, true, ManualSnowEnabled);
Debug.WriteLine("[vMenu] Dynamic weather is now turned on.");
}
else if ((args[2].ToString().ToLower() ?? $"{DynamicWeatherEnabled}") == "false")
{
TriggerEvent("vMenu:UpdateServerWeather", CurrentWeather, BlackoutEnabled, false, ManualSnowEnabled);
Debug.WriteLine("[vMenu] Dynamic weather is now turned off.");
}
else
{
Debug.WriteLine("[vMenu] Invalid command usage. Correct syntax: vmenuserver weather dynamic <true|false>");
}
}
else
{
Debug.WriteLine("[vMenu] Invalid command usage. Correct syntax: vmenuserver weather dynamic <true|false>");
}
}
else
{
Debug.WriteLine("[vMenu] This weather type is not valid!");
}
}
}
else if (args[0].ToString().ToLower() == "time")
{
if (args.Count == 2)
{
if (args[1].ToString().ToLower() == "freeze")
{
TriggerEvent("vMenu:UpdateServerTime", CurrentHours, CurrentMinutes, !FreezeTime);
Debug.WriteLine($"Time is now {(FreezeTime ? "frozen" : "not frozen")}.");
}
else
{
Debug.WriteLine("Invalid syntax. Use: ^5vmenuserver time <freeze|<hour> <minute>>^7 instead.");
}
}
else if (args.Count > 2)
{
if (int.TryParse(args[1].ToString(), out int hour))
{
if (int.TryParse(args[2].ToString(), out int minute))
{
if (hour >= 0 && hour < 24)
{
if (minute >= 0 && minute < 60)
{
TriggerEvent("vMenu:UpdateServerTime", hour, minute, FreezeTime);
Debug.WriteLine($"Time is now {(hour < 10 ? ("0" + hour.ToString()) : hour.ToString())}:{(minute < 10 ? ("0" + minute.ToString()) : minute.ToString())}.");
}
else
{
Debug.WriteLine("Invalid minute provided. Value must be between 0 and 59.");
}
}
else
{
Debug.WriteLine("Invalid hour provided. Value must be between 0 and 23.");
}
}
else
{
Debug.WriteLine("Invalid syntax. Use: ^5vmenuserver time <freeze|<hour> <minute>>^7 instead.");
}
}
else
{
Debug.WriteLine("Invalid syntax. Use: ^5vmenuserver time <freeze|<hour> <minute>>^7 instead.");
}
}
else
{
Debug.WriteLine("Invalid syntax. Use: ^5vmenuserver time <freeze|<hour> <minute>>^7 instead.");
}
}
else if (args[0].ToString().ToLower() == "ban" && source < 1) // only do this via server console (server id < 1)
{
if (args.Count > 3)
{
Player p = null;
bool findByServerId = args[1].ToString().ToLower() == "id";
string identifier = args[2].ToString().ToLower();
if (findByServerId)
{
if (Players.Any(player => player.Handle == identifier))
{
p = Players.Single(pl => pl.Handle == identifier);
}
else
{
Debug.WriteLine("[vMenu] Could not find this player, make sure they are online.");
return;
}
}
else
{
if (Players.Any(player => player.Name.ToLower() == identifier.ToLower()))
{
p = Players.Single(pl => pl.Name.ToLower() == identifier.ToLower());
}
else
{
Debug.WriteLine("[vMenu] Could not find this player, make sure they are online.");
return;
}
}
string reason = "Banned by staff for:";
args.GetRange(3, args.Count - 3).ForEach(arg => reason += " " + arg);
if (p != null)
{
BanManager.BanRecord ban = new BanManager.BanRecord(
BanManager.GetSafePlayerName(p.Name),
p.Identifiers.ToList(),
new DateTime(3000, 1, 1),
reason,
"Server Console",
new Guid()
);
BanManager.AddBan(ban);
BanManager.BanLog($"[vMenu] Player {p.Name}^7 has been banned by Server Console for [{reason}].");
TriggerEvent("vMenu:BanSuccessful", JsonConvert.SerializeObject(ban).ToString());
string timeRemaining = BanManager.GetRemainingTimeMessage(ban.bannedUntil.Subtract(DateTime.Now));
p.Drop($"You are banned from this server. Ban time remaining: {timeRemaining}. Banned by: {ban.bannedBy}. Ban reason: {ban.banReason}. Additional information: {vMenuShared.ConfigManager.GetSettingsString(vMenuShared.ConfigManager.Setting.vmenu_default_ban_message_information)}.");
}
else
{
Debug.WriteLine("[vMenu] Player not found, could not ban player.");
}
}
else
{
Debug.WriteLine("[vMenu] Not enough arguments, syntax: ^5vmenuserver ban <id|name> <server id|username> <reason>^7.");
}
}
else if (args[0].ToString().ToLower() == "help")
{
Debug.WriteLine("Available commands:");
Debug.WriteLine("(server console only): vmenuserver ban <id|name> <server id|username> <reason> (player must be online!)");
Debug.WriteLine("(server console only): vmenuserver unban <uuid>");
Debug.WriteLine("vmenuserver weather <new weather type | dynamic <true | false>>");
Debug.WriteLine("vmenuserver time <freeze|<hour> <minute>>");
Debug.WriteLine("vmenuserver migrate (This copies all banned players in the bans.json file to the new ban system in vMenu v3.3.0, you only need to do this once)");
}
else if (args[0].ToString().ToLower() == "migrate" && source < 1)
{
string file = LoadResourceFile(GetCurrentResourceName(), "bans.json");
if (string.IsNullOrEmpty(file) || file == "[]")
{
Debug.WriteLine("&1[vMenu] [ERROR]^7 No bans.json file found or it's empty.");
return;
}
Debug.WriteLine("^5[vMenu] [INFO]^7 Importing all ban records from the bans.json file into the new storage system. ^3This may take some time...^7");
var bans = JsonConvert.DeserializeObject<List<BanManager.BanRecord>>(file);
bans.ForEach((br) =>
{
var record = new BanManager.BanRecord(br.playerName, br.identifiers, br.bannedUntil, br.banReason, br.bannedBy, Guid.NewGuid());
BanManager.AddBan(record);
});
Debug.WriteLine("^2[vMenu] [SUCCESS]^7 All ban records have been imported. You now no longer need the bans.json file.");
}
else
{
Debug.WriteLine($"vMenu is currently running version: {Version}. Try ^5vmenuserver help^7 for info.");
}
}
else
{
Debug.WriteLine($"vMenu is currently running version: {Version}. Try ^5vmenuserver help^7 for info.");
}
}
else
{
Debug.WriteLine($"vMenu is currently running version: {Version}. Try ^5vmenuserver help^7 for info.");
}
}
#endregion
#region kick players from personal vehicle
/// <summary>
/// Makes the player leave the personal vehicle.
/// </summary>
/// <param name="source"></param>
/// <param name="vehicleNetId"></param>
/// <param name="playerOwner"></param>
[EventHandler("vMenu:GetOutOfCar")]
private void GetOutOfCar([FromSource] Player source, int vehicleNetId, int playerOwner)
{
if (source != null)
{
if (vMenuShared.PermissionsManager.GetPermissionAndParentPermissions(vMenuShared.PermissionsManager.Permission.PVKickPassengers).Any(perm => vMenuShared.PermissionsManager.IsAllowed(perm, source)))
{
TriggerClientEvent("vMenu:GetOutOfCar", vehicleNetId, playerOwner);
source.TriggerEvent("vMenu:Notify", "All passengers will be kicked out as soon as the vehicle stops moving, or after 10 seconds if they refuse to stop the vehicle.");
}
}
}
#endregion
#region clear area near pos
/// <summary>
/// Clear the area near this point for all players.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
[EventHandler("vMenu:ClearArea")]
private void ClearAreaNearPos(float x, float y, float z)
{
TriggerClientEvent("vMenu:ClearArea", x, y, z);
}
#endregion
#region Manage weather and time changes.
/// <summary>
/// Loop used for syncing and keeping track of the time in-game.
/// </summary>
/// <returns></returns>
private async Task TimeLoop()
{
if (IsServerTimeSynced)
{
var currentTime = DateTime.Now;
CurrentMinutes = currentTime.Minute;
CurrentHours = currentTime.Hour;
// Update this once every 60 seconds.
await Delay(60000);
}
else
{
if (!FreezeTime)
{
if ((CurrentMinutes + 1) > 59)
{
CurrentMinutes = 0;
if ((CurrentHours + 1) > 23)
{
CurrentHours = 0;
}
else
{
CurrentHours++;
}
}
else
{
CurrentMinutes++;
}
}
await Delay(MinuteClockSpeed);
}
}
/// <summary>
/// Task used for syncing and changing weather dynamically.
/// </summary>
/// <returns></returns>
private async Task WeatherLoop()
{
if (DynamicWeatherEnabled)
{
await Delay(DynamicWeatherMinutes * 60000);
if (GetSettingsBool(Setting.vmenu_enable_weather_sync))
{
// Manage dynamic weather changes.
{
// Disable dynamic weather because these weather types shouldn't randomly change.
if (CurrentWeather == "XMAS" || CurrentWeather == "HALLOWEEN" || CurrentWeather == "NEUTRAL")
{
DynamicWeatherEnabled = false;
return;
}
// Is it time to generate a new weather type?
if (GetGameTimer() - lastWeatherChange > (DynamicWeatherMinutes * 60000))
{
// Choose a new semi-random weather type.
RefreshWeather();
// Log if debug mode is on how long the change has taken and what the new weather type will be.
if (DebugMode)
{
Log($"Changing weather, new weather: {CurrentWeather}");
}
}
}
}
}
else
{
await Delay(5000);
}
}
/// <summary>
/// Select a new random weather type, based on the current weather and some patterns.
/// </summary>
private void RefreshWeather()
{
var random = new Random().Next(20);
if (CurrentWeather == "RAIN" || CurrentWeather == "THUNDER")
{
CurrentWeather = "CLEARING";
}
else if (CurrentWeather == "CLEARING")
{
CurrentWeather = "CLOUDS";
}
else
{
switch (random)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
CurrentWeather = (CurrentWeather == "EXTRASUNNY" ? "CLEAR" : "EXTRASUNNY");
break;
case 6:
case 7:
case 8:
CurrentWeather = (CurrentWeather == "SMOG" ? "FOGGY" : "SMOG");
break;
case 9:
case 10:
case 11:
CurrentWeather = (CurrentWeather == "CLOUDS" ? "OVERCAST" : "CLOUDS");
break;
case 12:
case 13:
case 14:
CurrentWeather = (CurrentWeather == "CLOUDS" ? "OVERCAST" : "CLOUDS");
break;
case 15:
CurrentWeather = (CurrentWeather == "OVERCAST" ? "THUNDER" : "OVERCAST");
break;
case 16:
CurrentWeather = (CurrentWeather == "CLOUDS" ? "EXTRASUNNY" : "RAIN");
break;
case 17:
case 18:
case 19:
default:
CurrentWeather = (CurrentWeather == "FOGGY" ? "SMOG" : "FOGGY");
break;
}
}
}
#endregion
#region Sync weather & time with clients
/// <summary>
/// Update the weather for all clients.
/// </summary>
/// <param name="newWeather"></param>
/// <param name="blackoutNew"></param>
/// <param name="dynamicWeatherNew"></param>
[EventHandler("vMenu:UpdateServerWeather")]
private void UpdateWeather(string newWeather, bool blackoutNew, bool dynamicWeatherNew, bool enableSnow)
{
// Automatically enable snow effects whenever one of the snow weather types is selected.
if (newWeather == "XMAS" || newWeather == "SNOWLIGHT" || newWeather == "SNOW" || newWeather == "BLIZZARD")
{
enableSnow = true;
}
// Update the new weather related variables.
CurrentWeather = newWeather;
BlackoutEnabled = blackoutNew;
DynamicWeatherEnabled = dynamicWeatherNew;
ManualSnowEnabled = enableSnow;
// Reset the dynamic weather loop timer to another (default) 10 mintues.
lastWeatherChange = GetGameTimer();
}
/// <summary>
/// Set a new random clouds type and opacity for all clients.
/// </summary>
/// <param name="removeClouds"></param>
[EventHandler("vMenu:UpdateServerWeatherCloudsType")]
private void UpdateWeatherCloudsType(bool removeClouds)
{
if (removeClouds)
{
TriggerClientEvent("vMenu:SetClouds", 0f, "removed");
}
else
{
float opacity = float.Parse(new Random().NextDouble().ToString());
string type = CloudTypes[new Random().Next(0, CloudTypes.Count)];
TriggerClientEvent("vMenu:SetClouds", opacity, type);
}
}
/// <summary>
/// Set and sync the time to all clients.
/// </summary>
/// <param name="newHours"></param>
/// <param name="newMinutes"></param>
/// <param name="freezeTimeNew"></param>
[EventHandler("vMenu:UpdateServerTime")]
private void UpdateTime(int newHours, int newMinutes, bool freezeTimeNew)
{
CurrentHours = newHours;
CurrentMinutes = newMinutes;
FreezeTime = freezeTimeNew;
}
#endregion
#region Online Players Menu Actions
/// <summary>
/// Kick a specific player.
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
/// <param name="kickReason"></param>
[EventHandler("vMenu:KickPlayer")]
private void KickPlayer([FromSource] Player source, int target, string kickReason = "You have been kicked from the server.")
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.Kick") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") ||
IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
{
// If the player is allowed to be kicked.
Player targetPlayer = Players[target];
if (targetPlayer != null)
{
if (!IsPlayerAceAllowed(targetPlayer.Handle, "vMenu.DontKickMe"))
{
TriggerEvent("vMenu:KickSuccessful", source.Name, kickReason, targetPlayer.Name);
KickLog($"Player: {source.Name} has kicked: {targetPlayer.Name} for: {kickReason}.");
TriggerClientEvent(player: source, eventName: "vMenu:Notify", args: $"The target player (<C>{targetPlayer.Name}</C>) has been kicked.");
// Kick the player from the server using the specified reason.
DropPlayer(targetPlayer.Handle, kickReason);
return;
}
// Trigger the client event on the source player to let them know that kicking this player is not allowed.
TriggerClientEvent(player: source, eventName: "vMenu:Notify", args: "Sorry, this player can ~r~not ~w~be kicked.");
return;
}
TriggerClientEvent(player: source, eventName: "vMenu:Notify", args: "An unknown error occurred. Report it here: vespura.com/vmenu");
}
else
{
BanManager.BanCheater(source);
}
}
/// <summary>
/// Kill a specific player.
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
[EventHandler("vMenu:KillPlayer")]
private void KillPlayer([FromSource] Player source, int target)
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.Kill") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") ||
IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
{
Player targetPlayer = Players[target];
if (targetPlayer != null)
{
// Trigger the client event on the target player to make them kill themselves. R.I.P.
TriggerClientEvent(player: targetPlayer, eventName: "vMenu:KillMe", args: source.Name);
return;
}
TriggerClientEvent(player: source, eventName: "vMenu:Notify", args: "An unknown error occurred. Report it here: vespura.com/vmenu");
}
else
{
BanManager.BanCheater(source);
}
}
/// <summary>
/// Teleport a specific player to another player.
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
[EventHandler("vMenu:SummonPlayer")]
private void SummonPlayer([FromSource] Player source, int target)
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.Summon") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") ||
IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
{
// Trigger the client event on the target player to make them teleport to the source player.
Player targetPlayer = Players[target];
if (targetPlayer != null)
{
TriggerClientEvent(player: targetPlayer, eventName: "vMenu:GoToPlayer", args: source.Handle);
return;
}
TriggerClientEvent(player: source, eventName: "vMenu:Notify", args: "An unknown error occurred. Report it here: vespura.com/vmenu");
}
else
{
BanManager.BanCheater(source);
}
}
[EventHandler("vMenu:SendMessageToPlayer")]
private void SendPrivateMessage([FromSource] Player source, int targetServerId, string message)
{
Player targetPlayer = Players[targetServerId];
if (targetPlayer != null)
{
targetPlayer.TriggerEvent("vMenu:PrivateMessage", source.Handle, message);
foreach (Player p in Players)
{
if (p != source && p != targetPlayer)
{
if (vMenuShared.PermissionsManager.IsAllowed(vMenuShared.PermissionsManager.Permission.OPSeePrivateMessages, p))
{
p.TriggerEvent("vMenu:Notify", $"[vMenu Staff Log] <C>{source.Name}</C>~s~ sent a PM to <C>{targetPlayer.Name}</C>~s~: {message}");
}
}
}
}
}
[EventHandler("vMenu:PmsDisabled")]
private void NotifySenderThatDmsAreDisabled([FromSource] Player source, string senderServerId)
{
var p = Players[int.Parse(senderServerId)];
if (p != null)
{
p.TriggerEvent("vMenu:Notify", $"Sorry, your private message to <C>{source.Name}</C>~s~ could not be delivered because they disabled private messages.");
}
}
#endregion
#region logging and update checks notifications
/// <summary>
/// If enabled using convars, will log all kick actions to the server console as well as an external file.
/// </summary>
/// <param name="kickLogMesage"></param>
private static void KickLog(string kickLogMesage)
{
//if (GetConvar("vMenuLogKickActions", "true") == "true")
if (GetSettingsBool(Setting.vmenu_log_kick_actions))
{
string file = LoadResourceFile(GetCurrentResourceName(), "vmenu.log") ?? "";
DateTime date = DateTime.Now;
string formattedDate = (date.Day < 10 ? "0" : "") + date.Day + "-" +
(date.Month < 10 ? "0" : "") + date.Month + "-" +
(date.Year < 10 ? "0" : "") + date.Year + " " +
(date.Hour < 10 ? "0" : "") + date.Hour + ":" +
(date.Minute < 10 ? "0" : "") + date.Minute + ":" +
(date.Second < 10 ? "0" : "") + date.Second;
string outputFile = file + $"[\t{formattedDate}\t] [KICK ACTION] {kickLogMesage}\n";
SaveResourceFile(GetCurrentResourceName(), "vmenu.log", outputFile, -1);
Debug.WriteLine("^3[vMenu] [KICK]^7 " + kickLogMesage + "\n");
}
}
#endregion
#region Add teleport location
[EventHandler("vMenu:SaveTeleportLocation")]
private void AddTeleportLocation([FromSource] Player source, string locationJson)
{
TeleportLocation location = JsonConvert.DeserializeObject<TeleportLocation>(locationJson);
if (GetTeleportLocationsData().Any(loc => loc.name == location.name))
{
Log("A teleport location with this name already exists, location was not saved.", LogLevel.error);
return;
}
var locs = GetLocations();
locs.teleports.Add(location);
if (!SaveResourceFile(GetCurrentResourceName(), "config/locations.json", JsonConvert.SerializeObject(locs, Formatting.Indented), -1))
{
Log("Could not save locations.json file, reason unknown.", LogLevel.error);
}
TriggerClientEvent("vMenu:UpdateTeleportLocations", JsonConvert.SerializeObject(locs.teleports));
}
#endregion
#region Infinity bits
private void RequestServerStateFromPlayer([FromSource] Player player)
{
player.TriggerEvent("vMenu:SetServerState", new
{
IsInfinity = GetConvar("onesync_enableInfinity", "false") == "true"
});
}
[EventHandler("vMenu:RequestPlayerList")]
private void RequestPlayerListFromPlayer([FromSource] Player player)
{
player.TriggerEvent("vMenu:ReceivePlayerList", Players.Select(p => new
{
n = p.Name,
s = int.Parse(p.Handle),
}));
}
[EventHandler("vMenu:GetPlayerCoords")]
private void GetPlayerCoords([FromSource] Player source, int playerId, NetworkCallbackDelegate callback)
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.Teleport") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") ||
IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
{
var coords = Players[playerId]?.Character?.Position ?? Vector3.Zero;
_ = callback(coords);
return;
}
_ = callback(Vector3.Zero);
}
#endregion
#region Player join/quit
private HashSet<string> joinedPlayers = new HashSet<string>();
private Task PlayersFirstTick()
{
Tick -= PlayersFirstTick;
foreach (var player in Players)
{
joinedPlayers.Add(player.Handle);
}
return Task.FromResult(0);
}
[EventHandler("playerJoining")]
private void OnPlayerJoining([FromSource] Player sourcePlayer)
{
joinedPlayers.Add(sourcePlayer.Handle);
foreach (var player in Players)
{
if (IsPlayerAceAllowed(player.Handle, "vMenu.MiscSettings.JoinQuitNotifs") ||
IsPlayerAceAllowed(player.Handle, "vMenu.MiscSettings.All"))
{
player.TriggerEvent("vMenu:PlayerJoinQuit", sourcePlayer.Name, null);
}
}
}
[EventHandler("playerDropped")]
private void OnPlayerDropped([FromSource] Player sourcePlayer, string reason)
{
if (!joinedPlayers.Contains(sourcePlayer.Handle))
{
return;
}
joinedPlayers.Remove(sourcePlayer.Handle);
foreach (var player in Players)
{
if (IsPlayerAceAllowed(player.Handle, "vMenu.MiscSettings.JoinQuitNotifs") ||
IsPlayerAceAllowed(player.Handle, "vMenu.MiscSettings.All"))
{
player.TriggerEvent("vMenu:PlayerJoinQuit", sourcePlayer.Name, reason);
}
}
}
#endregion
}
}