Advertisement
seishin77

ESX_Shops

Jun 9th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.18 KB | None | 0 0
  1. local Keys = {
  2.     ["ESC"] = 322, ["F1"] = 288, ["F2"] = 289, ["F3"] = 170, ["F5"] = 166, ["F6"] = 167, ["F7"] = 168, ["F8"] = 169, ["F9"] = 56, ["F10"] = 57,
  3.     ["~"] = 243, ["1"] = 157, ["2"] = 158, ["3"] = 160, ["4"] = 164, ["5"] = 165, ["6"] = 159, ["7"] = 161, ["8"] = 162, ["9"] = 163, ["-"] = 84, ["="] = 83, ["BACKSPACE"] = 177,
  4.     ["TAB"] = 37, ["Q"] = 44, ["W"] = 32, ["E"] = 38, ["R"] = 45, ["T"] = 245, ["Y"] = 246, ["U"] = 303, ["P"] = 199, ["["] = 39, ["]"] = 40, ["ENTER"] = 18,
  5.     ["CAPS"] = 137, ["A"] = 34, ["S"] = 8, ["D"] = 9, ["F"] = 23, ["G"] = 47, ["H"] = 74, ["K"] = 311, ["L"] = 182,
  6.     ["LEFTSHIFT"] = 21, ["Z"] = 20, ["X"] = 73, ["C"] = 26, ["V"] = 0, ["B"] = 29, ["N"] = 249, ["M"] = 244, [","] = 82, ["."] = 81,
  7.     ["LEFTCTRL"] = 36, ["LEFTALT"] = 19, ["SPACE"] = 22, ["RIGHTCTRL"] = 70,
  8.     ["HOME"] = 213, ["PAGEUP"] = 10, ["PAGEDOWN"] = 11, ["DELETE"] = 178,
  9.     ["LEFT"] = 174, ["RIGHT"] = 175, ["TOP"] = 27, ["DOWN"] = 173,
  10.     ["NENTER"] = 201, ["N4"] = 108, ["N5"] = 60, ["N6"] = 107, ["N+"] = 96, ["N-"] = 97, ["N7"] = 117, ["N8"] = 61, ["N9"] = 118
  11. }
  12.  
  13. ESX                           = nil
  14. local HasAlreadyEnteredMarker = false
  15. local LastZone                = nil
  16. local CurrentAction           = nil
  17. local CurrentActionMsg        = ''
  18. local CurrentActionData       = {}
  19.  
  20. Citizen.CreateThread(function()
  21.     while ESX == nil do
  22.         TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  23.         Citizen.Wait(0)
  24.     end
  25.  
  26.     Citizen.Wait(5000)
  27.  
  28.     ESX.TriggerServerCallback('esx_shops:requestDBItems', function(ShopItems)
  29.         for k,v in pairs(ShopItems) do
  30.             Config.Zones[k].Items = v
  31.         end
  32.     end)
  33. end)
  34.  
  35. function OpenShopMenu(zone)
  36.     local elements = {}
  37.     for i=1, #Config.Zones[zone].Items, 1 do
  38.         local item = Config.Zones[zone].Items[i]
  39.  
  40.         if item.limit == -1 then
  41.             item.limit = 100
  42.         end
  43.  
  44.         table.insert(elements, {
  45.             label      = ('%s - <span style="color:green;">%s</span>'):format(item.label, _U('shop_item', ESX.Math.GroupDigits(item.price))),
  46.             label_real = item.label,
  47.             item       = item.item,
  48.             price      = item.price,
  49.  
  50.             -- menu properties
  51.             value      = 1,
  52.             type       = 'slider',
  53.             min        = 1,
  54.             max        = item.limit
  55.         })
  56.     end
  57.  
  58.     ESX.UI.Menu.CloseAll()
  59.     ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop', {
  60.         title    = _U('shop'),
  61.         align    = 'bottom-right',
  62.         elements = elements
  63.     }, function(data, menu)
  64.         ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop_confirm', {
  65.             title    = _U('shop_confirm', data.current.value, data.current.label_real, ESX.Math.GroupDigits(data.current.price * data.current.value)),
  66.             align    = 'bottom-right',
  67.             elements = {
  68.                 {label = _U('no'),  value = 'no'},
  69.                 {label = _U('yes'), value = 'yes'}
  70.             }
  71.         }, function(data2, menu2)
  72.             if data2.current.value == 'yes' then
  73.                 TriggerServerEvent('esx_shops:buyItem', data.current.item, data.current.value, zone)
  74.             end
  75.  
  76.             menu2.close()
  77.         end, function(data2, menu2)
  78.             menu2.close()
  79.         end)
  80.     end, function(data, menu)
  81.         menu.close()
  82.  
  83.         CurrentAction     = 'shop_menu'
  84.         CurrentActionMsg  = _U('press_menu')
  85.         CurrentActionData = {zone = zone}
  86.     end)
  87. end
  88.  
  89. AddEventHandler('esx_shops:hasEnteredMarker', function(zone)
  90.     CurrentAction     = 'shop_menu'
  91.     CurrentActionMsg  = _U('press_menu')
  92.     CurrentActionData = {zone = zone}
  93. end)
  94.  
  95. AddEventHandler('esx_shops:hasExitedMarker', function(zone)
  96.     CurrentAction = nil
  97.     ESX.UI.Menu.CloseAll()
  98. end)
  99.  
  100. -- Display markers
  101. Citizen.CreateThread(function()
  102.     while true do
  103.         Citizen.Wait(0)
  104.         local coords = GetEntityCoords(PlayerPedId())
  105.  
  106.         for k,v in pairs(Config.Zones) do
  107.             for i = 1, #v.Pos, 1 do
  108.                 if(Config.Type ~= -1 and GetDistanceBetweenCoords(coords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true) < Config.DrawDistance) then
  109.                     DrawMarker(Config.Type, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.Size.x, Config.Size.y, Config.Size.z, Config.Color.r, Config.Color.g, Config.Color.b, 100, false, true, 2, false, false, false, false)
  110.                 end
  111.             end
  112.         end
  113.     end
  114. end)
  115.  
  116. -- Enter / Exit marker events
  117. Citizen.CreateThread(function()
  118.     while true do
  119.         Citizen.Wait(0)
  120.         local coords      = GetEntityCoords(PlayerPedId())
  121.         local isInMarker  = false
  122.         local currentZone = nil
  123.  
  124.         for k,v in pairs(Config.Zones) do
  125.             for i = 1, #v.Pos, 1 do
  126.                 if(GetDistanceBetweenCoords(coords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true) < Config.Size.x) then
  127.                     isInMarker  = true
  128.                     ShopItems   = v.Items
  129.                     currentZone = k
  130.                     LastZone    = k
  131.                 end
  132.             end
  133.         end
  134.         if isInMarker and not HasAlreadyEnteredMarker then
  135.             HasAlreadyEnteredMarker = true
  136.             TriggerEvent('esx_shops:hasEnteredMarker', currentZone)
  137.         end
  138.         if not isInMarker and HasAlreadyEnteredMarker then
  139.             HasAlreadyEnteredMarker = false
  140.             TriggerEvent('esx_shops:hasExitedMarker', LastZone)
  141.         end
  142.     end
  143. end)
  144.  
  145. -- Key Controls
  146. Citizen.CreateThread(function()
  147.     while true do
  148.         Citizen.Wait(0)
  149.  
  150.         if CurrentAction ~= nil then
  151.             ESX.ShowHelpNotification(CurrentActionMsg)
  152.  
  153.             if IsControlJustReleased(0, Keys['E']) then
  154.                 if CurrentAction == 'shop_menu' then
  155.                     OpenShopMenu(CurrentActionData.zone)
  156.                 end
  157.  
  158.                 CurrentAction = nil
  159.             end
  160.         else
  161.             Citizen.Wait(500)
  162.         end
  163.     end
  164. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement