Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Declare the saved variable
- FishingLootTrackerDB = FishingLootTrackerDB or {}
- -- Name of the bag we want to track
- local TARGET_BAG_NAME = "Bag of Fishing Treasures"
- local processingBag = false -- Flag to prevent multiple simultaneous actions
- local pendingBags = {} -- Queue for pending bags
- -- Function to initialize saved data
- local function InitializeDB()
- -- Ensure the saved variable structure exists
- if not FishingLootTrackerDB.BagCount then
- FishingLootTrackerDB.BagCount = 0
- end
- if not FishingLootTrackerDB.Items then
- FishingLootTrackerDB.Items = {}
- end
- end
- -- Function to scan inventory for the target bag
- local function ScanBags()
- for bag = 0, 4 do -- Check all bags (0 = backpack, 1-4 = regular bags)
- for slot = 1, GetContainerNumSlots(bag) do
- local itemLink = GetContainerItemLink(bag, slot)
- if itemLink then
- local itemName = GetItemInfo(itemLink)
- if itemName == TARGET_BAG_NAME then
- table.insert(pendingBags, {bag = bag, slot = slot})
- end
- end
- end
- end
- end
- -- Function to open the next bag in the queue
- local function OpenNextBag()
- if processingBag or #pendingBags == 0 then return end -- Prevent concurrent processing
- local nextBag = table.remove(pendingBags, 1)
- if nextBag then
- processingBag = true
- print(string.format("Opening %s in bag %d, slot %d...", TARGET_BAG_NAME, nextBag.bag, nextBag.slot))
- -- Use the item to open it
- UseContainerItem(nextBag.bag, nextBag.slot)
- -- Increment the bag count
- FishingLootTrackerDB.BagCount = FishingLootTrackerDB.BagCount + 1
- print("Bag of Fishing Treasures opened. Total Bags Opened: " .. FishingLootTrackerDB.BagCount)
- -- Delay processing of the next bag to ensure sequential execution
- local timerFrame = CreateFrame("Frame")
- local elapsed = 0
- timerFrame:SetScript("OnUpdate", function(self, delta)
- elapsed = elapsed + delta
- if elapsed >= 0.5 then
- self:SetScript("OnUpdate", nil)
- OpenNextBag()
- end
- end)
- end
- end
- -- Function to handle loot events when a bag is opened
- local function OnLootOpened()
- print("Loot window opened.")
- for i = 1, GetNumLootItems() do
- local itemLink = GetLootSlotLink(i)
- if itemLink then
- local itemName = GetItemInfo(itemLink)
- if itemName then
- FishingLootTrackerDB.Items[itemName] = (FishingLootTrackerDB.Items[itemName] or 0) + 1
- print(string.format("Tracked: %s (Total: %d)", itemName, FishingLootTrackerDB.Items[itemName]))
- end
- end
- end
- -- Mark bag processing as complete
- processingBag = false
- end
- -- Function to handle bag updates (automatically scans and opens bags)
- local function OnBagUpdate()
- if not processingBag then
- ScanBags() -- Look for any new bags
- OpenNextBag() -- Start opening them
- end
- end
- -- Function to display tracked loot and bag count in a frame
- local function ShowLootDataFrame()
- -- Create the main frame if it doesn't already exist
- if not FishingLootFrame then
- local frame = CreateFrame("Frame", "FishingLootFrame", UIParent)
- frame:SetSize(600, 400)
- frame:SetPoint("CENTER")
- frame:SetBackdrop({
- bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
- edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
- tile = true,
- tileSize = 32,
- edgeSize = 32,
- insets = { left = 8, right = 8, top = 8, bottom = 8 }
- })
- frame:SetMovable(true)
- frame:EnableMouse(true)
- frame:RegisterForDrag("LeftButton")
- frame:SetScript("OnDragStart", frame.StartMoving)
- frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
- -- Add a title
- local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
- title:SetPoint("TOP", 0, -10)
- title:SetText("Fishing Loot Tracker")
- -- Add a close button
- local closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
- closeButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -5, -5)
- closeButton:SetScript("OnClick", function() frame:Hide() end)
- -- Add a scrollable text area
- local scrollFrame = CreateFrame("ScrollFrame", "FishingLootScrollFrame", frame, "UIPanelScrollFrameTemplate")
- scrollFrame:SetPoint("TOPLEFT", 10, -40)
- scrollFrame:SetPoint("BOTTOMRIGHT", -30, 10)
- local editBox = CreateFrame("EditBox", "FishingLootEditBox", scrollFrame)
- editBox:SetMultiLine(true)
- editBox:SetFontObject(ChatFontNormal)
- editBox:SetWidth(550)
- editBox:SetAutoFocus(false)
- editBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
- scrollFrame:SetScrollChild(editBox)
- -- Save references for reuse
- frame.scrollFrame = scrollFrame
- frame.editBox = editBox
- FishingLootFrame = frame
- end
- -- Update the loot data when the frame is shown
- local frame = FishingLootFrame
- local editBox = frame.editBox
- local data = {"Total Bags Opened: " .. (FishingLootTrackerDB.BagCount or 0)}
- for itemName, count in pairs(FishingLootTrackerDB.Items or {}) do
- table.insert(data, string.format("%s: %d", itemName, count))
- end
- editBox:SetText(table.concat(data, "\n"))
- editBox:HighlightText()
- frame:Show()
- end
- -- Register events to track bag updates and loot openings
- local eventFrame = CreateFrame("Frame")
- eventFrame:RegisterEvent("ADDON_LOADED")
- eventFrame:RegisterEvent("BAG_UPDATE")
- eventFrame:RegisterEvent("LOOT_OPENED")
- eventFrame:SetScript("OnEvent", function(self, event, addonName)
- if event == "ADDON_LOADED" and addonName == "FishingLootTracker" then
- InitializeDB()
- elseif event == "BAG_UPDATE" then
- OnBagUpdate()
- elseif event == "LOOT_OPENED" then
- OnLootOpened()
- end
- end)
- -- Slash command to open the loot data frame
- SLASH_FISHINGLOOT1 = "/fishingloot"
- SlashCmdList["FISHINGLOOT"] = ShowLootDataFrame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement