Advertisement
Rochet2

[Lua] Automatic gossip menu creation

Dec 14th, 2011
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local NPC_Entry = 180001
  2.  
  3. local T = {}
  4.  
  5. function T.Load(pPlayer)
  6.     T[tostring(pPlayer:GetGUID())] = {}
  7.     local Query = WorldDBQuery("SELECT entry, name1 FROM items;")
  8.     if(Query == nil) then
  9.         return
  10.     else
  11.         for i = 1, Query:GetRowCount() do
  12.             T[tostring(pPlayer:GetGUID())][i] = {Query:GetColumn(0):GetLong(), Query:GetColumn(1):GetString()}
  13.             Query:NextRow()
  14.         end
  15.     end
  16. end
  17.  
  18. function T.Hello(pUnit, event, pPlayer)
  19.     pUnit:GossipCreateMenu(100, pPlayer, 0)
  20.     T.Load(pPlayer)
  21.     local GUID = tostring(pPlayer:GetGUID())
  22.     for k,v in ipairs(T[GUID]) do
  23.         pUnit:GossipMenuAddItem(0, v[2], k, 0, '', 0)
  24.         if(k == 29) then
  25.             pUnit:GossipMenuAddItem(7, "Next page", #T[GUID]+2, 0, '', 0)
  26.             pUnit:GossipMenuAddItem(7, "Jump to page", #T[GUID]+math.floor(#T[GUID]/30)+2, 1, 'Jump to a page 1-'..math.floor(#T[GUID]/30)+1, 0)
  27.             break
  28.         end
  29.     end
  30.     pUnit:GossipSendMenu(pPlayer)
  31. end
  32.  
  33. function T.Select(pUnit, event, pPlayer, id, intid, code)
  34.     -- print(intid)
  35.     local GUID = tostring(pPlayer:GetGUID())
  36.     if(intid == #T[GUID]+1) then
  37.         T.Hello(pUnit, event, pPlayer)
  38.         return
  39.     elseif(intid == #T[GUID]+math.floor(#T[GUID]/30)+2) then
  40.         local code = tonumber(code)
  41.         local A = false
  42.         if(code == nil or code <= 0) then
  43.             T.Hello(pUnit, event, pPlayer)
  44.             return
  45.         end
  46.         local code = math.ceil(code)
  47.         if(code > math.floor(#T[GUID]/30)+1) then
  48.             T.Hello(pUnit, event, pPlayer)
  49.             return
  50.         end
  51.         T.Select(pUnit, event, pPlayer, 0, #T[GUID]+code, 0)
  52.         return
  53.     elseif(intid > #T[GUID]) then
  54.         pUnit:GossipCreateMenu(100, pPlayer, 0)
  55.         local X = (30*(intid-#T[GUID]-1)) -- not sure what this was, but it seems important :3
  56.         for i = X, #T[GUID] do
  57.             pUnit:GossipMenuAddItem(0, T[GUID][i][2], i, 0, '', 0)
  58.             if(i-X == 29) then
  59.                 if(i < #T[GUID]) then
  60.                     pUnit:GossipMenuAddItem(7, "Next page", intid+1, 0, '', 0)
  61.                 end
  62.                 break
  63.             end
  64.         end
  65.         pUnit:GossipMenuAddItem(7, "Previous page", intid-1, 0, '', 0)
  66.         pUnit:GossipSendMenu(pPlayer)
  67.     else
  68.         -- normal scripts.
  69.         -- {
  70.        
  71.        
  72.             -- example
  73.             pPlayer:AddItem(T[GUID][intid][1], 1)
  74.        
  75.        
  76.        
  77.         -- }
  78.         -- return to current menu, avoid glitch and dont exit gossip:
  79.         if(intid < 30) then
  80.             T.Hello(pUnit, event, pPlayer)
  81.         else
  82.             T.Select(pUnit, event, pPlayer, 0, math.floor(intid/30)+#T[GUID]+1, 0)
  83.         end
  84.     end
  85. end
  86.  
  87. RegisterUnitGossipEvent(NPC_Entry, 1, T.Hello)
  88. RegisterUnitGossipEvent(NPC_Entry, 2, T.Select)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement