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

Last change on this file since 457 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 6.7 KB
RevLine 
[224]1using System.Collections.Generic;
[325]2using AllocsFixes.PersistentData;
[455]3using JetBrains.Annotations;
[224]4
[325]5namespace AllocsFixes.CustomCommands {
[455]6 [UsedImplicitly]
[325]7 public class ShowInventory : ConsoleCmdAbstract {
[420]8 protected override string getDescription () {
[238]9 return "list inventory of a given player";
[224]10 }
11
[420]12 protected override string getHelp () {
[238]13 return "Usage:\n" +
[369]14 " showinventory <user id / player name / entity id> [tag]\n" +
15 "Show the inventory of the player given by his UserID, player name or\n" +
[325]16 "entity id (as given by e.g. \"lpi\").\n" +
17 "Optionally specify a tag that is included in each line of the output. In\n" +
18 "this case output is designed to be easily parseable by tools.\n" +
19 "Note: This only shows the player's inventory after it was first sent to\n" +
20 "the server which happens at least every 30 seconds.";
[238]21 }
22
[420]23 protected override string[] getCommands () {
[325]24 return new[] {"showinventory", "si"};
[224]25 }
26
[325]27 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
[359]28 if (_params.Count < 1) {
29 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
30 return;
31 }
[224]32
[446]33 Player p = PersistentContainer.Instance.Players.GetByString (_params [0], true);
34 if (p == null) {
[359]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 }
[224]39
[359]40 string tag = null;
41 if (_params.Count > 1 && _params [1].Length > 0) {
42 tag = _params [1];
43 }
[273]44
[359]45 PersistentData.Inventory inv = p.Inventory;
[224]46
[359]47 if (tag == null) {
[455]48 SdtdConsole.Instance.Output ($"Belt of player {p.Name}:");
[359]49 }
[325]50
[359]51 PrintInv (inv.belt, p.EntityID, "belt", tag);
52 if (tag == null) {
53 SdtdConsole.Instance.Output (string.Empty);
54 }
[250]55
[359]56 if (tag == null) {
[455]57 SdtdConsole.Instance.Output ($"Bagpack of player {p.Name}:");
[359]58 }
[325]59
[359]60 PrintInv (inv.bag, p.EntityID, "backpack", tag);
61 if (tag == null) {
62 SdtdConsole.Instance.Output (string.Empty);
63 }
[250]64
[359]65 if (tag == null) {
[455]66 SdtdConsole.Instance.Output ($"Equipment of player {p.Name}:");
[359]67 }
[325]68
[359]69 PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
[250]70
[359]71 if (tag != null) {
[455]72 SdtdConsole.Instance.Output ($"tracker_item id={p.EntityID}, tag={tag}, SHOWINVENTORY DONE");
[224]73 }
74 }
[250]75
[455]76 private static void PrintInv (IReadOnlyList<InvItem> _inv, int _entityId, string _location, string _tag) {
[250]77 for (int i = 0; i < _inv.Count; i++) {
[455]78 if (_inv[i] == null) {
79 continue;
80 }
[325]81
[455]82 if (_tag == null) {
83 // no Tag defined -> readable output
84 if (_inv [i].quality < 0) {
85 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2}", i,
86 _inv [i].count, _inv [i].itemName));
[325]87 } else {
[455]88 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2} - quality: {3}", i,
89 _inv [i].count, _inv [i].itemName, _inv [i].quality));
[250]90 }
[455]91
92 DoParts (_inv [i].parts, 1, null);
93 } else {
94 // Tag defined -> parseable output
95 string partsMsg = DoParts (_inv [i].parts, 1, "");
96 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
97 ", slot=" + i + ", item=" + _inv [i].itemName + ", qnty=" + _inv [i].count +
98 ", quality=" + _inv [i].quality + ", parts=(" + partsMsg + ")";
99 SdtdConsole.Instance.Output (msg);
[250]100 }
101 }
102 }
103
[455]104 private static void PrintEquipment (IReadOnlyList<InvItem> _equipment, int _entityId, string _location, string _tag) {
[273]105 AddEquipment ("head", _equipment, EquipmentSlots.Headgear, _entityId, _location, _tag);
106 AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
107 AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
[250]108
[273]109 AddEquipment ("armor", _equipment, EquipmentSlots.ChestArmor, _entityId, _location, _tag);
110 AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
111 AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
[250]112
[273]113 AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
114 AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
115 AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
[250]116
[273]117 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
[250]118 }
119
[455]120 private static void AddEquipment (string _slotname, IReadOnlyList<InvItem> _items, EquipmentSlots _slot, int _entityId,
[325]121 string _location, string _tag) {
[253]122 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
123
124 for (int i = 0; i < slotindices.Length; i++) {
[455]125 if (_items == null || _items[slotindices[i]] == null) {
126 continue;
127 }
[325]128
[455]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));
[325]135 } else {
[455]136 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000} - quality: {2}",
137 _slotname, item.itemName, item.quality));
[253]138 }
[325]139
[455]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}, slot={_slotname}, item={item.itemName}, qnty=1, quality={item.quality}, parts=({partsMsg})";
145 SdtdConsole.Instance.Output (msg);
[250]146 }
[455]147
148 return;
[250]149 }
150 }
151
[455]152 private static string DoParts (IReadOnlyList<InvItem> _parts, int _indent, string _currentMessage) {
153 if (_parts == null || _parts.Count <= 0) {
154 return _currentMessage;
155 }
[325]156
[455]157 string indenter = new string (' ', _indent * 4);
158 for (int i = 0; i < _parts.Count; i++) {
159 if (_parts[i] == null) {
160 continue;
161 }
[325]162
[455]163 if (_currentMessage == null) {
164 // no currentMessage given -> readable output
165 if (_parts [i].quality < 0) {
166 SdtdConsole.Instance.Output (string.Format ("{0} - {1}", indenter,
167 _parts [i].itemName));
168 } else {
169 SdtdConsole.Instance.Output (string.Format ("{0} - {1} - quality: {2}",
170 indenter, _parts [i].itemName, _parts [i].quality));
[250]171 }
[455]172
173 DoParts (_parts [i].parts, _indent + 1, null);
174 } else {
175 // currentMessage given -> parseable output
176 if (_currentMessage.Length > 0) {
177 _currentMessage += ",";
178 }
179
180 _currentMessage += $"{_parts[i].itemName}@{_parts[i].quality}";
181 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
[250]182 }
183 }
[325]184
[273]185 return _currentMessage;
[250]186 }
[224]187 }
[325]188}
Note: See TracBrowser for help on using the repository browser.