Advertisement
Rochet2

Untitled

May 8th, 2016
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ID = 36 -- NPC entry
  2. local Check = false -- true: Do not show items the player can't buy, false: show all vendor items in the list, even if the player does not have the mats.
  3.  
  4. local T = {}
  5. T.Items =
  6. {
  7.     --  {{GivenItem, Amount}, {ReqItem1, Amount}, {ReqItem2, Amount}, ... {ReqItemN, Amount}}
  8.     {{25, 1}, {35, 1}},
  9.     {{25, 1}, {36, 1}},
  10.     {{25, 1}, {37, 1}},
  11.     {{25, 1}, {38, 1}},
  12.     {{25, 1}, {39, 1}},
  13.     {{25, 2}, {40, 3}, {38, 1}, {39, 1}, {36, 4}}, -- Testing multiple req items
  14.     {{25, 1}, {40, 1}},
  15.     {{25, 1}, {40, 1}},
  16.     {{25, 1}, {40, 1}},
  17.     {{25, 1}, {40, 1}},
  18.     {{25, 1}, {40, 1}},
  19.     {{25, 1}, {40, 1}},
  20.     {{25, 1}, {40, 1}},
  21.     {{25, 1}, {40, 1}},
  22.     {{25, 1}, {40, 1}},
  23.     {{25, 1}, {40, 1}},
  24.     {{25, 1}, {40, 1}},
  25.     {{25, 1}, {40, 1}},
  26.     {{25, 1}, {40, 1}},
  27.     {{25, 1}, {40, 1}},
  28.     {{25, 1}, {40, 1}},
  29.     {{25, 1}, {40, 1}},
  30.     {{25, 1}, {40, 1}},
  31.     {{25, 1}, {40, 1}},
  32.     {{25, 1}, {40, 1}},
  33.     {{25, 1}, {40, 1}},
  34.     {{25, 1}, {40, 1}},
  35.     {{25, 1}, {40, 1}},
  36.     {{25, 1}, {40, 1}},
  37.     {{25, 1}, {40, 1}},
  38.     {{25, 1}, {40, 1}},
  39.     {{25, 1}, {40, 1}},
  40.     {{25, 1}, {40, 1}},
  41.     {{25, 1}, {40, 1}},
  42.     {{25, 1}, {40, 1}},
  43.     {{25, 1}, {40, 1}},
  44.     {{25, 1}, {40, 1}},
  45.     {{25, 1}, {40, 1}},
  46.     {{25, 1}, {40, 1}},
  47.     {{25, 1}, {40, 1}},
  48.     {{25, 1}, {40, 1}},
  49.     {{25, 1}, {40, 1}},
  50.     {{25, 1}, {40, 1}},
  51.     {{25, 1}, {40, 1}},
  52.     {{25, 1}, {40, 1}},
  53.     {{35, 2}, {40, 1}}, -- Testing vendor with item entry 35
  54. }
  55.  
  56. T.Page = {}
  57. T.Buy = {}
  58. T.Options = 20
  59. local X = 10
  60.  
  61. function T.Count(Page)
  62.     if(not Page or Page < 1) then
  63.         return 1
  64.     else
  65.         return (Page*T.Options)
  66.     end
  67. end
  68.  
  69. function T.Max(Count, LData)
  70.     if(LData - Count >= T.Options) then
  71.         return Count+T.Options-1, true
  72.     else
  73.         return LData, false
  74.     end
  75. end
  76.  
  77. function T.Name(Entry)
  78.     return WorldDBQuery("SELECT Name FROM item_template WHERE entry = "..Entry):GetString(0)
  79. end
  80.  
  81. function T.Hello(event, player, creature)
  82.     local Items = {}
  83.     for k,v in ipairs(T.Items) do
  84.         local Add = true
  85.         for i = 2, #v do
  86.             if(Check and (not player:HasItem(v[i][1]) or player:GetItemCount(v[i][1]) < v[i][2])) then
  87.                 Add = false
  88.                 break
  89.             end
  90.         end
  91.         if(Add) then
  92.             Items[#Items+1] = {k, v[1][1], v[1][2]}
  93.         end
  94.     end
  95.     local str = tostring(player)
  96.     if(not T.Page[str] or T.Page[str] < 0) then
  97.         T.Page[str] = 0
  98.     end
  99.     local Page = T.Page[str]
  100.     local Count = T.Count(Page)
  101.     local Max, Next = T.Max(Count, #Items)
  102.     if(Next) then
  103.         player:GossipMenuAddItem(7, "Next page", 0, 3, false, '', 0)
  104.     end
  105.     if(Page > 0) then
  106.         player:GossipMenuAddItem(7, "Previous page", 0, 4, false, '', 0)
  107.     end
  108.     player:GossipMenuAddItem(4, "Refresh", 0, 1, false, '', 0)
  109.     player:GossipMenuAddItem(1, "Preview page's items", 0, X, false, '', 0)
  110.     for k = Count, Max do
  111.         local Amount = ""
  112.         if(Items[k][3] > 1) then
  113.             Amount = Items[k][3].." "
  114.         end
  115.         local Name = T.Name(Items[k][2])
  116.         player:GossipMenuAddItem(3, Amount..Name, 0, X+Items[k][1], false, '', 0)
  117.     end
  118.     player:GossipSendMenu(100, creature)
  119. end
  120.  
  121. function T.Select(event, player, creature, id, intid, code)
  122.     local str = tostring(player)
  123.     if(intid == 3) then
  124.         T.Page[str] = T.Page[str] + 1
  125.     elseif(intid == 4) then
  126.         T.Page[str] = T.Page[str] - 1
  127.     elseif(intid == 5) then
  128.         print(event, player, creature, id, T.Buy[str], code)
  129.         T.Select(666, player, creature, id, T.Buy[str], code)
  130.         return
  131.     elseif(intid == X) then
  132.         player:GossipComplete()
  133.         VendorRemoveAllItems(creature:GetEntry())
  134.         local Items = {}
  135.         for k,v in ipairs(T.Items) do
  136.             local Add = true
  137.             for i = 2, #v do
  138.                 if(Check and (not player:HasItem(v[i][1]) or player:GetItemCount(v[i][1]) < v[i][2])) then
  139.                     Add = false
  140.                     break
  141.                 end
  142.             end
  143.             if(Add) then
  144.                 Items[#Items+1] = v[1][1]
  145.             end
  146.         end
  147.         local Page = T.Page[str]
  148.         local Count = T.Count(Page)
  149.         local Max, Next = T.Max(Count, #Items)
  150.         for k = Count, Max do
  151.             AddVendorItem(creature:GetEntry(), Items[k], -1, 0, 0)
  152.         end
  153.         player:SendListInventory(creature)
  154.         return
  155.     elseif(T.Items[intid-X]) then
  156.         if(event ~= 666) then
  157.             T.Buy[str] = intid
  158.             player:SendBroadcastMessage("You need these items to purchase:")
  159.             for i = 2, #T.Items[intid-X] do
  160.                 local Name = T.Name(T.Items[intid-X][i][1])
  161.                 player:SendBroadcastMessage(T.Items[intid-X][i][2].." \124cff00B0E4\124Hitem:"..T.Items[intid-X][i][1]..":0:0:0:0:0:0:0:0\124h["..Name.."]\124h\124r")
  162.             end
  163.             local Name = T.Name(T.Items[intid-X][1][1])
  164.             player:GossipMenuAddItem(1, "Buy "..T.Items[intid-X][1][2].." "..Name, 0, 5, false, "Buying "..T.Items[intid-X][1][2].." "..Name.."!", 0)
  165.             player:GossipMenuAddItem(6, "Show price", 0, intid, false, '', 0)
  166.             player:GossipMenuAddItem(7, "Back..", 0, 1, false, '', 0)
  167.             player:GossipSendMenu(100, creature)
  168.             return
  169.         else
  170.             local intid = intid-X
  171.             local Add = true
  172.             for i = 2, #T.Items[intid] do
  173.                 if(not player:HasItem(T.Items[intid][i][1]) or player:GetItemCount(T.Items[intid][i][1]) < T.Items[intid][i][2]) then
  174.                     Add = false
  175.                     break
  176.                 end
  177.             end
  178.             if(Add) then
  179.                 local Done = 0
  180.                 for k = 1, T.Items[intid][1][2] do
  181.                     if(player:AddItem(T.Items[intid][1][1], 1)) then
  182.                         Done = Done + 1
  183.                     else
  184.                         -- player:RemoveItem(T.Items[intid][1][1], Done)
  185.                         player:SendAreaTriggerMessage("Your inventory is too full")
  186.                         break
  187.                     end
  188.                 end
  189.                 if(Done == T.Items[intid][1][2]) then
  190.                     for i = 2, #T.Items[intid] do
  191.                         player:RemoveItem(T.Items[intid][i][1], T.Items[intid][i][2])
  192.                     end
  193.                 else
  194.                     T.Select(0, player, creature, id, intid+X, code)
  195.                     return
  196.                 end
  197.             else
  198.                 player:SendAreaTriggerMessage("You do not have the required items")
  199.                 if(not Check) then
  200.                     T.Select(0, player, creature, id, intid+X, code)
  201.                     return
  202.                 end
  203.             end
  204.         end
  205.     end
  206.     T.Hello(event, player, creature)
  207. end
  208.  
  209. RegisterCreatureGossipEvent(ID, 1, T.Hello)
  210. RegisterCreatureGossipEvent(ID, 2, T.Select)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement