Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The Gossip NPC's entry and the amount of options on the screen.
- local NPC_Entry = 180002
- local Options = 12 -- Please do not use values over 29 or the gossip menu might have too many options (29 + 3 = 32 options. 31 is max.)
- local NoRefresh = true -- Incase you want to update the T.Data table on Gossip Hello, change NoRefresh to false.
- -- initialize the main table and the subtables.
- local T =
- {
- Page = {},
- }
- -- This defines how many intids are left untouched. 5 would mean intid's: 0,1,2,3,4, not 1,2,3,4,5.
- -- Defining this for later addtition / reduction of additional functions, like Jump to page etc.
- local Space = 4
- -- This function loads the data from the database by default. You can mold it, but it should populate the T.Data table with the amount of options at least.
- -- Currently this function has an example script, that will load the name and entry of items from the items table. (randomized amount of them for testing purposes)
- function T.LoadData(pPlayer)
- local Table
- if(not pPlayer) then
- T.Data = {}
- Table = T.Data
- else
- T.Data[tostring(pPlayer)] = {}
- Table = T.Data[tostring(pPlayer)]
- end
- local Query = WorldDBQuery("SELECT entry, name1 FROM items LIMIT "..math.random(100)..";")
- if(Query ~= nil) then
- for i = Space, Query:GetRowCount()+Space-1 do
- Table[i] = {Query:GetColumn(0):GetLong(), Query:GetColumn(1):GetString()}
- Query:NextRow()
- end
- local Amount
- local R, D = #Table%Options, #Table/Options
- if(R == 0) then
- Amount = D - 1
- elseif(R <= 5) then
- Amount = math.floor(D)
- else
- Amount = math.ceil(D) - 1
- end
- Table.Amount = Amount
- end
- end
- if(NoRefresh) then
- T.LoadData()
- end
- function T.GetMenu(pUnit, e, pPlayer, Page)
- local Table
- if(NoRefresh) then
- Table = T.Data
- else
- if(e ~= nil) then
- T.LoadData(pPlayer)
- end
- Table = T.Data[tostring(pPlayer)]
- end
- if(Page == nil or Page < 0) then -- On hello and faulty page, set to main menu.
- T.Page[tostring(pPlayer)] = 0
- Page = 0
- end
- pUnit:GossipCreateMenu(100, pPlayer, 0)
- local Rows, Count = #Table, T.Count(Page)
- local Max, Next = T.Max(Count, Rows)
- -- These 3 options can also be moved to the botton of the list, just put them under the for loop (above GossipSendMenu)
- -- I left them to the top for an easy access and use. If the option texts are long (like some item names are),
- -- the buttons will jump up and down if they are at the bottom.
- if(Next) then
- pUnit:GossipMenuAddItem(7, "Next page", 1, 0, '', 0)
- end
- if(Page > 0) then
- pUnit:GossipMenuAddItem(7, "Previous page", 2, 0, '', 0)
- end
- if(Rows > Options) then
- pUnit:GossipMenuAddItem(2, "Jump to page", 3, 1, 'Jump to a page 1-'..Table.Amount, 0)
- end
- for i = Count, Max do
- -- This loop creates our gossip options. Currently it takes the name of the option from T.Data column 2 (item name)
- pUnit:GossipMenuAddItem(4, Table[i][2], i, 0, '', 0)
- end
- pUnit:GossipSendMenu(pPlayer)
- end
- function T.Count(Page)
- if(Page == 0) then
- return Space
- else
- return Space+(Page*Options)
- end
- end
- function T.Max(Count, LData)
- if(LData - Count >= Options) then
- return Count+Options-1, true
- else
- return LData, false
- end
- end
- function T.Select(pUnit, event, pPlayer, id, intid, code)
- local str = tostring(pPlayer)
- local Table
- if(NoRefresh) then
- Table = T.Data
- else
- Table = T.Data[tostring(pPlayer)]
- end
- if(intid < Space) then
- -- This if statement contains the extra functions
- if(intid == 1) then -- Next page
- T.Page[str] = T.Page[str] + 1
- elseif(intid == 2) then -- Previous page
- T.Page[str] = T.Page[str] - 1
- elseif(intid == 3) then -- Jump to page
- local code = math.ceil(tonumber(code))
- if(code ~= nil and code >= 1 and code <= Table.Amount) then -- Check that the "code" (page) is suitable
- T.Page[str] = code-1
- end
- end
- else
- -- Place your script here, which will execute when a player clicks a normal option.
- -- example:
- -- Add item with the entry in T.Data table (column 1) based on the option we click
- pPlayer:AddItem(Table[intid][1], 1)
- end
- T.GetMenu(pUnit, nil, pPlayer, T.Page[str])
- end
- RegisterUnitGossipEvent(NPC_Entry, 1, T.GetMenu)
- RegisterUnitGossipEvent(NPC_Entry, 2, T.Select)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement