Advertisement
Lasivian

Untitled

Jan 20th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1. -- Declare the saved variable
  2. FishingLootTrackerDB = FishingLootTrackerDB or {}
  3.  
  4. -- Name of the bag we want to track
  5. local TARGET_BAG_NAME = "Bag of Fishing Treasures"
  6. local processingBag = false -- Flag to prevent multiple simultaneous actions
  7. local pendingBags = {} -- Queue for pending bags
  8.  
  9. -- Function to initialize saved data
  10. local function InitializeDB()
  11.     -- Ensure the saved variable structure exists
  12.     if not FishingLootTrackerDB.BagCount then
  13.         FishingLootTrackerDB.BagCount = 0
  14.     end
  15.  
  16.     if not FishingLootTrackerDB.Items then
  17.         FishingLootTrackerDB.Items = {}
  18.     end
  19. end
  20.  
  21. -- Function to scan inventory for the target bag
  22. local function ScanBags()
  23.     for bag = 0, 4 do -- Check all bags (0 = backpack, 1-4 = regular bags)
  24.         for slot = 1, GetContainerNumSlots(bag) do
  25.             local itemLink = GetContainerItemLink(bag, slot)
  26.             if itemLink then
  27.                 local itemName = GetItemInfo(itemLink)
  28.                 if itemName == TARGET_BAG_NAME then
  29.                     table.insert(pendingBags, {bag = bag, slot = slot})
  30.                 end
  31.             end
  32.         end
  33.     end
  34. end
  35.  
  36. -- Function to open the next bag in the queue
  37. local function OpenNextBag()
  38.     if processingBag or #pendingBags == 0 then return end -- Prevent concurrent processing
  39.  
  40.     local nextBag = table.remove(pendingBags, 1)
  41.     if nextBag then
  42.         processingBag = true
  43.         print(string.format("Opening %s in bag %d, slot %d...", TARGET_BAG_NAME, nextBag.bag, nextBag.slot))
  44.  
  45.         -- Use the item to open it
  46.         UseContainerItem(nextBag.bag, nextBag.slot)
  47.  
  48.         -- Increment the bag count
  49.         FishingLootTrackerDB.BagCount = FishingLootTrackerDB.BagCount + 1
  50.         print("Bag of Fishing Treasures opened. Total Bags Opened: " .. FishingLootTrackerDB.BagCount)
  51.  
  52.         -- Delay processing of the next bag to ensure sequential execution
  53.         local timerFrame = CreateFrame("Frame")
  54.         local elapsed = 0
  55.         timerFrame:SetScript("OnUpdate", function(self, delta)
  56.             elapsed = elapsed + delta
  57.             if elapsed >= 0.5 then
  58.                 self:SetScript("OnUpdate", nil)
  59.                 OpenNextBag()
  60.             end
  61.         end)
  62.     end
  63. end
  64.  
  65. -- Function to handle loot events when a bag is opened
  66. local function OnLootOpened()
  67.     print("Loot window opened.")
  68.     for i = 1, GetNumLootItems() do
  69.         local itemLink = GetLootSlotLink(i)
  70.         if itemLink then
  71.             local itemName = GetItemInfo(itemLink)
  72.             if itemName then
  73.                 FishingLootTrackerDB.Items[itemName] = (FishingLootTrackerDB.Items[itemName] or 0) + 1
  74.                 print(string.format("Tracked: %s (Total: %d)", itemName, FishingLootTrackerDB.Items[itemName]))
  75.             end
  76.         end
  77.     end
  78.  
  79.     -- Mark bag processing as complete
  80.     processingBag = false
  81. end
  82.  
  83. -- Function to handle bag updates (automatically scans and opens bags)
  84. local function OnBagUpdate()
  85.     if not processingBag then
  86.         ScanBags() -- Look for any new bags
  87.         OpenNextBag() -- Start opening them
  88.     end
  89. end
  90.  
  91. -- Function to display tracked loot and bag count in a frame
  92. local function ShowLootDataFrame()
  93.     -- Create the main frame if it doesn't already exist
  94.     if not FishingLootFrame then
  95.         local frame = CreateFrame("Frame", "FishingLootFrame", UIParent)
  96.         frame:SetSize(600, 400)
  97.         frame:SetPoint("CENTER")
  98.         frame:SetBackdrop({
  99.             bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  100.             edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
  101.             tile = true,
  102.             tileSize = 32,
  103.             edgeSize = 32,
  104.             insets = { left = 8, right = 8, top = 8, bottom = 8 }
  105.         })
  106.         frame:SetMovable(true)
  107.         frame:EnableMouse(true)
  108.         frame:RegisterForDrag("LeftButton")
  109.         frame:SetScript("OnDragStart", frame.StartMoving)
  110.         frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
  111.  
  112.         -- Add a title
  113.         local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  114.         title:SetPoint("TOP", 0, -10)
  115.         title:SetText("Fishing Loot Tracker")
  116.  
  117.         -- Add a close button
  118.         local closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
  119.         closeButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -5, -5)
  120.         closeButton:SetScript("OnClick", function() frame:Hide() end)
  121.  
  122.         -- Add a scrollable text area
  123.         local scrollFrame = CreateFrame("ScrollFrame", "FishingLootScrollFrame", frame, "UIPanelScrollFrameTemplate")
  124.         scrollFrame:SetPoint("TOPLEFT", 10, -40)
  125.         scrollFrame:SetPoint("BOTTOMRIGHT", -30, 10)
  126.  
  127.         local editBox = CreateFrame("EditBox", "FishingLootEditBox", scrollFrame)
  128.         editBox:SetMultiLine(true)
  129.         editBox:SetFontObject(ChatFontNormal)
  130.         editBox:SetWidth(550)
  131.         editBox:SetAutoFocus(false)
  132.         editBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
  133.         scrollFrame:SetScrollChild(editBox)
  134.  
  135.         -- Save references for reuse
  136.         frame.scrollFrame = scrollFrame
  137.         frame.editBox = editBox
  138.         FishingLootFrame = frame
  139.     end
  140.  
  141.     -- Update the loot data when the frame is shown
  142.     local frame = FishingLootFrame
  143.     local editBox = frame.editBox
  144.  
  145.     local data = {"Total Bags Opened: " .. (FishingLootTrackerDB.BagCount or 0)}
  146.     for itemName, count in pairs(FishingLootTrackerDB.Items or {}) do
  147.         table.insert(data, string.format("%s: %d", itemName, count))
  148.     end
  149.  
  150.     editBox:SetText(table.concat(data, "\n"))
  151.     editBox:HighlightText()
  152.     frame:Show()
  153. end
  154.  
  155. -- Register events to track bag updates and loot openings
  156. local eventFrame = CreateFrame("Frame")
  157. eventFrame:RegisterEvent("ADDON_LOADED")
  158. eventFrame:RegisterEvent("BAG_UPDATE")
  159. eventFrame:RegisterEvent("LOOT_OPENED")
  160. eventFrame:SetScript("OnEvent", function(self, event, addonName)
  161.     if event == "ADDON_LOADED" and addonName == "FishingLootTracker" then
  162.         InitializeDB()
  163.     elseif event == "BAG_UPDATE" then
  164.         OnBagUpdate()
  165.     elseif event == "LOOT_OPENED" then
  166.         OnLootOpened()
  167.     end
  168. end)
  169.  
  170. -- Slash command to open the loot data frame
  171. SLASH_FISHINGLOOT1 = "/fishingloot"
  172. SlashCmdList["FISHINGLOOT"] = ShowLootDataFrame
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement