Advertisement
Guest User

ItemHandler.lua

a guest
Sep 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.28 KB | None | 0 0
  1. --[[
  2.     Project         : LastSeen © 2019
  3.     Author          : Oxlotus - Area 52-US
  4.     Date Created    : 2019-04-23
  5.     Purpose         : Handler for all item-related functions.
  6. ]]--
  7.  
  8. local lastSeen, LastSeenTbl = ...;
  9. local L = LastSeenTbl.L;
  10. local select = select;
  11. local sourceIsKnown;
  12.  
  13. -- Common API calls
  14. local GetPlayerMapPosition = C_Map.GetPlayerMapPosition;
  15. local GetBestMapForUnit = C_Map.GetBestMapForUnit;
  16.  
  17. LastSeenTbl.New = function(itemID, itemName, itemLink, itemRarity, itemType, today, source, currentMap, key)
  18.     local isInInstance = IsInInstance();
  19.  
  20.     if isInInstance then
  21.         local _, _, _, difficultyName = GetInstanceInfo();
  22.         currentMap = currentMap .. " (" .. difficultyName .. ")";
  23.        
  24.         if LastSeenTbl.encounterName ~= nil then
  25.             if LastSeenTbl.encounterName ~= "" then
  26.                 source = LastSeenTbl.encounterName;
  27.             end
  28.         end
  29.     end
  30.  
  31.     LastSeenItemsDB[itemID] = {itemName = itemName, itemLink = itemLink, itemRarity = itemRarity, itemType = itemType, lootDate = today, source = source,
  32.     location = currentMap, key = key, sourceIDs = {}};
  33.    
  34.     LastSeenLootTemplate[itemID] = {[source] = 1};
  35.    
  36.     local _, sourceID = C_TransmogCollection.GetItemInfo(itemID);
  37.     if sourceID then
  38.         LastSeenItemsDB[itemID]["sourceIDs"][sourceID] = today;
  39.     end
  40.    
  41.     if LastSeenTbl.mode ~= L["QUIET_MODE"] then
  42.         if LastSeenTbl.mode == L["VERBOSE_MODE"] then
  43.             print(L["ADDON_NAME"] .. L["ADDED_ITEM"] .. "|T"..select(5, GetItemInfoInstant(itemID))..":0|t" .. itemLink .. ". (" .. LastSeenTbl.GetItemsSeen(LastSeenItemsDB) .. ")");
  44.         else
  45.             print(L["ADDON_NAME"] .. L["ADDED_ITEM"] .. "|T"..select(5, GetItemInfoInstant(itemID))..":0|t" .. itemLink .. ".");
  46.         end
  47.     end
  48. end
  49.  
  50. LastSeenTbl.Update = function(manualEntry, itemID, itemName, itemLink, itemType, itemRarity, lootDate, source, location, key)
  51.     if not source then return end; -- For some reason auctions are calling this...
  52.    
  53.     local isInInstance = IsInInstance();
  54.     local isSourceKnown;
  55.  
  56.     if isInInstance then
  57.         local _, _, _, difficultyName = GetInstanceInfo();
  58.         location = location .. " (" .. difficultyName .. ")";
  59.        
  60.         if LastSeenTbl.encounterName ~= nil then
  61.             if LastSeenTbl.encounterName ~= "" then
  62.                 source = LastSeenTbl.encounterName;
  63.             end
  64.         end
  65.     end
  66.  
  67.     if LastSeenItemsDB[itemID].manualEntry == true then -- A manually entered item has been seen!
  68.         LastSeenItemsDB[itemID].itemName = itemName;
  69.         LastSeenItemsDB[itemID].itemLink = itemLink;
  70.         LastSeenItemsDB[itemID].itemType = itemType;
  71.         LastSeenItemsDB[itemID].itemRarity = itemRarity;
  72.         LastSeenItemsDB[itemID].lootDate = lootDate;
  73.         LastSeenItemsDB[itemID].source = source;
  74.         LastSeenItemsDB[itemID].location = location;
  75.         LastSeenItemsDB[itemID].key = key;
  76.         LastSeenItemsDB[itemID].manualEntry = nil; -- Remove the manual entry flag.
  77.         LastSeenTbl.wasUpdated = true;
  78.     else
  79.         for v in pairs(LastSeenItemsDB[itemID]) do
  80.             if v == "lootDate" then if LastSeenItemsDB[itemID][v] ~= lootDate then LastSeenItemsDB[itemID][v] = lootDate; LastSeenTbl.wasUpdated = true; end; end
  81.             if v == "location" then if LastSeenItemsDB[itemID][v] ~= location then LastSeenItemsDB[itemID][v] = location; LastSeenTbl.wasUpdated = true; end; end
  82.             if v == "source" then if LastSeenItemsDB[itemID][v] ~= source then LastSeenItemsDB[itemID][v] = source; LastSeenTbl.wasUpdated = true; end; end
  83.             if v == "key" then if LastSeenItemsDB[itemID][v] ~= key then LastSeenItemsDB[itemID][v] = key; LastSeenTbl.wasUpdated = true; end; end
  84.         end
  85.     end
  86.    
  87.     if LastSeenLootTemplate[itemID] then -- The item has been added to the loot template database at some point in the past.
  88.         for k, v in next, LastSeenLootTemplate[itemID] do
  89.             if (k == source) then -- An existing source was discovered; therefore we should increment that source.
  90.                 v = v + 1; LastSeenLootTemplate[itemID][k] = v;
  91.                 sourceIsKnown = true;
  92.             else
  93.                 sourceIsKnown = false;
  94.             end
  95.         end
  96.        
  97.         if not sourceIsKnown then
  98.             LastSeenLootTemplate[itemID][source] = 1;
  99.             sourceIsKnown = ""; -- Set this boolean equal to a blank string.
  100.         end
  101.     else -- The item exists in the item template database, but hasn't been inserted into the loot template database yet.
  102.         LastSeenLootTemplate[itemID] = {[source] = 1};
  103.     end
  104.    
  105.     local _, sourceID = C_TransmogCollection.GetItemInfo(itemID);
  106.     local sourceTblSize = table.getn(LastSeenItemsDB[itemID]["sourceIDs"]);
  107.     if sourceID then
  108.         if (sourceTblSize < 1) then
  109.             LastSeenItemsDB[itemID]["sourceIDs"][sourceID] = lootDate;
  110.         else
  111.             for k, v in pairs(LastSeenItemsDB[itemID]["sourceIDs"]) do
  112.                 if (k == sourceID) then
  113.                     if (v ~= lootDate) then
  114.                         v = lootDate;
  115.                     end
  116.                 end
  117.             end
  118.         end
  119.     end
  120.    
  121.     if LastSeenTbl.wasUpdated and LastSeenTbl.mode ~= L["QUIET_MODE"] then
  122.         print(L["ADDON_NAME"] .. L["UPDATED_ITEM"] .. "|T"..select(5, GetItemInfoInstant(itemID))..":0|t" .. itemLink .. ".");
  123.         LastSeenTbl.wasUpdated = false;
  124.     end
  125. end
  126.  
  127. local function GetCoords()
  128.     if not (IsInInstance()) then
  129.         local uiMapID = GetBestMapForUnit("player");
  130.         local position = GetPlayerMapPosition(uiMapID, "player");
  131.         local x, y = position:GetXY(); x = LastSeenTbl.Round(x, 2); y = LastSeenTbl.Round(y, 2);
  132.         local coords = x .. ", " .. y;
  133.        
  134.         return " (" .. coords .. ")";
  135.     else
  136.         return "";
  137.     end
  138. end
  139.  
  140. local function GetItemIDFromItemLink(itemLink)
  141.     local itemID = select(1, GetItemInfoInstant(itemLink));
  142.  
  143.     return itemID;
  144. end
  145.  
  146. local function GetItemNameFromItemID(itemID)
  147.     local itemName = select(1, GetItemInfo(itemID));
  148.  
  149.     return itemName;
  150. end
  151.  
  152. local function GetItemRarityFromItemID(itemID)
  153.     local itemRarity = select(3, GetItemInfo(itemID));
  154.  
  155.     return itemRarity;
  156. end
  157.  
  158. local function GetItemTypeFromItemID(itemID)
  159.     local itemType = select(6, GetItemInfo(itemID));
  160.  
  161.     return itemType;
  162. end
  163.  
  164. --Modified This Area (Vandiel)
  165. local function PlayerLootedContainer(itemLink, currentDate, currentMap, itemSource)
  166.     local itemID = GetItemIDFromItemLink(itemLink);
  167.     if not itemID then return end;
  168.  
  169.     local itemName = GetItemNameFromItemID(itemID); -- This is the name of the item container, not the loot.
  170.     local itemRarity = GetItemRarityFromItemID(itemID);
  171.     local itemType = GetItemTypeFromItemID(itemID);
  172.  
  173.     if itemRarity >= LastSeenSettingsCacheDB.rarity or LastSeenItemsDB[itemID] and LastSeenItemsDB[itemID]["manualEntry"] then
  174.         for k, v in pairs(LastSeenTbl.ignoredItemTypes) do
  175.             if itemType == v and not LastSeenTbl.doNotIgnore then
  176.                 return;
  177.             end
  178.         end
  179.         for k, v in pairs(LastSeenTbl.ignoredItems) do
  180.             if itemID == k and not LastSeenTbl.doNotIgnore then
  181.                 return;
  182.             end
  183.         end
  184.         if LastSeenIgnoredItemsDB[itemID] and LastSeenTbl.doNotIgnore then
  185.             return;
  186.         end
  187.  
  188.         --check itemSource (Vandiel)
  189.         if itemSource == L["IS_MISCELLANEOUS"] or itemSource == L["IS_CONSUMABLE"] then
  190.             LootItem = LastSeenTbl.lootedItem;
  191.         end
  192.         if itemSource == L["IS_OBJECT"] then
  193.             LootItem = L["OBJECT"] .. _G["GameTooltipTextLeft1"]:GetText();
  194.         end
  195.         if itemSource == L["AUCTION_HOUSE_SOURCE"] then
  196.             LootItem = L["AUCTION_HOUSE_SOURCE"];
  197.         end
  198.        
  199.         if LastSeenItemsDB[itemID] then
  200.             LastSeenTbl.Update(manualEntry, itemID, itemName, itemLink, itemType, itemRarity, currentDate, LootItem, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  201.         else
  202.             LastSeenTbl.New(itemID, itemName, itemLink, itemRarity, itemType, currentDate, LootItem, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  203.         end
  204.     end
  205. end
  206.  
  207. LastSeenTbl.GenerateItemKey = function(itemID)
  208.     return itemID .. LastSeenAccountKey .. string.byte(itemID);
  209. end
  210.  
  211. LastSeenTbl.LootDetected = function(constant, currentDate, currentMap, itemSource, questID)
  212.     if not constant then return end; -- If the passed constant is nil, then simply return to avoid error.
  213.    
  214.     if LastSeenTbl.doNotUpdate then
  215.         LastSeenTbl.doNotUpdate = false;
  216.     end
  217.  
  218.     questID = questID or 0; -- The questID argument is an optional argument.
  219.  
  220.     local link = LastSeenTbl.ExtractItemLink(constant); if not link then return end;
  221.    
  222.     if select(1, GetItemInfoInstant(link)) == 0 then return end; -- This is here for items like pet cages.
  223.    
  224.     -- The item passed isn't a looted item, but a received item from something else.
  225.     -- Let's figure out what that source is.
  226.     if itemSource == L["IS_QUEST_ITEM"] and questID ~= 0 then -- Quest Item
  227.         -- The item received was a quest reward and shouldn't be handled by the ItemHandler.
  228.         LastSeenTbl.QuestChoices(questID, link, currentDate);
  229.         return;
  230.     --Modified This Area (Vandiel)
  231.     elseif itemSource == L["IS_MISCELLANEOUS"] or itemSource == L["IS_CONSUMABLE"] or itemSource == L["IS_OBJECT"] or itemSource == L["AUCTION_HOUSE_SOURCE"] then
  232.         PlayerLootedContainer(link, currentDate, currentMap, itemSource);
  233.     else
  234.         link = LastSeenTbl.ExtractItemLink(constant); -- Just an item looted from a creature. Simple; classic.
  235.     end
  236.  
  237.     if not LastSeenTbl.lootControl then -- Track items when they're looted.
  238.         local itemID = select(1, GetItemInfoInstant(link)); if not itemID then return end;
  239.         local itemLink = select(2, GetItemInfo(itemID));
  240.         local itemName = select(1, GetItemInfo(itemID));
  241.         local itemRarity = select(3, GetItemInfo(itemID));
  242.         local itemType = select(6, GetItemInfo(itemID));
  243.         local itemSourceCreatureID = LastSeenTbl.itemsToSource[itemID];
  244.        
  245.         if itemRarity >= LastSeenSettingsCacheDB.rarity then
  246.             for k, v in pairs(LastSeenTbl.ignoredItemTypes) do
  247.                 if itemType == v and not LastSeenTbl.doNotIgnore then
  248.                     return;
  249.                 end
  250.             end
  251.             for k, v in pairs(LastSeenTbl.ignoredItems) do
  252.                 if itemID == k and not LastSeenTbl.doNotIgnore then
  253.                     return;
  254.                 end
  255.             end
  256.             if LastSeenIgnoredItemsDB[itemID] and LastSeenTbl.doNotIgnore then
  257.                 return;
  258.             end
  259.  
  260.             if LastSeenItemsDB[itemID] then -- This is an update situation because the item has been looted before.
  261.                 if itemSourceCreatureID ~= nil then
  262.                     LastSeenTbl.Update(manualEntry, itemID, itemName, itemLink, itemType, itemRarity, currentDate, LastSeenCreaturesDB[itemSourceCreatureID].unitName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  263.                 elseif LastSeenTbl.encounterName ~= "" then
  264.                     LastSeenTbl.Update(manualEntry, itemID, itemName, itemLink, itemType, itemRarity, currentDate, LastSeenTbl.encounterName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  265.                 end
  266.             else -- An item seen for the first time.
  267.                 if itemSourceCreatureID ~= nil then
  268.                     if LastSeenCreaturesDB[itemSourceCreatureID] and not LastSeenTbl.isMailboxOpen then
  269.                         if not LastSeenTbl.isAutoLootPlusLoaded then
  270.                             LastSeenTbl.New(itemID, itemName, itemLink, itemRarity, itemType, currentDate, LastSeenCreaturesDB[itemSourceCreatureID].unitName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  271.                         end
  272.                     else
  273.                         print(L["ADDON_NAME"] .. L["UNABLE_TO_DETERMINE_SOURCE"] .. itemLink .. ". " .. L["DISCORD_REPORT"]);
  274.                     end
  275.                 elseif LastSeenTbl.encounterName ~= "" then
  276.                     LastSeenTbl.New(itemID, itemName, itemLink, itemType, itemRarity, currentDate, LastSeenTbl.encounterName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  277.                 end
  278.             end
  279.         elseif LastSeenTbl.TableHasField(LastSeenItemsDB, itemID, "manualEntry") then
  280.             if LastSeenItemsDB[itemID] then -- This is an update situation because the item has been looted before.
  281.                 if itemSourceCreatureID ~= nil then
  282.                     LastSeenTbl.Update(manualEntry, itemID, itemName, itemLink, itemType, itemRarity, today, LastSeenCreaturesDB[itemSourceCreatureID].unitName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  283.                 elseif LastSeenTbl.encounterName ~= "" then
  284.                     LastSeenTbl.Update(manualEntry, itemID, itemName, itemLink, itemType, itemRarity, currentDate, LastSeenTbl.encounterName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  285.                 end
  286.             else -- An item seen for the first time.
  287.                 if itemSourceCreatureID ~= nil then
  288.                     if LastSeenCreaturesDB[itemSourceCreatureID] and not LastSeenTbl.isMailboxOpen then
  289.                         if not LastSeenTbl.isAutoLootPlusLoaded then
  290.                             LastSeenTbl.New(itemID, itemName, itemLink, itemRarity, itemType, today, LastSeenCreaturesDB[itemSourceCreatureID].unitName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  291.                         end
  292.                     elseif LastSeenTbl.encounterName ~= "" then
  293.                         LastSeenTbl.New(itemID, itemName, itemLink, itemType, itemRarity, currentDate, LastSeenTbl.encounterName, currentMap, LastSeenTbl.GenerateItemKey(itemID));
  294.                     else
  295.                         print(L["ADDON_NAME"] .. L["UNABLE_TO_DETERMINE_SOURCE"] .. itemLink .. ". " .. L["DISCORD_REPORT"]);
  296.                     end
  297.                 end
  298.             end
  299.         end
  300.     end
  301. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement