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

Last change on this file since 452 was 446, checked in by alloc, 17 months ago

24_27_41

File size: 6.7 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.PersistentData;
3
4namespace AllocsFixes.CustomCommands {
5 public class ShowInventory : ConsoleCmdAbstract {
6 protected override string getDescription () {
7 return "list inventory of a given player";
8 }
9
10 protected 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 protected 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 Player p = PersistentContainer.Instance.Players.GetByString (_params [0], true);
32 if (p == 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 PersistentData.Inventory inv = p.Inventory;
44
45 if (tag == null) {
46 SdtdConsole.Instance.Output ("Belt of player " + p.Name + ":");
47 }
48
49 PrintInv (inv.belt, p.EntityID, "belt", tag);
50 if (tag == null) {
51 SdtdConsole.Instance.Output (string.Empty);
52 }
53
54 if (tag == null) {
55 SdtdConsole.Instance.Output ("Bagpack of player " + p.Name + ":");
56 }
57
58 PrintInv (inv.bag, p.EntityID, "backpack", tag);
59 if (tag == null) {
60 SdtdConsole.Instance.Output (string.Empty);
61 }
62
63 if (tag == null) {
64 SdtdConsole.Instance.Output ("Equipment of player " + p.Name + ":");
65 }
66
67 PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
68
69 if (tag != null) {
70 SdtdConsole.Instance.Output ("tracker_item id=" + p.EntityID + ", tag=" + tag +
71 ", SHOWINVENTORY DONE");
72 }
73 }
74
75 private void PrintInv (List<InvItem> _inv, int _entityId, string _location, string _tag) {
76 for (int i = 0; i < _inv.Count; i++) {
77 if (_inv [i] != null) {
78 if (_tag == null) {
79 // no Tag defined -> readable output
80 if (_inv [i].quality < 0) {
81 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2}", i,
82 _inv [i].count, _inv [i].itemName));
83 } else {
84 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2} - quality: {3}", i,
85 _inv [i].count, _inv [i].itemName, _inv [i].quality));
86 }
87
88 DoParts (_inv [i].parts, 1, null);
89 } else {
90 // Tag defined -> parseable output
91 string partsMsg = DoParts (_inv [i].parts, 1, "");
92 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
93 ", slot=" + i + ", item=" + _inv [i].itemName + ", qnty=" + _inv [i].count +
94 ", quality=" + _inv [i].quality + ", parts=(" + partsMsg + ")";
95 SdtdConsole.Instance.Output (msg);
96 }
97 }
98 }
99 }
100
101 private void PrintEquipment (InvItem[] _equipment, int _entityId, string _location, string _tag) {
102 AddEquipment ("head", _equipment, EquipmentSlots.Headgear, _entityId, _location, _tag);
103 AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
104 AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
105
106 AddEquipment ("armor", _equipment, EquipmentSlots.ChestArmor, _entityId, _location, _tag);
107 AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
108 AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
109
110 AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
111 AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
112 AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
113
114 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
115 }
116
117 private void AddEquipment (string _slotname, InvItem[] _items, EquipmentSlots _slot, int _entityId,
118 string _location, string _tag) {
119 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
120
121 for (int i = 0; i < slotindices.Length; i++) {
122 if (_items != null && _items [slotindices [i]] != null) {
123 InvItem item = _items [slotindices [i]];
124 if (_tag == null) {
125 // no Tag defined -> readable output
126 if (item.quality < 0) {
127 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000}", _slotname,
128 item.itemName));
129 } else {
130 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000} - quality: {2}",
131 _slotname, item.itemName, item.quality));
132 }
133
134 DoParts (_items [slotindices [i]].parts, 1, null);
135 } else {
136 // Tag defined -> parseable output
137 string partsMsg = DoParts (_items [slotindices [i]].parts, 1, "");
138 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
139 ", slot=" + _slotname + ", item=" + item.itemName + ", qnty=1, quality=" +
140 item.quality + ", parts=(" + partsMsg + ")";
141 SdtdConsole.Instance.Output (msg);
142 }
143
144 return;
145 }
146 }
147 }
148
149 private string DoParts (InvItem[] _parts, int _indent, string _currentMessage) {
150 if (_parts != null && _parts.Length > 0) {
151 string indenter = new string (' ', _indent * 4);
152 for (int i = 0; i < _parts.Length; i++) {
153 if (_parts [i] != null) {
154 if (_currentMessage == null) {
155 // no currentMessage given -> readable output
156 if (_parts [i].quality < 0) {
157 SdtdConsole.Instance.Output (string.Format ("{0} - {1}", indenter,
158 _parts [i].itemName));
159 } else {
160 SdtdConsole.Instance.Output (string.Format ("{0} - {1} - quality: {2}",
161 indenter, _parts [i].itemName, _parts [i].quality));
162 }
163
164 DoParts (_parts [i].parts, _indent + 1, null);
165 } else {
166 // currentMessage given -> parseable output
167 if (_currentMessage.Length > 0) {
168 _currentMessage += ",";
169 }
170
171 _currentMessage += _parts [i].itemName + "@" + _parts [i].quality;
172 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
173 }
174 }
175 }
176 }
177
178 return _currentMessage;
179 }
180 }
181}
Note: See TracBrowser for help on using the repository browser.