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

Last change on this file since 294 was 273, checked in by alloc, 9 years ago

fixes 8_10_13_1

File size: 6.5 KB
RevLine 
[224]1using AllocsFixes.PersistentData;
2using System;
3using System.Collections.Generic;
4
5namespace AllocsFixes.CustomCommands
6{
[273]7 public class ShowInventory : ConsoleCmdAbstract
8 {
[253]9
[273]10 public override string GetDescription ()
11 {
[238]12 return "list inventory of a given player";
[224]13 }
14
[273]15 public override string GetHelp ()
16 {
[238]17 return "Usage:\n" +
[273]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.";
[238]25 }
26
[273]27 public override string[] GetCommands ()
28 {
[224]29 return new string[] { "showinventory", "si" };
30 }
31
[273]32 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo)
33 {
[224]34 try {
[230]35 if (_params.Count < 1) {
[273]36 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
[224]37 return;
38 }
39
40 string steamid = PersistentContainer.Instance.Players.GetSteamID (_params [0], true);
41 if (steamid == null) {
[230]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).");
[224]43 return;
44 }
45
[273]46 string tag = null;
47 if (_params.Count > 1 && _params [1].Length > 0) {
48 tag = _params [1];
49 }
50
[233]51 Player p = PersistentContainer.Instance.Players [steamid, false];
[224]52 PersistentData.Inventory inv = p.Inventory;
53
[273]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);
[250]59
[273]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);
[250]65
[273]66 if (tag == null)
67 SdtdConsole.Instance.Output ("Equipment of player " + p.Name + ":");
68 PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
[250]69
[224]70 } catch (Exception e) {
71 Log.Out ("Error in ShowInventory.Run: " + e);
72 }
73 }
[250]74
[273]75 private void PrintInv (List<InvItem> _inv, int _entityId, string _location, string _tag)
76 {
[250]77 for (int i = 0; i < _inv.Count; i++) {
78 if (_inv [i] != null) {
[273]79 if (_tag == null) { // no Tag defined -> readable output
80 if (_inv [i].quality < 0) {
81 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2}", i, _inv [i].count, _inv [i].itemName));
82 } else {
83 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2} - quality: {3}", i, _inv [i].count, _inv [i].itemName, _inv [i].quality));
84 }
85 DoParts (_inv [i].parts, 1, null);
86 } else { // Tag defined -> parseable output
87 String partsMsg = DoParts(_inv[i].parts, 1, "");
88 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 + ")";
89 SdtdConsole.Instance.Output(msg);
[250]90 }
91 }
92 }
93 }
94
[273]95 private void PrintEquipment (InvItem[] _equipment, int _entityId, string _location, string _tag)
96 {
97 AddEquipment ("head", _equipment, EquipmentSlots.Headgear, _entityId, _location, _tag);
98 AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
99 AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
[250]100
[273]101 AddEquipment ("armor", _equipment, EquipmentSlots.ChestArmor, _entityId, _location, _tag);
102 AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
103 AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
[250]104
[273]105 AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
106 AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
107 AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
[250]108
[273]109 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
[250]110 }
111
[273]112 private void AddEquipment (string _slotname, InvItem[] _items, EquipmentSlots _slot, int _entityId, string _location, string _tag)
113 {
[253]114 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
115
116 for (int i = 0; i < slotindices.Length; i++) {
117 if (_items != null && _items [slotindices [i]] != null) {
118 InvItem item = _items [slotindices [i]];
[273]119 if (_tag == null) { // no Tag defined -> readable output
120 if (item.quality < 0) {
121 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000}", _slotname, item.itemName));
122 } else {
123 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000} - quality: {2}", _slotname, item.itemName, item.quality));
124 }
125 DoParts (_items [slotindices [i]].parts, 1, null);
126 } else { // Tag defined -> parseable output
127 String partsMsg = DoParts(_items[slotindices[i]].parts, 1, "");
128 String msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location + ", slot=" + _slotname + ", item=" + item.itemName + ", qnty=1, quality=" + item.quality + ", parts=(" + partsMsg + ")";
129 SdtdConsole.Instance.Output(msg);
[253]130 }
131 return;
[250]132 }
133 }
134 }
135
[273]136 private string DoParts (InvItem[] _parts, int _indent, string _currentMessage)
137 {
[250]138 if (_parts != null && _parts.Length > 0) {
139 string indenter = new string (' ', _indent * 4);
140 for (int i = 0; i < _parts.Length; i++) {
141 if (_parts [i] != null) {
[273]142 if (_currentMessage == null) { // no currentMessage given -> readable output
143 if (_parts [i].quality < 0) {
144 SdtdConsole.Instance.Output (string.Format ("{0} - {1}", indenter, _parts [i].itemName));
145 } else {
146 SdtdConsole.Instance.Output (string.Format ("{0} - {1} - quality: {2}", indenter, _parts [i].itemName, _parts [i].quality));
147 }
148 DoParts (_parts [i].parts, _indent + 1, _currentMessage);
149 } else { // currentMessage given -> parseable output
150 if (_currentMessage.Length > 0) {
151 _currentMessage += ",";
152 }
153 _currentMessage += _parts[i].itemName + "@" + _parts[i].quality;
154 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
[250]155 }
156 }
157 }
158 }
[273]159 return _currentMessage;
[250]160 }
161
[224]162 }
163}
Note: See TracBrowser for help on using the repository browser.