source: binary-improvements/AllocsCommands/Commands/ShowInventory.cs@ 328

Last change on this file since 328 was 325, checked in by alloc, 6 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

File size: 6.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes.CustomCommands {
6 public class ShowInventory : ConsoleCmdAbstract {
7 public override string GetDescription () {
8 return "list inventory of a given player";
9 }
10
11 public override string GetHelp () {
12 return "Usage:\n" +
13 " showinventory <steam id / player name / entity id> [tag]\n" +
14 "Show the inventory of the player given by his SteamID, player name or\n" +
15 "entity id (as given by e.g. \"lpi\").\n" +
16 "Optionally specify a tag that is included in each line of the output. In\n" +
17 "this case output is designed to be easily parseable by tools.\n" +
18 "Note: This only shows the player's inventory after it was first sent to\n" +
19 "the server which happens at least every 30 seconds.";
20 }
21
22 public override string[] GetCommands () {
23 return new[] {"showinventory", "si"};
24 }
25
26 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
27 try {
28 if (_params.Count < 1) {
29 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
30 return;
31 }
32
33 string 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 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 +
74 ", SHOWINVENTORY DONE");
75 }
76 } catch (Exception e) {
77 Log.Out ("Error in ShowInventory.Run: " + e);
78 }
79 }
80
81 private void PrintInv (List<InvItem> _inv, int _entityId, string _location, string _tag) {
82 for (int i = 0; i < _inv.Count; i++) {
83 if (_inv [i] != null) {
84 if (_tag == null) {
85 // no Tag defined -> readable output
86 if (_inv [i].quality < 0) {
87 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2}", i,
88 _inv [i].count, _inv [i].itemName));
89 } else {
90 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2} - quality: {3}", i,
91 _inv [i].count, _inv [i].itemName, _inv [i].quality));
92 }
93
94 DoParts (_inv [i].parts, 1, null);
95 } else {
96 // Tag defined -> parseable output
97 string partsMsg = DoParts (_inv [i].parts, 1, "");
98 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
99 ", slot=" + i + ", item=" + _inv [i].itemName + ", qnty=" + _inv [i].count +
100 ", quality=" + _inv [i].quality + ", parts=(" + partsMsg + ")";
101 SdtdConsole.Instance.Output (msg);
102 }
103 }
104 }
105 }
106
107 private void PrintEquipment (InvItem[] _equipment, int _entityId, string _location, string _tag) {
108 AddEquipment ("head", _equipment, EquipmentSlots.Headgear, _entityId, _location, _tag);
109 AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
110 AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
111
112 AddEquipment ("armor", _equipment, EquipmentSlots.ChestArmor, _entityId, _location, _tag);
113 AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
114 AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
115
116 AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
117 AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
118 AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
119
120 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
121 }
122
123 private void AddEquipment (string _slotname, InvItem[] _items, EquipmentSlots _slot, int _entityId,
124 string _location, string _tag) {
125 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
126
127 for (int i = 0; i < slotindices.Length; i++) {
128 if (_items != null && _items [slotindices [i]] != null) {
129 InvItem item = _items [slotindices [i]];
130 if (_tag == null) {
131 // no Tag defined -> readable output
132 if (item.quality < 0) {
133 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000}", _slotname,
134 item.itemName));
135 } else {
136 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000} - quality: {2}",
137 _slotname, item.itemName, item.quality));
138 }
139
140 DoParts (_items [slotindices [i]].parts, 1, null);
141 } else {
142 // Tag defined -> parseable output
143 string partsMsg = DoParts (_items [slotindices [i]].parts, 1, "");
144 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
145 ", slot=" + _slotname + ", item=" + item.itemName + ", qnty=1, quality=" +
146 item.quality + ", parts=(" + partsMsg + ")";
147 SdtdConsole.Instance.Output (msg);
148 }
149
150 return;
151 }
152 }
153 }
154
155 private string DoParts (InvItem[] _parts, int _indent, string _currentMessage) {
156 if (_parts != null && _parts.Length > 0) {
157 string indenter = new string (' ', _indent * 4);
158 for (int i = 0; i < _parts.Length; i++) {
159 if (_parts [i] != null) {
160 if (_currentMessage == null) {
161 // no currentMessage given -> readable output
162 if (_parts [i].quality < 0) {
163 SdtdConsole.Instance.Output (string.Format ("{0} - {1}", indenter,
164 _parts [i].itemName));
165 } else {
166 SdtdConsole.Instance.Output (string.Format ("{0} - {1} - quality: {2}",
167 indenter, _parts [i].itemName, _parts [i].quality));
168 }
169
170 DoParts (_parts [i].parts, _indent + 1, _currentMessage);
171 } else {
172 // currentMessage given -> parseable output
173 if (_currentMessage.Length > 0) {
174 _currentMessage += ",";
175 }
176
177 _currentMessage += _parts [i].itemName + "@" + _parts [i].quality;
178 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
179 }
180 }
181 }
182 }
183
184 return _currentMessage;
185 }
186 }
187}
Note: See TracBrowser for help on using the repository browser.