-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cs
1602 lines (1362 loc) · 67.1 KB
/
QuickSort.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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using UnityEngine;
using Pool = Facepunch.Pool;
/*
7. Use offsets instead of anchors for all the child UI elements (my preference).
8. Is it necessary to use OnEntityDeath / OnEntityKill / OnPlayerSleep?
Nowadays I think the loot hooks are sufficiently comprehensive.
Do need OnPlayerLootEnd and possibly OnLootEntityEnd (for edge cases).
*/
namespace Oxide.Plugins
{
[Info("Quick Sort", "MON@H", "1.8.3")]
[Description("Adds a GUI that allows players to quickly sort items into containers")]
public class QuickSort : RustPlugin
{
#region Variables
private const string GUIPanelName = "QuickSortUI";
private const string PermissionAutoLootAll = "quicksort.autolootall";
private const string PermissionLootAll = "quicksort.lootall";
private const string PermissionUse = "quicksort.use";
private readonly Hash<int, string> _cacheUiJson = new Hash<int, string>();
private readonly Hash<string, int> _cacheLanguageIDs = new Hash<string, int>();
private readonly HashSet<uint> _cacheContainersExcluded = new HashSet<uint>();
// Keep track of UI viewers to reduce unnecessary calls to destroy the UI.
private readonly HashSet<ulong> _uiViewers = new HashSet<ulong>();
private object[] _noteInv = new object[2];
// When players do not have data, use this shared object to avoid unnecessary heap allocations.
private PlayerData _defaultPlayerData;
#endregion Variables
#region Initialization
private void Init()
{
UnsubscribeHooks();
RegisterPermissions();
AddCommands();
}
private void OnServerInitialized()
{
CreateCache();
LoadData();
SubscribeHooks();
}
private void Unload()
{
foreach (BasePlayer activePlayer in BasePlayer.activePlayerList)
{
UiDestroy(activePlayer);
}
}
#endregion Initialization
#region Configuration
private ConfigData _configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Global settings")]
public GlobalConfiguration GlobalSettings = new GlobalConfiguration();
[JsonProperty(PropertyName = "Custom UI Settings")]
public UiConfiguration CustomUISettings = new UiConfiguration();
public class GlobalConfiguration
{
[JsonProperty(PropertyName = "Default enabled")]
public bool DefaultEnabled = true;
[JsonProperty(PropertyName = "Default UI style (center, lite, right, custom)")]
public string DefaultUiStyle = "right";
[JsonProperty(PropertyName = "Loot all delay in seconds (0 to disable)")]
public int LootAllDelay = 0;
[JsonProperty(PropertyName = "Enable loot all on the sleepers")]
public bool LootSleepers = false;
[JsonProperty(PropertyName = "Auto loot all enabled by default")]
public bool AutoLootAll = false;
[JsonProperty(PropertyName = "Default enabled container types")]
public PlayerContainers Containers = new PlayerContainers();
[JsonProperty(PropertyName = "Chat steamID icon")]
public ulong SteamIDIcon = 0;
[JsonProperty(PropertyName = "Chat command")]
public string[] Commands = new[] { "qs", "quicksort" };
[JsonProperty(PropertyName = "Excluded containers", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<string> ContainersExcluded = new List<string>()
{
"assets/prefabs/deployable/single shot trap/guntrap.deployed.prefab",
"assets/prefabs/npc/autoturret/autoturret_deployed.prefab",
"assets/prefabs/npc/flame turret/flameturret.deployed.prefab",
"assets/prefabs/npc/sam_site_turret/sam_site_turret_deployed.prefab",
"assets/prefabs/npc/sam_site_turret/sam_static.prefab",
};
}
public class UiConfiguration
{
public string AnchorsMin = "0.5 1.0";
public string AnchorsMax = "0.5 1.0";
public string OffsetsMin = "192 -137";
public string OffsetsMax = "573 0";
public string Color = "0.5 0.5 0.5 0.33";
public string ButtonsColor = "0.75 0.43 0.18 0.8";
public string LootAllColor = "0.41 0.50 0.25 0.8";
public string TextColor = "0.77 0.92 0.67 0.8";
public int TextSize = 16;
public int CategoriesTextSize = 14;
}
}
public class PlayerContainers
{
public bool Belt = false;
public bool Main = true;
public bool Wear = false;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
SaveConfig();
}
}
catch
{
PrintError("The configuration file is corrupted");
LoadDefaultConfig();
SaveConfig();
}
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig() => Config.WriteObject(_configData);
#endregion Configuration
#region DataFile
private StoredData _storedData;
private class StoredData
{
public readonly Hash<ulong, PlayerData> PlayerData = new Hash<ulong, PlayerData>();
}
public class PlayerData
{
public bool Enabled;
public bool AutoLootAll;
public string UiStyle;
public PlayerContainers Containers;
}
public void LoadData()
{
_storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Name);
List<ulong> toRemove = Pool.Get<List<ulong>>();
foreach (KeyValuePair<ulong, PlayerData> playerData in _storedData.PlayerData)
{
if (!playerData.Value.AutoLootAll.Equals(_defaultPlayerData.AutoLootAll))
{
continue;
}
if (!playerData.Value.Enabled.Equals(_defaultPlayerData.Enabled))
{
continue;
}
if (!playerData.Value.UiStyle.Equals(_defaultPlayerData.UiStyle))
{
continue;
}
if (!playerData.Value.Containers.Belt.Equals(_defaultPlayerData.Containers.Belt))
{
continue;
}
if (!playerData.Value.Containers.Main.Equals(_defaultPlayerData.Containers.Main))
{
continue;
}
if (!playerData.Value.Containers.Wear.Equals(_defaultPlayerData.Containers.Wear))
{
continue;
}
toRemove.Add(playerData.Key);
}
if (toRemove.Count > 0)
{
for (int i = 0; i < toRemove.Count; i++)
{
_storedData.PlayerData.Remove(toRemove[i]);
}
Puts($"Removed {toRemove.Count} players with default settings from datafile.");
SaveData();
}
Pool.FreeUnmanaged(ref toRemove);
}
public void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Name, _storedData);
public PlayerData GetPlayerData(ulong userID)
{
return _storedData.PlayerData[userID] ?? _defaultPlayerData;
}
#endregion DataFile
#region Localization
public string Lang(string key, string userIDString = null, params object[] args)
{
try
{
return string.Format(lang.GetMessage(key, this, userIDString), args);
}
catch (Exception ex)
{
PrintError($"Lang Key '{key}' threw exception:\n{ex}");
throw;
}
}
private static class LangKeys
{
public static class Error
{
private const string Base = nameof(Error) + ".";
public const string NoPermission = Base + nameof(NoPermission);
public const string Syntax = Base + nameof(Syntax);
}
public static class Info
{
private const string Base = nameof(Info) + ".";
public const string QuickSort = Base + nameof(QuickSort);
public const string Style = Base + nameof(Style);
public const string AutoLootAll = Base + nameof(AutoLootAll);
public const string ContainerType = Base + nameof(ContainerType);
}
public static class Format
{
private const string Base = nameof(Format) + ".";
public const string All = Base + nameof(All);
public const string Ammo = Base + nameof(Ammo);
public const string Attire = Base + nameof(Attire);
public const string Components = Base + nameof(Components);
public const string Construction = Base + nameof(Construction);
public const string Deployables = Base + nameof(Deployables);
public const string Deposit = Base + nameof(Deposit);
public const string Disabled = Base + nameof(Disabled);
public const string Electrical = Base + nameof(Electrical);
public const string Enabled = Base + nameof(Enabled);
public const string Existing = Base + nameof(Existing);
public const string Food = Base + nameof(Food);
public const string LootAll = Base + nameof(LootAll);
public const string Medical = Base + nameof(Medical);
public const string Misc = Base + nameof(Misc);
public const string Prefix = Base + nameof(Prefix);
public const string Resources = Base + nameof(Resources);
public const string Tools = Base + nameof(Tools);
public const string Traps = Base + nameof(Traps);
public const string Weapons = Base + nameof(Weapons);
}
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
[LangKeys.Error.NoPermission] = "You do not have permission to use this command!",
[LangKeys.Format.All] = "All",
[LangKeys.Format.Ammo] = "Ammo",
[LangKeys.Format.Attire] = "Attire",
[LangKeys.Format.Components] = "Components",
[LangKeys.Format.Construction] = "Construction",
[LangKeys.Format.Deployables] = "Deployables",
[LangKeys.Format.Deposit] = "Deposit",
[LangKeys.Format.Disabled] = "<color=#B22222>Disabled</color>",
[LangKeys.Format.Electrical] = "Electrical",
[LangKeys.Format.Enabled] = "<color=#228B22>Enabled</color>",
[LangKeys.Format.Existing] = "Existing",
[LangKeys.Format.Food] = "Food",
[LangKeys.Format.LootAll] = "Loot All",
[LangKeys.Format.Medical] = "Medical",
[LangKeys.Format.Misc] = "Misc",
[LangKeys.Format.Prefix] = "<color=#00FF00>[Quick Sort]</color>: ",
[LangKeys.Format.Resources] = "Resources",
[LangKeys.Format.Tools] = "Tools",
[LangKeys.Format.Traps] = "Traps",
[LangKeys.Format.Weapons] = "Weapons",
[LangKeys.Info.AutoLootAll] = "Automated looting is now {0}",
[LangKeys.Info.ContainerType] = "Quick Sort for container type {0} is now {1}",
[LangKeys.Info.QuickSort] = "Quick Sort GUI is now {0}",
[LangKeys.Info.Style] = "Quick Sort GUI style is now {0}",
[LangKeys.Error.Syntax] = "List Commands:\n" +
"<color=#FFFF00>/{0} on</color> - Enable GUI\n" +
"<color=#FFFF00>/{0} off</color> - Disable GUI\n" +
"<color=#FFFF00>/{0} auto</color> - Enable/Disable automated looting\n" +
"<color=#FFFF00>/{0} <s | style> <center | lite | right | custom></color> - change GUI style\n" +
"<color=#FFFF00>/{0} <c | conatiner> <main | wear | belt></color> - add/remove container type from the sort",
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
[LangKeys.Error.NoPermission] = "У вас нет разрешения на использование этой команды!",
[LangKeys.Format.All] = "Всё",
[LangKeys.Format.Ammo] = "Патроны",
[LangKeys.Format.Attire] = "Одежда",
[LangKeys.Format.Components] = "Компоненты",
[LangKeys.Format.Construction] = "Конструкции",
[LangKeys.Format.Deployables] = "Развертываемые",
[LangKeys.Format.Deposit] = "Положить",
[LangKeys.Format.Disabled] = "<color=#B22222>Отключена</color>",
[LangKeys.Format.Electrical] = "Электричество",
[LangKeys.Format.Enabled] = "<color=#228B22>Включена</color>",
[LangKeys.Format.Existing] = "Существующие",
[LangKeys.Format.Food] = "Еда",
[LangKeys.Format.LootAll] = "Забрать всё",
[LangKeys.Format.Medical] = "Медикаменты",
[LangKeys.Format.Misc] = "Разное",
[LangKeys.Format.Prefix] = "<color=#00FF00>[Быстрая сортировка]</color>: ",
[LangKeys.Format.Resources] = "Ресурсы",
[LangKeys.Format.Tools] = "Инструменты",
[LangKeys.Format.Traps] = "Ловушки",
[LangKeys.Format.Weapons] = "Оружие",
[LangKeys.Info.AutoLootAll] = "Забирать всё автоматически теперь {0}",
[LangKeys.Info.ContainerType] = "Быстрая сортировка для типа контейнера {0} теперь {1}",
[LangKeys.Info.QuickSort] = "GUI быстрой сортировки теперь {0}",
[LangKeys.Info.Style] = "Стиль GUI быстрой сортировки теперь {0}",
[LangKeys.Error.Syntax] = "Список команд:\n" +
"<color=#FFFF00>/{0} on</color> - Включить GUI\n" +
"<color=#FFFF00>/{0} off</color> - Отключить GUI\n" +
"<color=#FFFF00>/{0} auto</color> - Включить/Отключить забирать всё автоматически.\n" +
"<color=#FFFF00>/{0} <s | style> <center | lite | right | custom></color> - изменить стиль GUI быстрой сортировки.\n" +
"<color=#FFFF00>/{0} <c | conatiner> <main | wear | belt></color> - добавить/удалить тип контейнера для сортировки.",
}, this, "ru");
}
#endregion Localization
#region Oxide Hooks
private void OnLootPlayer(BasePlayer player) => UiCreate(player);
private void OnLootEntity(BasePlayer player, BaseEntity entity)
{
if (entity.IsValid() && !IsContainerExcluded(player, entity))
{
HandleLootEntity(player);
}
}
private void OnPlayerLootEnd(PlayerLoot inventory)
{
BasePlayer player = inventory.baseEntity;
if (player.IsValid())
{
UiDestroy(player);
}
}
private void OnEntityDeath(BasePlayer player, HitInfo info)
{
if (player.IsValid() && !player.IsNpc && player.userID.IsSteamId())
{
UiDestroy(player);
}
}
private void OnEntityKill(BasePlayer player)
{
if (player.IsValid() && !player.IsNpc && player.userID.IsSteamId())
{
UiDestroy(player);
}
}
private void OnPlayerSleep(BasePlayer player) => UiDestroy(player);
#endregion Oxide Hooks
#region Commands
private void CmdQuickSort(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, PermissionUse))
{
PlayerSendMessage(player, Lang(LangKeys.Error.NoPermission, player.UserIDString));
return;
}
if (args == null || args.Length == 0)
{
PlayerSendMessage(player, Lang(LangKeys.Error.Syntax, player.UserIDString, _configData.GlobalSettings.Commands[0]));
return;
}
PlayerData playerData = _storedData.PlayerData[player.userID];
if (playerData == null)
{
playerData = new PlayerData()
{
Enabled = _configData.GlobalSettings.DefaultEnabled,
AutoLootAll = _configData.GlobalSettings.AutoLootAll,
UiStyle = _configData.GlobalSettings.DefaultUiStyle,
Containers = _configData.GlobalSettings.Containers,
};
_storedData.PlayerData[player.userID] = playerData;
}
switch (args[0].ToLower())
{
case "on":
if (!playerData.Enabled)
{
playerData.Enabled = true;
SaveData();
}
PlayerSendMessage(player, Lang(LangKeys.Info.QuickSort, player.UserIDString, Lang(LangKeys.Format.Enabled, player.UserIDString)));
return;
case "off":
if (playerData.Enabled)
{
playerData.Enabled = false;
SaveData();
}
PlayerSendMessage(player, Lang(LangKeys.Info.QuickSort, player.UserIDString, Lang(LangKeys.Format.Disabled, player.UserIDString)));
return;
case "auto":
playerData.AutoLootAll = !playerData.AutoLootAll;
SaveData();
PlayerSendMessage(player, Lang(LangKeys.Info.AutoLootAll, player.UserIDString, playerData.AutoLootAll ? Lang(LangKeys.Format.Enabled, player.UserIDString) : Lang(LangKeys.Format.Disabled, player.UserIDString)));
return;
case "s":
case "style":
{
if (args.Length > 1)
{
switch (args[1].ToLower())
{
case "center":
case "lite":
case "right":
case "custom":
{
playerData.UiStyle = args[1].ToLower();
SaveData();
PlayerSendMessage(player, Lang(LangKeys.Info.Style, player.UserIDString, args[1].ToLower()));
return;
}
}
}
break;
}
case "c":
case "container":
{
if (args.Length > 1)
{
switch (args[1].ToLower())
{
case "main":
{
bool flag = false;
if (args.Length > 2 && bool.TryParse(args[2], out flag))
{
playerData.Containers.Main = flag;
}
else
{
playerData.Containers.Main = !playerData.Containers.Main;
}
SaveData();
PlayerSendMessage(player, Lang(LangKeys.Info.ContainerType, player.UserIDString, "main", playerData.Containers.Main ? Lang(LangKeys.Format.Enabled, player.UserIDString) : Lang(LangKeys.Format.Disabled, player.UserIDString)));
return;
}
case "wear":
{
bool flag = false;
if (args.Length > 2 && bool.TryParse(args[2], out flag))
{
playerData.Containers.Wear = flag;
}
else
{
playerData.Containers.Wear = !playerData.Containers.Wear;
}
SaveData();
PlayerSendMessage(player, Lang(LangKeys.Info.ContainerType, player.UserIDString, "wear", playerData.Containers.Wear ? Lang(LangKeys.Format.Enabled, player.UserIDString) : Lang(LangKeys.Format.Disabled, player.UserIDString)));
return;
}
case "belt":
{
bool flag = false;
if (args.Length > 2 && bool.TryParse(args[2], out flag))
{
playerData.Containers.Belt = flag;
}
else
{
playerData.Containers.Belt = !playerData.Containers.Belt;
}
SaveData();
PlayerSendMessage(player, Lang(LangKeys.Info.ContainerType, player.UserIDString, "belt", playerData.Containers.Belt ? Lang(LangKeys.Format.Enabled, player.UserIDString) : Lang(LangKeys.Format.Disabled, player.UserIDString)));
return;
}
}
}
break;
}
}
PlayerSendMessage(player, Lang(LangKeys.Error.Syntax, player.UserIDString, _configData.GlobalSettings.Commands[0]));
}
[ConsoleCommand("quicksortgui")]
private void SortCommand(ConsoleSystem.Arg arg)
{
BasePlayer player = arg.Player();
if (player.IsValid() && permission.UserHasPermission(player.UserIDString, PermissionUse))
{
try
{
SortItems(player, arg.Args);
}
catch { }
}
}
[ConsoleCommand("quicksortgui.lootall")]
private void LootAllCommand(ConsoleSystem.Arg arg)
{
BasePlayer player = arg.Player();
if (player.IsValid() && permission.UserHasPermission(player.UserIDString, PermissionLootAll))
{
timer.Once(_configData.GlobalSettings.LootAllDelay, () => LootAll(player));
}
}
[ConsoleCommand("quicksortgui.lootdelay")]
private void LootDelayCommand(ConsoleSystem.Arg arg)
{
BasePlayer player = arg.Player();
if (player.IsValid() && player.IsAdmin)
{
int x;
if (int.TryParse(arg.Args[0], out x))
{
_configData.GlobalSettings.LootAllDelay = x;
SaveConfig();
}
}
}
#endregion Commands
#region Loot Handling
public void HandleLootEntity(BasePlayer player)
{//We need this to wait for container initialization
NextTick(() =>
{
if (permission.UserHasPermission(player.UserIDString, PermissionAutoLootAll)
&& AutoLootAll(player))
{
return;
}
UiCreate(player);
});
}
public bool AutoLootAll(BasePlayer player)
{
PlayerData playerData = GetPlayerData(player.userID);
if (!playerData.AutoLootAll)
{
return false;
}
List<ItemContainer> containers = GetLootedInventory(player);
if (containers == null)
{
return false;
}
int fullyLooted = 0;
foreach (ItemContainer itemContainer in containers)
{
if (itemContainer.HasFlag(ItemContainer.Flag.NoItemInput))
{
LootAll(player);
if (itemContainer.IsEmpty())
{
fullyLooted++;
}
}
}
if (fullyLooted > 0 && fullyLooted == containers.Count)
{
player.EndLooting();
return true;
}
return false;
}
public void LootAll(BasePlayer player)
{
List<ItemContainer> containers = GetLootedInventory(player);
if (containers == null)
{
return;
}
if (!_configData.GlobalSettings.LootSleepers && IsOwnerSleeper(containers[0]))
{
return;
}
List<Item> itemsSelected = Pool.Get<List<Item>>();
foreach (ItemContainer itemContainer in containers)
{
for (int i = 0; i < itemContainer.itemList.Count; i++)
{
itemsSelected.Add(itemContainer.itemList[i]);
}
}
itemsSelected.Sort((item1, item2) => item2.info.itemid.CompareTo(item1.info.itemid));
foreach (Item item in itemsSelected)
{
int amount = item.amount;
if (item.MoveToContainer(player.inventory.containerMain))
{
_noteInv[0] = item.info.itemid;
_noteInv[1] = amount;
player.Command("note.inv", _noteInv);
continue;
}
if (item.MoveToContainer(player.inventory.containerBelt))
{
_noteInv[0] = item.info.itemid;
_noteInv[1] = amount;
player.Command("note.inv", _noteInv);
continue;
}
int movedAmount = amount - item.amount;
if (movedAmount > 0)
{
_noteInv[0] = item.info.itemid;
_noteInv[1] = movedAmount;
player.Command("note.inv", _noteInv);
}
}
Pool.FreeUnmanaged(ref itemsSelected);
}
public void SortItems(BasePlayer player, string[] args)
{
if (!player.IsValid())
{
return;
}
PlayerContainers type = GetPlayerData(player.userID).Containers;
ItemContainer container = GetLootedInventory(player)[0];
ItemContainer playerMain = player.inventory?.containerMain;
ItemContainer playerWear = player.inventory?.containerWear;
ItemContainer playerBelt = player.inventory?.containerBelt;
if (container == null || playerMain == null || container.HasFlag(ItemContainer.Flag.NoItemInput))
{
return;
}
List<Item> itemsSelected = Pool.Get<List<Item>>();
if (args == null)
{
if (_configData.GlobalSettings.Containers.Main && (type == null || type.Main))
{
for (int i = 0; i < playerMain.itemList.Count; i++)
{
itemsSelected.Add(playerMain.itemList[i]);
}
}
if (playerWear != null && _configData.GlobalSettings.Containers.Wear && type != null && type.Wear)
{
for (int i = 0; i < playerWear.itemList.Count; i++)
{
itemsSelected.Add(playerWear.itemList[i]);
}
}
if (playerBelt != null && _configData.GlobalSettings.Containers.Belt && type != null && type.Belt)
{
for (int i = 0; i < playerBelt.itemList.Count; i++)
{
itemsSelected.Add(playerBelt.itemList[i]);
}
}
}
else
{
if (args[0].Equals("existing"))
{
if (_configData.GlobalSettings.Containers.Main && (type == null || type.Main))
{
AddExistingItems(itemsSelected, playerMain, container);
}
if (playerWear != null && _configData.GlobalSettings.Containers.Wear && type != null && type.Wear)
{
AddExistingItems(itemsSelected, playerWear, container);
}
if (playerBelt != null && _configData.GlobalSettings.Containers.Belt && type != null && type.Belt)
{
AddExistingItems(itemsSelected, playerBelt, container);
}
}
else
{
ItemCategory category = StringToItemCategory(args[0]);
if (_configData.GlobalSettings.Containers.Main && (type == null || type.Main))
{
AddItemsOfType(itemsSelected, playerMain, category);
}
if (playerWear != null && _configData.GlobalSettings.Containers.Wear && type != null && type.Wear)
{
AddItemsOfType(itemsSelected, playerWear, category);
}
if (playerBelt != null && _configData.GlobalSettings.Containers.Belt && type != null && type.Belt)
{
AddItemsOfType(itemsSelected, playerBelt, category);
}
}
}
itemsSelected.Sort((item1, item2) => item2.info.itemid.CompareTo(item1.info.itemid));
MoveItems(itemsSelected, container);
Pool.FreeUnmanaged(ref itemsSelected);
}
public void AddExistingItems(List<Item> list, ItemContainer primary, ItemContainer secondary)
{
if (primary == null || secondary == null)
{
return;
}
foreach (Item primaryItem in primary.itemList)
{
foreach (Item secondaryItem in secondary.itemList)
{
if (primaryItem.info.itemid == secondaryItem.info.itemid)
{
list.Add(primaryItem);
break;
}
}
}
}
public void AddItemsOfType(List<Item> list, ItemContainer container, ItemCategory category)
{
foreach (Item item in container.itemList)
{
if (item.info.category == category)
{
list.Add(item);
}
}
}
public List<ItemContainer> GetLootedInventory(BasePlayer player)
{
PlayerLoot playerLoot = player.inventory.loot;
if (playerLoot != null && playerLoot.IsLooting())
{
List<ItemContainer> containers = playerLoot.containers;
foreach (ItemContainer container in containers)
{
BaseEntity entity = container.entityOwner;
if (entity.IsValid() && IsContainerExcluded(player, entity))
{
return null;
}
}
return containers;
}
return null;
}
public void MoveItems(IEnumerable<Item> items, ItemContainer to)
{
foreach (Item item in items)
{
item.MoveToContainer(to);
}
}
public ItemCategory StringToItemCategory(string categoryName)
{
string[] categoryNames = Enum.GetNames(typeof(ItemCategory));
for (int i = 0; i < categoryNames.Length; i++)
{
if (categoryName.ToLower().Equals(categoryNames[i].ToLower()))
{
return (ItemCategory)i;
}
}
return (ItemCategory)categoryNames.Length;
}
public bool IsContainerExcluded(BasePlayer player, BaseEntity entity)
{
if (entity is ShopFront || entity is BigWheelBettingTerminal || entity is NPCVendingMachine)
{
return true;
}
if (entity is VendingMachine)
{
VendingMachine vendingMachine = (VendingMachine)entity;
if (!vendingMachine.PlayerBehind(player))
{
return true;
}
}
if (entity is DropBox)
{
DropBox dropBox = (DropBox)entity;
if (!dropBox.PlayerBehind(player))
{
return true;
}
}
if (entity is IItemContainerEntity)
{
IItemContainerEntity container = (IItemContainerEntity)entity;
if (container.inventory.IsLocked())
{
return true;
}
}
if (_cacheContainersExcluded.Contains(entity.prefabID))
{
return true;
}
if (Interface.CallHook("QuickSortExcluded", player, entity) != null)
{
return true;
}
return false;
}
public static bool IsOwnerSleeper(ItemContainer container)
{
BasePlayer playerOwner = container.playerOwner;
if ((object)playerOwner == null || !IsPlayerContainer(container, playerOwner))
return false;
return playerOwner.IsSleeping();
}
public static bool IsPlayerContainer(ItemContainer container, BasePlayer player)
{
return player.inventory.containerMain == container
|| player.inventory.containerBelt == container
|| player.inventory.containerWear == container;
}
#endregion Loot Handling
#region Helpers
public void UnsubscribeHooks()
{
Unsubscribe(nameof(OnEntityDeath));
Unsubscribe(nameof(OnEntityKill));
Unsubscribe(nameof(OnLootEntity));
Unsubscribe(nameof(OnLootPlayer));
Unsubscribe(nameof(OnPlayerLootEnd));
Unsubscribe(nameof(OnPlayerSleep));
}
public void SubscribeHooks()
{
//Subscribe(nameof(OnEntityDeath));
//Subscribe(nameof(OnEntityKill));
Subscribe(nameof(OnLootEntity));
Subscribe(nameof(OnLootPlayer));
Subscribe(nameof(OnPlayerLootEnd));
//Subscribe(nameof(OnPlayerSleep));
}
public void RegisterPermissions()
{
permission.RegisterPermission(PermissionAutoLootAll, this);
permission.RegisterPermission(PermissionLootAll, this);
permission.RegisterPermission(PermissionUse, this);
}
public void AddCommands()
{
if (_configData.GlobalSettings.Commands.Length == 0)
{
_configData.GlobalSettings.Commands = new[] { "qs" };
SaveConfig();
}
foreach (string command in _configData.GlobalSettings.Commands)
{
cmd.AddChatCommand(command, this, nameof(CmdQuickSort));
}
}
public void CreateCache()
{
_defaultPlayerData = new PlayerData()
{
Enabled = _configData.GlobalSettings.DefaultEnabled,
AutoLootAll = _configData.GlobalSettings.AutoLootAll,
UiStyle = _configData.GlobalSettings.DefaultUiStyle,
Containers = _configData.GlobalSettings.Containers,