source: binary-improvements2/AllocsCommands/Commands/ShowInventory.cs@ 385

Last change on this file since 385 was 383, checked in by alloc, 2 years ago

Fixed a bunch of warnings

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