Advertisement
Guest User

SellJunk sell greens

a guest
Oct 14th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.78 KB | None | 0 0
  1. SellJunk = LibStub("AceAddon-3.0"):NewAddon("SellJunk", "AceConsole-3.0","AceEvent-3.0")
  2. local addon = LibStub("AceAddon-3.0"):GetAddon("SellJunk")
  3. local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
  4. local AceConfigDialog = LibStub("AceConfigDialog-3.0")
  5.  
  6. local addonName, addonTable = ...
  7. local L = addonTable.L
  8. local _
  9.  
  10. addon.optionsFrame = {}
  11. local options = nil
  12.  
  13. addon.sellButton = CreateFrame("Button", nil, MerchantFrame, "OptionsButtonTemplate")
  14.  
  15. if IsAddOnLoaded("GnomishVendorShrinker") then
  16.   addon.sellButton:SetPoint("TOPRIGHT", -23, 0)
  17. else
  18.   addon.sellButton:SetPoint("TOPLEFT", 60, -32)
  19. end
  20.  
  21. addon.sellButton:SetText(L["Sell Junk"])
  22. addon.sellButton:SetScript("OnClick", function() SellJunk:Sell() end)
  23.  
  24. -- upvalues
  25. local floor = floor
  26. local mod = mod
  27. local string_find = string.find
  28. local pairs = pairs
  29. local wipe = wipe
  30. local DeleteCursorItem = DeleteCursorItem
  31. local GetContainerItemInfo = GetContainerItemInfo
  32. local GetItemInfo = GetItemInfo
  33. local PickupContainerItem = PickupContainerItem
  34. local PickupMerchantItem = PickupMerchantItem
  35.  
  36.  
  37. function addon:OnInitialize()
  38.   self:RegisterChatCommand("selljunk", "HandleSlashCommands")
  39.   self:RegisterChatCommand("sj", "HandleSlashCommands")
  40.  
  41.   self.db = LibStub("AceDB-3.0"):New("SellJunkDB")
  42.   self.db:RegisterDefaults({
  43.     char = {
  44.       auto = true,
  45.             max12 = false,
  46.             printGold = true,
  47.       showSpam = true
  48.     },
  49.     global = {
  50.       exceptions = {},
  51.     }
  52.   })
  53.  
  54.   self:PopulateOptions()
  55.   AceConfigRegistry:RegisterOptionsTable("SellJunk", options)
  56.   addon.optionsFrame = AceConfigDialog:AddToBlizOptions("SellJunk", nil, nil, "general")
  57. end
  58.  
  59. function addon:OnEnable()
  60.   self:RegisterEvent("MERCHANT_SHOW")
  61.     self.total = 0
  62. end
  63.  
  64. function addon:MERCHANT_SHOW() 
  65.   if addon.db.char.auto then
  66.     self:Sell()
  67.   end
  68. end
  69.  
  70. function addon:AddProfit(profit)
  71.     if profit then
  72.         self.total = self.total + profit
  73.     end
  74. end
  75.  
  76. -------------------------------------------------------------------
  77. -- Sells items:                                                  --
  78. --   - grey/green quality, unless it's in exception list         --
  79. --   - better than grey/green quality, if it's in exception list --
  80. -------------------------------------------------------------------
  81. function addon:Sell()
  82.     local limit = 0
  83.   local currPrice
  84.   local showSpam = addon.db.char.showSpam
  85.   local max12 = addon.db.char.max12
  86.  
  87.   for bag = 0,4 do
  88.     for slot = 1,GetContainerNumSlots(bag) do
  89.       local item = GetContainerItemLink(bag,slot)
  90.       if item then
  91.                 -- is it grey quality item?
  92.         local grey = string_find(item,"|cff9d9d9d")
  93.         local green = string_find(item,"|cff1eff00")
  94.  
  95.         if ((grey or green) and (not addon:isException(item))) or ((not (grey or green)) and (addon:isException(item))) then
  96.           currPrice = select(11, GetItemInfo(item)) * select(2, GetContainerItemInfo(bag, slot))
  97.           -- this should get rid of problems with grey items, that cant be sell to a vendor
  98.           if currPrice > 0 then
  99.             addon:AddProfit(currPrice)
  100.             PickupContainerItem(bag, slot)
  101.             PickupMerchantItem()
  102.             if showSpam then
  103.               self:Print(L["Sold"]..": "..item)
  104.             end
  105.  
  106.             if max12 then
  107.               limit = limit + 1
  108.               if limit == 12 then
  109.                 return
  110.               end
  111.             end
  112.           end
  113.         end
  114.       end
  115.     end
  116.   end
  117.  
  118.     if self.db.char.printGold then
  119.         self:PrintGold()
  120.     end
  121.     self.total = 0
  122. end
  123.  
  124. -------------------------------------------------------------
  125. -- Destroys items:                                         --
  126. --   - grey quality, unless it's in exception list         --
  127. --   - better than grey quality, if it's in exception list --
  128. -------------------------------------------------------------
  129. function addon:Destroy(count)
  130.   local limit = 9001 -- it's over NINE THOUSAND!!!
  131.   if count ~= nil then
  132.     limit = count
  133.   end
  134.  
  135.   local showSpam = addon.db.char.showSpam
  136.  
  137.   for bag = 0,4 do
  138.     for slot = 1,GetContainerNumSlots(bag) do
  139.       local item = GetContainerItemLink(bag,slot)
  140.       if item then
  141.                 -- is it grey quality item?
  142.         local grey = string_find(item,"|cff9d9d9d")
  143.  
  144.         if (grey and (not addon:isException(item))) or ((not grey) and (addon:isException(item))) then
  145.           PickupContainerItem(bag, slot)
  146.                     DeleteCursorItem()
  147.           if showSpam then
  148.             self:Print(L["Destroyed"]..": "..item)
  149.           end
  150.           limit = limit - 1
  151.           if limit == 0 then
  152.             break
  153.           end
  154.         end
  155.       end
  156.     end
  157.     if limit == 0 then
  158.       break
  159.     end
  160.   end
  161.  
  162.     if self.db.char.printGold then
  163.         self:PrintGold()
  164.     end
  165.     self.total = 0
  166. end
  167.  
  168. function addon:PrintGold()
  169.     local ret = ""
  170.     local gold = floor(self.total / (COPPER_PER_SILVER * SILVER_PER_GOLD));
  171.     local silver = floor((self.total - (gold * COPPER_PER_SILVER * SILVER_PER_GOLD)) / COPPER_PER_SILVER);
  172.     local copper = mod(self.total, COPPER_PER_SILVER);
  173.     if gold > 0 then
  174.         ret = gold.." "..L["gold"].." "
  175.     end
  176.     if silver > 0 or gold > 0 then
  177.         ret = ret..silver .." "..L["silver"].." "
  178.     end
  179.     ret = ret..copper.." "..L["copper"]
  180.     if silver > 0 or gold > 0  or copper > 0 then
  181.         self:Print(L["Gained"]..": "..ret)
  182.     end
  183. end
  184.  
  185. function addon:Add(link)
  186.  
  187.     -- remove all trailing whitespace
  188.     link = strtrim(link)
  189.  
  190.     -- extract name from an itemlink
  191.   local found, _, name = string_find(link, "^|c%x+|H.+|h.(.*)\].+")
  192.  
  193.     -- if it's not an itemlink, guess it's name of an item
  194.     if not found then
  195.         name = link
  196.     end
  197.  
  198.   local exceptions = self.db.global.exceptions
  199.   for k,v in pairs(exceptions) do
  200.     if v == name or v == link then
  201.       return
  202.     end
  203.   end
  204.  
  205.   -- append name of the item to global exception list
  206.   exceptions[#exceptions + 1] = name
  207.   self:Print(L["Added"] .. ": " .. link)
  208. end
  209.  
  210. function addon:Rem(link)
  211.     -- remove all trailing whitespace
  212.     link = strtrim(link)
  213.  
  214.     -- extract name from an itemlink
  215.   local isLink, _, name = string_find(link, "^|c%x+|H.+|h.(.*)\].+")
  216.  
  217.     -- if it's not an itemlink, guess it's name of an item
  218.     if not isLink then
  219.         name = link
  220.     end
  221.  
  222.   -- looping through exceptions
  223.   local found = false
  224.   local exception
  225.   local exceptions = self.db.global.exceptions
  226.   for k,v in pairs(exceptions) do
  227.     found = false
  228.     -- comparing exception list entry with given name
  229.     if v:lower() == name:lower() then
  230.       found = true
  231.     end
  232.  
  233.     -- extract name from itemlink (only for compatibility with old saved variables)
  234.     isLink, _, exception = string_find(v, "^|c%x+|H.+|h.(.*)\].+")
  235.     if isLink then
  236.       -- comparing exception list entry with given name
  237.       if exception:lower() == name:lower() then
  238.         found = true
  239.       end
  240.     end
  241.  
  242.     if found then
  243.       if exceptions[k + 1] then
  244.         exceptions[k] = exceptions[k + 1]
  245.       else
  246.         exceptions[k] = nil
  247.       end
  248.       self:Print(L["Removed"]..": "..link)
  249.       break
  250.     end
  251.   end
  252. end
  253.  
  254. function addon:isException(link)
  255.     local exception = nil
  256.  
  257.     -- extracting name of an item from the itemlink
  258.     local isLink, _, name = string_find(link, "^|c%x+|H.+|h.(.*)\].+")
  259.  
  260.     -- it's not an itemlink, so guess it's name of the item
  261.     if not isLink then
  262.         name = link
  263.     end
  264.  
  265.   local exceptions = self.db.global.exceptions
  266.     if exceptions then
  267.  
  268.         -- looping through global exceptions
  269.         for k,v in pairs(exceptions) do
  270.  
  271.             -- comparing exception list entry with given name
  272.             if v:lower() == name:lower() then
  273.                 return true
  274.             end
  275.  
  276.             -- extract name from itemlink (only for compatibility with old saved variables)
  277.             isLink, _, exception = string_find(v, "^|c%x+|H.+|h.(.*)\].+")
  278.             if isLink then
  279.                 -- comparing exception list entry with given name
  280.                 if exception:lower() == name:lower() then
  281.                     return true
  282.                 end
  283.             end
  284.         end
  285.     end
  286.  
  287.     -- item not found in exception list
  288.     return false
  289. end
  290.  
  291. function addon:ClearDB()
  292.   wipe(self.db.global.exceptions)
  293.   self:Print(L["Exceptions succesfully cleared."])
  294. end
  295.  
  296. function addon:HandleSlashCommands(input)
  297.   local arg1, arg2 = self:GetArgs(input, 2, 1, input)
  298.   if arg1 == 'destroy' then
  299.     self:Destroy(arg2)
  300.   elseif arg1 == 'add' and arg2 ~= nil then
  301.     if arg2:find('|Hitem') == nil then
  302.       self:Print(L["Command accepts only itemlinks."])
  303.     else
  304.       self:Add(arg2, true)
  305.     end
  306.   elseif (arg1 == 'rem' or arg1 == 'remove') and arg2 ~= nil then
  307.     if arg2:find('|Hitem') == nil then
  308.       self:Print(L["Command accepts only itemlinks."])
  309.     else
  310.       self:Rem(arg2, true)
  311.     end
  312.   else
  313.     InterfaceOptionsFrame_OpenToCategory(addon.optionsFrame)
  314.   end
  315. end
  316.  
  317. function addon:PopulateOptions()
  318.     if not options then
  319.         options = {
  320.             order = 1,
  321.             type  = "group",
  322.             name  = "SellJunk",
  323.             args  = {
  324.                 general = {
  325.                     order   = 1,
  326.                     type    = "group",
  327.                     name    = "global",
  328.                     args    = {
  329.                         divider1 = {
  330.                             order   = 1,
  331.                             type    = "description",
  332.                             name    = "",
  333.                         },
  334.                         auto = {
  335.                             order   = 2,
  336.                             type    = "toggle",
  337.                             name    = L["Automatically sell junk"],
  338.                             desc    = L["Toggles the automatic selling of junk when the merchant window is opened."],
  339.                             get     = function() return addon.db.char.auto end,
  340.                             set     = function() self.db.char.auto = not self.db.char.auto end,
  341.                         },
  342.                         divider2 = {
  343.                             order   = 3,
  344.                             type    = "description",
  345.                             name    = "",
  346.                         },
  347.  
  348.                         max12 = {
  349.                             order = 4,
  350.                             type  = "toggle",
  351.                             name  = L["Sell max. 12 items"],
  352.                             desc  = L["This is failsafe mode. Will sell only 12 items in one pass. In case of an error, all items can be bought back from vendor."],
  353.                             get     = function() return addon.db.char.max12 end,
  354.                             set     = function() self.db.char.max12 = not self.db.char.max12 end,
  355.                         },
  356.                         divider3 = {
  357.                             order   = 5,
  358.                             type    = "description",
  359.                             name    = "",
  360.                         },
  361.                         printGold = {
  362.                             order = 6,
  363.                             type  = "toggle",
  364.                             name  = L["Show gold gained"],
  365.                             desc  = L["Shows gold gained from selling trash."],
  366.                             get     = function() return addon.db.char.printGold end,
  367.                             set     = function() self.db.char.printGold = not self.db.char.printGold end,
  368.                         },
  369.             divider4 = {
  370.                             order   = 7,
  371.                             type    = "description",
  372.                             name    = "",
  373.                         },
  374.             showSpam = {
  375.               order = 8,
  376.               type  = "toggle",
  377.               name  = L["Show 'item sold' spam"],
  378.               desc  = L["Prints itemlinks to chat, when automatically selling items."],
  379.               get   = function() return addon.db.char.showSpam end,
  380.               set   = function() addon.db.char.showSpam = not addon.db.char.showSpam end,
  381.             },
  382.                         divider5 = {
  383.                             order   = 9,
  384.                             type    = "header",
  385.                             name    = L["Clear exceptions"],
  386.                         },
  387.                         clearglobal = {
  388.                             order   = 10,
  389.                             type    = "execute",
  390.                             name    = L["Clear"],
  391.               desc  = L["Removes all exceptions."],
  392.                             func    = function() addon:ClearDB() end,
  393.                         },
  394.                         divider6 = {
  395.                             order   = 12,
  396.                             type    = "description",
  397.                             name    = "",
  398.                         },
  399.                         header1 = {
  400.                             order   = 13,
  401.                             type    = "header",
  402.                             name    = L["Exceptions"],
  403.                         },
  404.                         note1 = {
  405.                             order = 14,
  406.                             type    = "description",
  407.                             name    = L["Drag item into this window to add/remove it from exception list"],
  408.                         },
  409.                         add = {
  410.                             order   = 15,
  411.                             type    = "input",
  412.                             name    = L["Add item"]..':',
  413.                             usage = L["<Item Link>"],
  414.                             get     = false,
  415.                             set     = function(info, v) addon:Add(v) end,
  416.                         },
  417.                         rem = {
  418.                             order   = 16,
  419.                             type    = "input",
  420.                             name    = L["Remove item"]..':',
  421.                             usage   = L["<Item Link>"],
  422.                             get     = false,
  423.                             set     = function(info, v) addon:Rem(v) end,
  424.                         },
  425.                     }
  426.                 }
  427.             }
  428.         }
  429.     end
  430. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement