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

Last change on this file since 309 was 297, checked in by alloc, 8 years ago

Fixes

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