From 72f698f95ead15875ea4e330da5d9d36520873bd Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 16:53:13 +0100 Subject: [PATCH 1/7] Getting content of specific bags only --- nampower/item_scripts.cpp | 143 +++++++++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 42 deletions(-) diff --git a/nampower/item_scripts.cpp b/nampower/item_scripts.cpp index b8f0354..6fbeda1 100644 --- a/nampower/item_scripts.cpp +++ b/nampower/item_scripts.cpp @@ -767,6 +767,8 @@ namespace Nampower { } } + + uint32_t Script_GetBagItem(uintptr_t *luaState) { luaState = GetLuaStatePtr(); @@ -890,62 +892,57 @@ namespace Nampower { return 1; } - uint32_t Script_GetBagItems(uintptr_t *luaState) { - luaState = GetLuaStatePtr(); - + uint32_t GetSingleBagItems(uintptr_t *luaState, int32_t bagIndex) { auto const getContainerGuid = reinterpret_cast(Offsets::GetContainerGuid); auto const getBagItem = reinterpret_cast(Offsets::CGBag_C_GetItemAtSlot); - // Get or create reusable main table - GetTableRef(luaState, bagItemsTableRef); - auto playerGuid = game::ClntObjMgrGetActivePlayerGuid(); - auto player = game::GetObjectPtr(playerGuid); - auto inventory = game::GetPlayerInventoryPtr(player); + if (bagIndex == 0) { + const auto playerGuid = game::ClntObjMgrGetActivePlayerGuid(); + const auto player = game::GetObjectPtr(playerGuid); + const auto inventory = game::GetPlayerInventoryPtr(player); + GetTableRef(luaState, bagTableRefs[0]); - // Backpack (bag 0) - lua_pushnumber(luaState, static_cast(0)); - GetTableRef(luaState, bagTableRefs[0]); - - for (uint32_t slot = 23; slot <= 38; slot++) { - auto item = getBagItem(inventory, slot); - uint32_t adjustedSlot = slot - 0x17; - if (item) { - PushBagCGItemToTable(luaState, 0, slot, item); - } else { - // Check if item was removed from this slot - ClearBagSlot(luaState, 0, adjustedSlot); + for (uint32_t slot = 23; slot <= 38; slot++) { + auto item = getBagItem(inventory, slot); + uint32_t adjustedSlot = slot - 0x17; + if (item) { + PushBagCGItemToTable(luaState, 0, slot, item); + } else { + // Check if item was removed from this slot + ClearBagSlot(luaState, 0, adjustedSlot); + } } + return 1; } - lua_settable(luaState, -3); - - // Bags 1-4 - for (int32_t bagIndex = 1; bagIndex <= 4; bagIndex++) { + if (bagIndex >= 1 && bagIndex <= 4) { uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 1-4 maps to container 0-3 if (containerGuid == 0) { // Bag slot is empty, clear all cached items for this bag - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); for (uint32_t slot = 0; slot < MAX_BAG_SLOTS; slot++) { ClearBagSlot(luaState, bagIndex, slot); } - lua_settable(luaState, -3); - continue; + return 1; } auto containerPtr = game::ClntObjMgrObjectPtr(game::TYPEMASK_CONTAINER, containerGuid); - if (!containerPtr) continue; + if (!containerPtr) { + return 0; + } auto bagPtr = GetBagPtrFromContainer(containerPtr); - if (!bagPtr) continue; + if (!bagPtr) { + return 0; + } - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); auto bagSize = *bagPtr; // Check all possible slots up to bag size - for (uint32_t slot = 0; slot < bagSize && slot < MAX_BAG_SLOTS; slot++) { + for (uint32_t slot = 0; + slot < bagSize && slot < MAX_BAG_SLOTS; slot++) { auto item = getBagItem(bagPtr, slot); if (item) { PushBagCGItemToTable(luaState, bagIndex, slot, item); @@ -953,33 +950,33 @@ namespace Nampower { ClearBagSlot(luaState, bagIndex, slot); } } - - lua_settable(luaState, -3); + return 1; } - // Bank bags 5-9 - uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); - if (bankGuid > 0) { - for (int32_t bagIndex = 5; bagIndex <= 9; bagIndex++) { + if (bagIndex >= 5 && bagIndex <= 9) { + uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); + if (bankGuid > 0) { uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 5-9 maps to container 4-8 if (containerGuid == 0) { // Bag slot is empty, clear all cached items for this bag - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); for (uint32_t slot = 0; slot < MAX_BAG_SLOTS; slot++) { ClearBagSlot(luaState, bagIndex, slot); } lua_settable(luaState, -3); - continue; + return 1; } auto containerPtr = game::ClntObjMgrObjectPtr(game::TYPEMASK_CONTAINER, containerGuid); - if (!containerPtr) continue; + if (!containerPtr) { + return 0; + } auto bagPtr = GetBagPtrFromContainer(containerPtr); - if (!bagPtr) continue; + if (!bagPtr) { + return 0; + } - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); auto bagSize = *bagPtr; @@ -993,12 +990,74 @@ namespace Nampower { } } + return 1; + } + } + + + // TODO Error for not finding the bag + return 0; + } + + uint32_t GetAllBagItems(uintptr_t* luaState) { + // Get or create reusable main table + GetTableRef(luaState, bagItemsTableRef); + + // Backpack (bag 0) + lua_pushnumber(luaState, static_cast(0)); + GetSingleBagItems(luaState, 0); + + lua_settable(luaState, -3); + + // Bags 1-4 + for (int32_t bagIndex = 1; bagIndex <= 4; bagIndex++) { + + const auto top = lua_gettop(luaState); + lua_pushnumber(luaState, static_cast(bagIndex)); + const auto bagResult = GetSingleBagItems(luaState, bagIndex); + if (!bagResult) { + lua_settop(luaState, top); + } + else { lua_settable(luaState, -3); } } + // Bank bags 5-9 + uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); + if (bankGuid > 0) { + for (int32_t bagIndex = 5; bagIndex <= 9; bagIndex++) { + const auto top = lua_gettop(luaState); + lua_pushnumber(luaState, static_cast(bagIndex)); + const auto bagResult = GetSingleBagItems(luaState, bagIndex); + if (!bagResult) { + lua_settop(luaState, top); + } + else { + lua_settable(luaState, -3); + } + } + } + return 1; } + uint32_t Script_GetBagItems(uintptr_t* luaState) { + luaState = GetLuaStatePtr(); + + if (lua_gettop(luaState) == 1) { + + if (!lua_isnumber(luaState, 1)) { + lua_error(luaState, "Bag index must be a number"); + return 0; + } + + const auto bagIndex = static_cast(lua_tonumber(luaState, 1)); + return GetSingleBagItems(luaState, bagIndex); + } + + return GetAllBagItems(luaState); + } + } From 7ef1ab103008c2ce6acd940579c50347383acef6 Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 16:54:55 +0100 Subject: [PATCH 2/7] Bank bags go 5 to 9 --- nampower/item_scripts.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nampower/item_scripts.cpp b/nampower/item_scripts.cpp index 6fbeda1..7dda723 100644 --- a/nampower/item_scripts.cpp +++ b/nampower/item_scripts.cpp @@ -767,8 +767,6 @@ namespace Nampower { } } - - uint32_t Script_GetBagItem(uintptr_t *luaState) { luaState = GetLuaStatePtr(); @@ -845,7 +843,7 @@ namespace Nampower { lua_error(luaState, "For bag -1, slot must be 1-24 (bank) or 31-42 (buyback) (Lua 1-indexed)"); return 0; } - } else if (bagIndex >= 4 && bagIndex <= 8) { + } else if (bagIndex >= 5 && bagIndex <= 9) { uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); if (bankGuid == 0) { lua_error(luaState, "Bank is not open"); From f6c280fa1f006388ff06504b0bbc50e3115e9f23 Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 17:13:52 +0100 Subject: [PATCH 3/7] Evaluating -1 bank as well --- nampower/item_scripts.cpp | 119 +++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 46 deletions(-) diff --git a/nampower/item_scripts.cpp b/nampower/item_scripts.cpp index 7dda723..3b1368e 100644 --- a/nampower/item_scripts.cpp +++ b/nampower/item_scripts.cpp @@ -894,26 +894,50 @@ namespace Nampower { auto const getContainerGuid = reinterpret_cast(Offsets::GetContainerGuid); auto const getBagItem = reinterpret_cast(Offsets::CGBag_C_GetItemAtSlot); - - if (bagIndex == 0) { + if (bagIndex == 0 || bagIndex == -1) { const auto playerGuid = game::ClntObjMgrGetActivePlayerGuid(); const auto player = game::GetObjectPtr(playerGuid); const auto inventory = game::GetPlayerInventoryPtr(player); + GetTableRef(luaState, bagTableRefs[0]); - for (uint32_t slot = 23; slot <= 38; slot++) { - auto item = getBagItem(inventory, slot); - uint32_t adjustedSlot = slot - 0x17; - if (item) { - PushBagCGItemToTable(luaState, 0, slot, item); - } else { - // Check if item was removed from this slot - ClearBagSlot(luaState, 0, adjustedSlot); + if (bagIndex == 0) { + for (uint32_t slot = 0; slot <= 16; slot++) { + uint32_t adjustedSlot = slot + 23; // Backbag goes from 23 to 39 + auto item = getBagItem(inventory, adjustedSlot); + if (item) { + PushBagCGItemToTable(luaState, 0, adjustedSlot, item); + } else { + // Check if item was removed from this slot + ClearBagSlot(luaState, 0, slot); + } } + return 1; + } + + if (bagIndex == -1) { + uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); + + if (bankGuid <= 0) { + return 0; + } + + for (uint32_t slot = 0; slot <= 24; slot++) { + uint32_t adjustedSlot = slot + 39; + auto item = getBagItem(inventory, adjustedSlot); + if (item) { + PushBagCGItemToTable(luaState, -1, adjustedSlot, item); + } + else { + // Check if item was removed from this slot + ClearBagSlot(luaState, -1, slot); + } + } + return 1; } - return 1; } + if (bagIndex >= 1 && bagIndex <= 4) { uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 1-4 maps to container 0-3 if (containerGuid == 0) { @@ -953,45 +977,45 @@ namespace Nampower { if (bagIndex >= 5 && bagIndex <= 9) { uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); - if (bankGuid > 0) { - uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 5-9 maps to container 4-8 - if (containerGuid == 0) { - // Bag slot is empty, clear all cached items for this bag - GetTableRef(luaState, bagTableRefs[bagIndex]); - for (uint32_t slot = 0; slot < MAX_BAG_SLOTS; slot++) { - ClearBagSlot(luaState, bagIndex, slot); - } - lua_settable(luaState, -3); - return 1; - } - - auto containerPtr = game::ClntObjMgrObjectPtr(game::TYPEMASK_CONTAINER, containerGuid); - if (!containerPtr) { - return 0; - } - - auto bagPtr = GetBagPtrFromContainer(containerPtr); - if (!bagPtr) { - return 0; - } - + if (bankGuid <= 0) { + return 0; + } + uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 5-9 maps to container 4-8 + if (containerGuid == 0) { + // Bag slot is empty, clear all cached items for this bag GetTableRef(luaState, bagTableRefs[bagIndex]); - - auto bagSize = *bagPtr; - // Check all possible slots up to bag size - for (uint32_t slot = 0; slot < bagSize && slot < MAX_BAG_SLOTS; slot++) { - auto item = getBagItem(bagPtr, slot); - if (item) { - PushBagCGItemToTable(luaState, bagIndex, slot, item); - } else { - ClearBagSlot(luaState, bagIndex, slot); - } + for (uint32_t slot = 0; slot < MAX_BAG_SLOTS; slot++) { + ClearBagSlot(luaState, bagIndex, slot); } - + lua_settable(luaState, -3); return 1; } - } + auto containerPtr = game::ClntObjMgrObjectPtr(game::TYPEMASK_CONTAINER, containerGuid); + if (!containerPtr) { + return 0; + } + + auto bagPtr = GetBagPtrFromContainer(containerPtr); + if (!bagPtr) { + return 0; + } + + GetTableRef(luaState, bagTableRefs[bagIndex]); + + auto bagSize = *bagPtr; + // Check all possible slots up to bag size + for (uint32_t slot = 0; slot < bagSize && slot < MAX_BAG_SLOTS; slot++) { + auto item = getBagItem(bagPtr, slot); + if (item) { + PushBagCGItemToTable(luaState, bagIndex, slot, item); + } else { + ClearBagSlot(luaState, bagIndex, slot); + } + } + + return 1; + } // TODO Error for not finding the bag return 0; @@ -1004,7 +1028,6 @@ namespace Nampower { // Backpack (bag 0) lua_pushnumber(luaState, static_cast(0)); GetSingleBagItems(luaState, 0); - lua_settable(luaState, -3); // Bags 1-4 @@ -1024,6 +1047,10 @@ namespace Nampower { // Bank bags 5-9 uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); if (bankGuid > 0) { + lua_pushnumber(luaState, static_cast(-1)); + GetSingleBagItems(luaState, -1); + lua_settable(luaState, -3); + for (int32_t bagIndex = 5; bagIndex <= 9; bagIndex++) { const auto top = lua_gettop(luaState); lua_pushnumber(luaState, static_cast(bagIndex)); From f0b547394538b5b1f7e51dfcadeb50399e0c6970 Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 17:14:02 +0100 Subject: [PATCH 4/7] Updating documentation --- SCRIPTS.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/SCRIPTS.md b/SCRIPTS.md index 7d2621b..0a44326 100644 --- a/SCRIPTS.md +++ b/SCRIPTS.md @@ -56,7 +56,7 @@ The following functions use reusable table references: - **`GetCastInfo()`** - Returns cast information table - **`GetEquippedItems([unitToken])`** - Returns equipped items table -- **`GetBagItems()`** - Returns bag items table +- **`GetBagItems([bagIndex])`** - Returns bag items table - **`GetBagItem(bagIndex, slot)`** - Returns item info table - **`GetEquippedItem(unitToken, slot)`** - Returns item info table - **`GetSpellIdCooldown(spellId)`** - Returns cooldown detail table @@ -338,8 +338,8 @@ if helm and helm.durability then end ``` -#### GetBagItems() -Returns a nested table reference containing all items in all bags (including bank if open). +#### GetBagItems([bagIndex]) +If no bagIndex is specified, the function returns a nested table reference containing all items in all bags (including bank if open). With specified index, it only returns the contents of that bag **Returns:** - A Lua table reference with bag indices as keys and bag contents as values @@ -367,6 +367,12 @@ for bagIndex, bagContents in pairs(allItems) do end end +-- Get all items in backbag +local bagContents = GetBagItems(0) +for slot, itemInfo in pairs(bagContents) do + print(" Slot " .. slot .. ": " .. itemInfo.itemId .. " (x" .. itemInfo.stackCount .. ")") +end + -- Count total number of a specific item local function CountItem(itemId) local total = 0 From c5ad4e0ed96b95ce4b7db852e52c899a1b8bcccb Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 17:14:36 +0100 Subject: [PATCH 5/7] Return error for invalid bags --- nampower/item_scripts.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nampower/item_scripts.cpp b/nampower/item_scripts.cpp index 3b1368e..babd952 100644 --- a/nampower/item_scripts.cpp +++ b/nampower/item_scripts.cpp @@ -1016,8 +1016,8 @@ namespace Nampower { return 1; } - - // TODO Error for not finding the bag + + lua_error(luaState, "Invalid bag index. Valid values: 0, 1-4, -1, 5-9 (bank)"); return 0; } From b1f21bd7500a854af56acc9211320f0516a5d48e Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 17:22:25 +0100 Subject: [PATCH 6/7] Fixing navigation --- SCRIPTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SCRIPTS.md b/SCRIPTS.md index 0a44326..76d1df5 100644 --- a/SCRIPTS.md +++ b/SCRIPTS.md @@ -14,7 +14,7 @@ For installation, configuration, and general usage information, see the main [RE - [UseItemIdOrName](#useitemidornameitemidorname-target) - [GetEquippedItems](#getequippeditemsunittoken) - [GetEquippedItem](#getequippeditemunittoken-slot) - - [GetBagItems](#getbagitems) + - [GetBagItems](#getbagitemsbagindex) - [GetBagItem](#getbagitembagindex-slot) - [GetSpellRec](#getspellrecspellid-copy) - [GetSpellRecField](#getspellrecfieldspellid-fieldname-copy) From 0e034272965bc466e5b6ea7bbad95ccced182dfa Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 17:28:51 +0100 Subject: [PATCH 7/7] Convert to 0 based index --- nampower/item_scripts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nampower/item_scripts.cpp b/nampower/item_scripts.cpp index babd952..858d6ef 100644 --- a/nampower/item_scripts.cpp +++ b/nampower/item_scripts.cpp @@ -850,7 +850,7 @@ namespace Nampower { return 0; } - uint64_t containerGuid = getContainerGuid(bagIndex); + uint64_t containerGuid = getContainerGuid(bagIndex-1); if (containerGuid == 0) { lua_pushnil(luaState); return 1;