source: binary-improvements2/CommandExtensions/src/Commands/ShowInventory.cs@ 419

Last change on this file since 419 was 405, checked in by alloc, 21 months ago

Refactored console commands for A21 caching

File size: 6.5 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.PersistentData;
3using JetBrains.Annotations;
4
5namespace CommandExtensions.Commands {
6 [UsedImplicitly]
7 public class ShowInventory : ConsoleCmdAbstract {
8 protected override string getDescription () {
9 return "list inventory of a given player";
10 }
11
12 protected override string getHelp () {
13 return "Usage:\n" +
14 " showinventory <user id / player name / entity id> [tag]\n" +
15 "Show the inventory of the player given by his UserID, player name or\n" +
16 "entity id (as given by e.g. \"lpi\").\n" +
17 "Optionally specify a tag that is included in each line of the output. In\n" +
18 "this case output is designed to be easily parseable by tools.\n" +
19 "Note: This only shows the player's inventory after it was first sent to\n" +
20 "the server which happens at least every 30 seconds.";
21 }
22
23 protected override string[] getCommands () {
24 return new[] {"showinventory", "si"};
25 }
26
27 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
28 if (_params.Count < 1) {
29 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
30 return;
31 }
32
33 PlatformUserIdentifierAbs steamid = PersistentContainer.Instance.Players.GetSteamID (_params [0], true);
34 if (steamid == null) {
35 SdtdConsole.Instance.Output (
36 "Playername or entity/steamid id not found or no inventory saved (first saved after a player has been online for 30s).");
37 return;
38 }
39
40 string tag = null;
41 if (_params.Count > 1 && _params [1].Length > 0) {
42 tag = _params [1];
43 }
44
45 Player p = PersistentContainer.Instance.Players [steamid, false];
46 AllocsFixes.PersistentData.Inventory inv = p.Inventory;
47
48 if (tag == null) {
49 SdtdConsole.Instance.Output ($"Belt of player {p.Name}:");
50 }
51
52 PrintInv (inv.belt, p.EntityID, "belt", tag);
53 if (tag == null) {
54 SdtdConsole.Instance.Output (string.Empty);
55 }
56
57 if (tag == null) {
58 SdtdConsole.Instance.Output ($"Bagpack of player {p.Name}:");
59 }
60
61 PrintInv (inv.bag, p.EntityID, "backpack", tag);
62 if (tag == null) {
63 SdtdConsole.Instance.Output (string.Empty);
64 }
65
66 if (tag == null) {
67 SdtdConsole.Instance.Output ($"Equipment of player {p.Name}:");
68 }
69
70 PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
71
72 if (tag != null) {
73 SdtdConsole.Instance.Output ($"tracker_item id={p.EntityID}, tag={tag}, SHOWINVENTORY DONE");
74 }
75 }
76
77 private void PrintInv (List<InvItem> _inv, int _entityId, string _location, string _tag) {
78 for (int i = 0; i < _inv.Count; i++) {
79 if (_inv [i] == null) {
80 continue;
81 }
82
83 if (_tag == null) {
84 // no Tag defined -> readable output
85 if (_inv [i].quality < 0) {
86 SdtdConsole.Instance.Output ($" Slot {i}: {_inv [i].count:000} * {_inv [i].itemName}");
87 } else {
88 SdtdConsole.Instance.Output ($" Slot {i}: {_inv [i].count:000} * {_inv [i].itemName} - quality: {_inv [i].quality}");
89 }
90
91 DoParts (_inv [i].parts, 1, null);
92 } else {
93 // Tag defined -> parseable output
94 string partsMsg = DoParts (_inv [i].parts, 1, "");
95 string msg =
96 $"tracker_item id={_entityId}, tag={_tag}, location={_location}, slot={i}, item={_inv [i].itemName}, qnty={_inv [i].count}, quality={_inv [i].quality}, parts=({partsMsg})";
97 SdtdConsole.Instance.Output (msg);
98 }
99 }
100 }
101
102 private void PrintEquipment (InvItem[] _equipment, int _entityId, string _location, string _tag) {
103 AddEquipment ("head", _equipment, EquipmentSlots.Headgear, _entityId, _location, _tag);
104 AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
105 AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
106
107 AddEquipment ("armor", _equipment, EquipmentSlots.ChestArmor, _entityId, _location, _tag);
108 AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
109 AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
110
111 AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
112 AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
113 AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
114
115 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
116 }
117
118 private void AddEquipment (string _slotname, InvItem[] _items, EquipmentSlots _slot, int _entityId,
119 string _location, string _tag) {
120 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
121
122 for (int i = 0; i < slotindices.Length; i++) {
123 if (_items? [slotindices [i]] == null) {
124 continue;
125 }
126
127 InvItem item = _items [slotindices [i]];
128 if (_tag == null) {
129 // no Tag defined -> readable output
130 if (item.quality < 0) {
131 SdtdConsole.Instance.Output ($" Slot {_slotname:8}: {item.itemName:000}");
132 } else {
133 SdtdConsole.Instance.Output ($" Slot {_slotname:8}: {item.itemName:000} - quality: {item.quality}");
134 }
135
136 DoParts (_items [slotindices [i]].parts, 1, null);
137 } else {
138 // Tag defined -> parseable output
139 string partsMsg = DoParts (_items [slotindices [i]].parts, 1, "");
140 string msg =
141 $"tracker_item id={_entityId}, tag={_tag}, location={_location}, slot={_slotname}, item={item.itemName}, qnty=1, quality={item.quality}, parts=({partsMsg})";
142 SdtdConsole.Instance.Output (msg);
143 }
144
145 return;
146 }
147 }
148
149 private string DoParts (InvItem[] _parts, int _indent, string _currentMessage) {
150 if (_parts == null || _parts.Length <= 0) {
151 return _currentMessage;
152 }
153
154 string indenter = new string (' ', _indent * 4);
155 for (int i = 0; i < _parts.Length; i++) {
156 if (_parts [i] == null) {
157 continue;
158 }
159
160 if (_currentMessage == null) {
161 // no currentMessage given -> readable output
162 if (_parts [i].quality < 0) {
163 SdtdConsole.Instance.Output ($"{indenter} - {_parts [i].itemName}");
164 } else {
165 SdtdConsole.Instance.Output ($"{indenter} - {_parts [i].itemName} - quality: {_parts [i].quality}");
166 }
167
168 DoParts (_parts [i].parts, _indent + 1, _currentMessage);
169 } else {
170 // currentMessage given -> parseable output
171 if (_currentMessage.Length > 0) {
172 _currentMessage += ",";
173 }
174
175 _currentMessage += $"{_parts [i].itemName}@{_parts [i].quality}";
176 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
177 }
178 }
179
180 return _currentMessage;
181 }
182 }
183}
Note: See TracBrowser for help on using the repository browser.