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

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

Fixed a bunch of warnings

File size: 6.7 KB
RevLine 
[224]1using System.Collections.Generic;
[325]2using AllocsFixes.PersistentData;
[224]3
[325]4namespace AllocsFixes.CustomCommands {
5 public class ShowInventory : ConsoleCmdAbstract {
6 public override string GetDescription () {
[238]7 return "list inventory of a given player";
[224]8 }
9
[325]10 public override string GetHelp () {
[238]11 return "Usage:\n" +
[369]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" +
[325]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.";
[238]19 }
20
[325]21 public override string[] GetCommands () {
22 return new[] {"showinventory", "si"};
[224]23 }
24
[325]25 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
[359]26 if (_params.Count < 1) {
27 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
28 return;
29 }
[224]30
[369]31 PlatformUserIdentifierAbs steamid = PersistentContainer.Instance.Players.GetSteamID (_params [0], true);
[359]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 }
[224]37
[359]38 string tag = null;
39 if (_params.Count > 1 && _params [1].Length > 0) {
40 tag = _params [1];
41 }
[273]42
[359]43 Player p = PersistentContainer.Instance.Players [steamid, false];
44 PersistentData.Inventory inv = p.Inventory;
[224]45
[359]46 if (tag == null) {
47 SdtdConsole.Instance.Output ("Belt of player " + p.Name + ":");
48 }
[325]49
[359]50 PrintInv (inv.belt, p.EntityID, "belt", tag);
51 if (tag == null) {
52 SdtdConsole.Instance.Output (string.Empty);
53 }
[250]54
[359]55 if (tag == null) {
56 SdtdConsole.Instance.Output ("Bagpack of player " + p.Name + ":");
57 }
[325]58
[359]59 PrintInv (inv.bag, p.EntityID, "backpack", tag);
60 if (tag == null) {
61 SdtdConsole.Instance.Output (string.Empty);
62 }
[250]63
[359]64 if (tag == null) {
65 SdtdConsole.Instance.Output ("Equipment of player " + p.Name + ":");
66 }
[325]67
[359]68 PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
[250]69
[359]70 if (tag != null) {
71 SdtdConsole.Instance.Output ("tracker_item id=" + p.EntityID + ", tag=" + tag +
72 ", SHOWINVENTORY DONE");
[224]73 }
74 }
[250]75
[325]76 private void PrintInv (List<InvItem> _inv, int _entityId, string _location, string _tag) {
[250]77 for (int i = 0; i < _inv.Count; i++) {
78 if (_inv [i] != null) {
[325]79 if (_tag == null) {
80 // no Tag defined -> readable output
[273]81 if (_inv [i].quality < 0) {
[325]82 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2}", i,
83 _inv [i].count, _inv [i].itemName));
[273]84 } else {
[325]85 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2} - quality: {3}", i,
86 _inv [i].count, _inv [i].itemName, _inv [i].quality));
[273]87 }
[325]88
[273]89 DoParts (_inv [i].parts, 1, null);
[325]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);
[250]97 }
98 }
99 }
100 }
101
[325]102 private void PrintEquipment (InvItem[] _equipment, int _entityId, string _location, string _tag) {
[273]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);
[250]106
[273]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);
[250]110
[273]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);
[250]114
[273]115 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
[250]116 }
117
[325]118 private void AddEquipment (string _slotname, InvItem[] _items, EquipmentSlots _slot, int _entityId,
119 string _location, string _tag) {
[253]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]];
[325]125 if (_tag == null) {
126 // no Tag defined -> readable output
[273]127 if (item.quality < 0) {
[383]128 SdtdConsole.Instance.Output ($" Slot {_slotname:8}: {item.itemName:000}");
[273]129 } else {
[383]130 SdtdConsole.Instance.Output ($" Slot {_slotname:8}: {item.itemName:000} - quality: {item.quality}");
[273]131 }
[325]132
[273]133 DoParts (_items [slotindices [i]].parts, 1, null);
[325]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);
[253]141 }
[325]142
[253]143 return;
[250]144 }
145 }
146 }
147
[325]148 private string DoParts (InvItem[] _parts, int _indent, string _currentMessage) {
[250]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) {
[325]153 if (_currentMessage == null) {
154 // no currentMessage given -> readable output
[273]155 if (_parts [i].quality < 0) {
[383]156 SdtdConsole.Instance.Output ($"{indenter} - {_parts [i].itemName}");
[273]157 } else {
[383]158 SdtdConsole.Instance.Output ($"{indenter} - {_parts [i].itemName} - quality: {_parts [i].quality}");
[273]159 }
[325]160
[273]161 DoParts (_parts [i].parts, _indent + 1, _currentMessage);
[325]162 } else {
163 // currentMessage given -> parseable output
[273]164 if (_currentMessage.Length > 0) {
165 _currentMessage += ",";
166 }
[325]167
168 _currentMessage += _parts [i].itemName + "@" + _parts [i].quality;
[273]169 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
[250]170 }
171 }
172 }
173 }
[325]174
[273]175 return _currentMessage;
[250]176 }
[224]177 }
[325]178}
Note: See TracBrowser for help on using the repository browser.