It's possible the LOOT_CLOSED event is too early to use, since it fires before the last CHAT_MSG_LOOT event for everything you looted. I suggest using BAG_UPDATE_DELAYED, which fires once at the end of any bag updates. I also suggest using that filename variable instead, since the first return won't be "Mage" if your client is not English. The filename is an all-caps identifier that will always be the English name in every client. It's also possible that UnitClass doesn't return anything while addons are loading. It's not uncommon to wait for the ADDON_LOADED, PLAYER_LOGIN, or even PLAYER_ENTERING_WORLD events before requesting data.
I rearranged your code with my suggestions and cleaned it up a bit:
Lua Code:
local UnitClass,GetContainerNumSlots,GetContainerItemInfo,PickupContainerItem,DeleteCursorItem,UseContainerItem,select
= UnitClass,GetContainerNumSlots,GetContainerItemInfo,PickupContainerItem,DeleteCursorItem,UseContainerItem,select
local _, class
local items = { -- abandon list
--food
[4599] = true,
[4608] = true,
}
local open = { -- open list
[2744] = true,
[5523] = true,
[5524] = true,
[7973] = true,
[15874] = true,
[20767] = true,
[21150] = true,
}
local f = CreateFrame("frame")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("BAG_UPDATE_DELAYED")
f:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_LOGIN" then
_, class = UnitClass("player")
else
for b = 0, 4 do
for s = 1, GetContainerNumSlots(b) do
local id = select(10, GetContainerItemInfo(b, s))
if class == "MAGE" and items[id] then
PickupContainerItem(b, s)
DeleteCursorItem()
--print("clean")
end
if open[id] then
UseContainerItem(b, s)
--print("open")
end
end
end
end
end)