View Single Post
02-11-23, 11:58 AM   #12
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
It can with a few tweaks. I added notes for educational purposes and fixed skipping free container slots if it encounters an empty inventory slot.

Lua Code:
  1. local InventorySlots={--    Comment or remove whichever slots you don't want to process, these enums exist in all clients
  2.     INVSLOT_AMMO;
  3.     INVSLOT_HEAD;
  4.     INVSLOT_NECK;
  5.     INVSLOT_SHOULDER;
  6.     INVSLOT_BODY;
  7.     INVSLOT_CHEST;
  8.     INVSLOT_WAIST;
  9.     INVSLOT_LEGS;
  10.     INVSLOT_FEET;
  11.     INVSLOT_WRIST;
  12.     INVSLOT_HAND;
  13.     INVSLOT_FINGER1;
  14.     INVSLOT_FINGER2;
  15.     INVSLOT_TRINKET1;
  16.     INVSLOT_TRINKET2;
  17.     INVSLOT_BACK;
  18.     INVSLOT_MAINHAND;
  19.     INVSLOT_OFFHAND;
  20.     INVSLOT_RANGED;
  21.     INVSLOT_TABARD;
  22. };
  23.  
  24. local function UnequipAll()
  25.     if #InventorySlots<=0 then return; end--    Sanity check
  26.     local slotindex=1;
  27.  
  28.     ClearCursor();--    Make sure the cursor isn't holding anything, otherwise we might accidentally issue an item swap instead of an unequip
  29.     for bag=NUM_BAG_SLOTS or NUM_TOTAL_EQUIPPED_BAG_SLOTS,0,-1 do-- CE and Wrath use NUM_BAG_SLOTS, DF uses NUM_TOTAL_EQUIPPED_BAG_SLOTS
  30.         local free,type=(C_Container or _G).GetContainerNumFreeSlots(bag);--    C_Container is used in Wrath and DF, CE still has this in _G
  31.         free=(type==0 and free or 0);-- Uses a quirk with this style of condition, only process bags with no item type restriction with fallback to zero if no bag is there (if free is nil, it'll fallback to zero even if the condition is true)
  32.  
  33.         for _=1,free do--   Variable is unused, we just need to loop for every free slot we see
  34.             local invslot=InventorySlots[slotindex];--  Cache slot mapped to current index
  35.             while not GetInventoryItemID("player",invslot) do-- Loop if no item in slot and until we find one
  36.                 if slotindex<#InventorySlots then slotindex=slotindex+1; else return; end-- Increment to next index or stop when we have no more inventory slots to process
  37.                 invslot=InventorySlots[slotindex];--    Update to new slot
  38.             end
  39.  
  40. --          This pair is a complete operation, cursor is expected to be clear by the time both of these lines have run
  41.             PickupInventoryItem(invslot);
  42.             (bag==0 and PutItemInBackpack or PutItemInBag)((C_Container or _G).ContainerIDToInventoryID(bag));--    First set of parenthesis chooses function to run before calling it, PutItemInBackpack() doesn't accept any args, so ContainerIDToInventoryID() is safe to run regardless
  43.  
  44.             if slotindex<#InventorySlots then slotindex=slotindex+1; else return; end-- Increment to next index or stop when we have no more inventory slots to process
  45.         end
  46.     end
  47. end
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 02-11-23 at 12:20 PM.
  Reply With Quote