C_Container.GetContainerItemID(bagIndex, slotIndex)

This commit is contained in:
Brues
2026-05-08 00:44:05 -05:00
parent 473681416e
commit 51484ff47b
6 changed files with 85 additions and 15 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ Quick index:
| Spell | `GetSpellInfo` (with `(slot, bookType)` overload), `C_Spell.GetSpellInfo`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `GetSpellLink` (with `(slot, bookType)` overload), `C_Spell.GetSpellLink`, `FindSpellBookSlotByID`, `GameTooltip:SetSpellByID`, `C_Spell.GetSpellDescription` |
| Quest | `GetQuestIDFromLogIndex`, `C_QuestLog.RequestLoadQuestByID` (+ `QUEST_DATA_LOAD_RESULT` event), `C_QuestLog.GetTitleForQuestID` |
| Faction | `GetFactionIDByIndex`, `GetFactionInfoByID` |
| Item | `C_Item.IsBound`, `C_Item.GetItemID`, `GetInventoryItemID`, `C_Item.GetItemInfoInstant`, `C_Item.IsItemDataCached(ByID)`, `C_Item.RequestLoadItemData(ByID)` (+ `ITEM_DATA_LOAD_RESULT` event) |
| Item | `C_Item.IsBound`, `C_Item.GetItemID`, `GetInventoryItemID`, `C_Container.GetContainerItemID`, `C_Item.GetItemInfoInstant`, `C_Item.IsItemDataCached(ByID)`, `C_Item.RequestLoadItemData(ByID)` (+ `ITEM_DATA_LOAD_RESULT` event) |
| Events | `C_EventUtils.IsEventValid` |
| Global | `CLASSIC_API_VERSION` |
+14 -5
View File
@@ -240,12 +240,21 @@ value of `GetFactionInfo`, but 1.12-3.3.5 don't expose it at all.
See [src/faction/Info.cpp](src/faction/Info.cpp) and "Faction lookups" in
[CLAUDE.md](CLAUDE.md) for the resolver/dual-count details.
## 16. `GetContainerItemID(bagID, slotIndex)` — trivial
## ~~16. `C_Container.GetContainerItemID(bagIndex, slotIndex)`~~ — DONE
Modern positional accessor for the same data `C_Item.GetItemID({bagID=B,
slotIndex=S})` returns. We already have the full bag→CGItem→itemID chain
in [src/item/Location.cpp](src/item/Location.cpp); this is just a
two-positional-arg wrapper over it.
Shipped under the modern `C_Container.*` namespace (matching post-MoP
WoW). Wraps the existing bag→CGItem path: a new public
`Item::Location::ResolveBag(L, bagID, slotIndex)` exposes the bag
resolver that was previously private inside the table-shape
`Resolve()`, and a small `ItemIDFromCGItem` helper in
[src/item/ID.cpp](src/item/ID.cpp) factors out the CGItem → instance
block → itemID step now used by both `C_Item.GetItemID` and
`C_Container.GetContainerItemID`.
Note: ships under `C_Container` rather than as the historical legacy
global `GetContainerItemID` since the modern namespace is the canonical
home for bag/container APIs (the global was deprecated in 10.0). If
addons need the legacy global it's a one-line addition.
## 17. `GetItemIcon(item)` — trivial
+23
View File
@@ -27,6 +27,7 @@ build instructions.
- [`C_Item.IsBound(itemLocation)`](#c_itemisbounditemlocation)
- [`C_Item.GetItemID(itemLocation)`](#c_itemgetitemiditemlocation)
- [`GetInventoryItemID(unit, slot)`](#getinventoryitemidunit-slot)
- [`C_Container.GetContainerItemID(bagIndex, slotIndex)`](#c_containergetcontaineritemidbagindex-slotindex)
- [`C_Item.GetItemInfoInstant(item)`](#c_itemgetiteminfoinstantitem)
- [`C_Item.IsItemDataCachedByID(item)` / `C_Item.IsItemDataCached(itemLocation)`](#c_itemisitemdatacachedbyiditem--c_itemisitemdatacacheditemlocation)
- [`C_Item.RequestLoadItemDataByID(item)` / `C_Item.RequestLoadItemData(itemLocation)`](#c_itemrequestloaditemdatabyiditem--c_itemrequestloaditemdataitemlocation)
@@ -494,6 +495,28 @@ end
local headID = GetInventoryItemID("party1", INVSLOT_HEAD)
```
### `C_Container.GetContainerItemID(bagIndex, slotIndex)`
Returns the itemID at the given bag/slot, or `nil` if the slot is empty
or the indices are out of range. Modern positional-arg form of the same
lookup `C_Item.GetItemID({bagID=B, slotIndex=S})` performs.
- `bagIndex = 0` — the player's main backpack.
- `bagIndex = 1..4` — the player's equipped bag slots.
- `slotIndex` — 1-based, capped at the bag's actual slot count (the
engine's `PackBagSlot` rejects out-of-range slots and returns nil
cleanly).
```lua
for slot = 1, 16 do
local id = C_Container.GetContainerItemID(0, slot)
if id then
local _, type, subtype = C_Item.GetItemInfoInstant(id)
-- ...
end
end
```
### `C_Item.GetItemInfoInstant(item)`
Modern-style accessor for the always-available subset of item info — the
+37 -9
View File
@@ -19,31 +19,59 @@
namespace Item::ID {
// Reads the itemID off a resolved `CGItem *` via the instance block at
// +0x08. Returns 0 for null/unpopulated items so callers can short-
// circuit to a nil push.
static int ItemIDFromCGItem(const uint8_t *item) {
if (item == nullptr)
return 0;
auto *instance = *reinterpret_cast<const uint8_t *const *>(
item + Offsets::OFF_ITEM_INSTANCE_BLOCK);
if (instance == nullptr)
return 0;
return static_cast<int>(*reinterpret_cast<const uint32_t *>(
instance + Offsets::OFF_INSTANCE_BLOCK_ITEM_ID));
}
static int __fastcall Script_GetItemID(void *L) {
if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) {
Game::Lua::Error(L, "Usage: C_Item.GetItemID(itemLocation)");
return 0;
}
const uint8_t *item = Item::Location::Resolve(L, 1);
if (item == nullptr) {
const int itemID = ItemIDFromCGItem(Item::Location::Resolve(L, 1));
if (itemID == 0) {
Game::Lua::PushNil(L);
return 1;
}
auto *instance = *reinterpret_cast<const uint8_t *const *>(
item + Offsets::OFF_ITEM_INSTANCE_BLOCK);
if (instance == nullptr) {
Game::Lua::PushNil(L);
return 1;
Game::Lua::PushNumber(L, static_cast<double>(itemID));
return 1;
}
// `C_Container.GetContainerItemID(bagIndex, slotIndex)` — modern
// positional-arg accessor for the same data `C_Item.GetItemID({bagID=B,
// slotIndex=S})` returns. Both go through `Item::Location::ResolveBag`
// → engine `PackBagSlot` → `GetItemBySlot` → CGItem → instance block
// → itemID. Returns nil for empty slots or invalid bag indices.
static int __fastcall Script_C_Container_GetContainerItemID(void *L) {
if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2)) {
Game::Lua::Error(L, "Usage: C_Container.GetContainerItemID(bagIndex, slotIndex)");
return 0;
}
const uint32_t itemID = *reinterpret_cast<const uint32_t *>(
instance + Offsets::OFF_INSTANCE_BLOCK_ITEM_ID);
const int bagID = static_cast<int>(Game::Lua::ToNumber(L, 1));
const int slotIndex = static_cast<int>(Game::Lua::ToNumber(L, 2));
const int itemID = ItemIDFromCGItem(Item::Location::ResolveBag(L, bagID, slotIndex));
if (itemID == 0)
return 0;
Game::Lua::PushNumber(L, static_cast<double>(itemID));
return 1;
}
static void RegisterLuaFunctions() {
Game::Lua::RegisterTableFunction("C_Item", "GetItemID", &Script_GetItemID);
Game::Lua::RegisterTableFunction("C_Container", "GetContainerItemID",
&Script_C_Container_GetContainerItemID);
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
+4
View File
@@ -83,6 +83,10 @@ const uint8_t *ResolveBagSlot(void *L, int bagID, int slotIndex) {
} // namespace
const uint8_t *ResolveBag(void *L, int bagID, int slotIndex) {
return ResolveBagSlot(L, bagID, slotIndex);
}
const uint8_t *Resolve(void *L, int locIdx) {
if (Game::Lua::Type(L, locIdx) != Game::Lua::TYPE_TABLE)
return nullptr;
+6
View File
@@ -34,4 +34,10 @@ namespace Item::Location {
// Only safe to call from inside a Lua callback.
const uint8_t *Resolve(void *L, int locIdx);
// Resolves a `(bagID, slotIndex)` pair directly to a `CGItem *`. Used by
// the positional-arg C_Container.* APIs that don't go through the
// ItemLocation table shape. Stomps the Lua stack — same caveat as
// `Resolve()` above.
const uint8_t *ResolveBag(void *L, int bagID, int slotIndex);
} // namespace Item::Location