|
| 1 | +using FFXIVClientStructs.FFXIV.Client.Game; |
| 2 | +using SomethingNeedDoing.Core.Interfaces; |
| 3 | + |
| 4 | +namespace SomethingNeedDoing.LuaMacro.Modules; |
| 5 | +public class InventoryModule : LuaModuleBase |
| 6 | +{ |
| 7 | + public override string ModuleName => "Inventory"; |
| 8 | + |
| 9 | + [LuaFunction] public InventoryContainerWrapper GetInventoryContainer(InventoryType container) => new(container); |
| 10 | + [LuaFunction] public InventoryItemWrapper GetInventoryItem(InventoryType container, int slot) => new(container, slot); |
| 11 | + |
| 12 | + [LuaFunction] |
| 13 | + public unsafe InventoryItemWrapper? GetInventoryItem(uint itemId) |
| 14 | + { |
| 15 | + foreach (var type in Enum.GetValues<InventoryType>()) |
| 16 | + { |
| 17 | + var container = InventoryManager.Instance()->GetInventoryContainer(type); |
| 18 | + if (container == null) continue; |
| 19 | + for (var i = 0; i < container->Size; i++) |
| 20 | + if (container->Items[i].ItemId == itemId) |
| 21 | + return new(container, i); |
| 22 | + } |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + public unsafe class InventoryContainerWrapper(InventoryType container) : IWrapper |
| 27 | + { |
| 28 | + private readonly InventoryContainer* _container = InventoryManager.Instance()->GetInventoryContainer(container); |
| 29 | + [LuaDocs] public uint Count => _container->Size; |
| 30 | + |
| 31 | + [LuaDocs] |
| 32 | + public int FreeSlots |
| 33 | + { |
| 34 | + get |
| 35 | + { |
| 36 | + var count = 0; |
| 37 | + for (var i = 0; i < Count; i++) |
| 38 | + if (_container->Items[i].ItemId == 0) |
| 39 | + count++; |
| 40 | + return count; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [LuaDocs] |
| 45 | + public List<InventoryItemWrapper> Items |
| 46 | + { |
| 47 | + get |
| 48 | + { |
| 49 | + List<InventoryItemWrapper> list = []; |
| 50 | + for (var i = 0; i < Count; i++) |
| 51 | + if (_container->Items[i].ItemId != 0) |
| 52 | + list.Add(new(_container, i)); |
| 53 | + return list; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [LuaDocs] public InventoryItemWrapper this[int index] => new(_container, index); |
| 58 | + } |
| 59 | + |
| 60 | + public unsafe class InventoryItemWrapper : IWrapper |
| 61 | + { |
| 62 | + private InventoryItem* Item { get; set; } |
| 63 | + public InventoryItemWrapper(InventoryType container, int slot) => Item = InventoryManager.Instance()->GetInventoryContainer(container)->GetInventorySlot(slot); |
| 64 | + public InventoryItemWrapper(InventoryContainer* container, int slot) => Item = container->GetInventorySlot(slot); |
| 65 | + public InventoryItemWrapper(InventoryItem* item) => Item = item; |
| 66 | + |
| 67 | + [LuaDocs] public uint ItemId => Item->ItemId; |
| 68 | + [LuaDocs] public uint BaseItemId => Item->GetBaseItemId(); |
| 69 | + [LuaDocs] public int Count => Item->Quantity; |
| 70 | + [LuaDocs] public ushort SpiritbondOrCollectability => Item->SpiritbondOrCollectability; |
| 71 | + [LuaDocs] public ushort Condition => Item->Condition; |
| 72 | + [LuaDocs] public uint GlamourId => Item->GlamourId; |
| 73 | + [LuaDocs] public bool IsHighQuality => Item->IsHighQuality(); |
| 74 | + [LuaDocs] public InventoryItemWrapper? LinkedItem => Item->GetLinkedItem() is not null ? new(Item->GetLinkedItem()) : null; |
| 75 | + |
| 76 | + [LuaDocs] public InventoryType Container => Item->Container; |
| 77 | + [LuaDocs] public int Slot => Item->Slot; |
| 78 | + |
| 79 | + [LuaDocs] public void Use() => Game.UseItem(ItemId, IsHighQuality); |
| 80 | + } |
| 81 | +} |
0 commit comments