Advertisement
Nidhoggx

esx_shops client

May 3rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. ESX = nil
  2. local HasAlreadyEnteredMarker = false
  3. local LastZone = nil
  4. local CurrentAction = nil
  5. local CurrentActionMsg = ''
  6. local CurrentActionData = {}
  7.  
  8. Citizen.CreateThread(function()
  9. while ESX == nil do
  10. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  11. Citizen.Wait(0)
  12. end
  13. end)
  14.  
  15. AddEventHandler('onClientMapStart', function()
  16.  
  17. ESX.TriggerServerCallback('esx_shops:requestDBItems', function(ShopItems)
  18. for k,v in pairs(ShopItems) do
  19. Config.Zones[k].Items = v
  20. end
  21. end)
  22.  
  23. end)
  24.  
  25. function OpenShopMenu(zone)
  26. local elements = {}
  27. for i=1, #Config.Zones[zone].Items, 1 do
  28.  
  29. local item = Config.Zones[zone].Items[i]
  30. table.insert(elements, {
  31. label = item.label .. ' - <span style="color: green;">' .. item.price .. '$</span>',
  32. realLabel = item.label,
  33. value = item.name,
  34. price = item.price
  35. })
  36. end
  37.  
  38. ESX.UI.Menu.CloseAll()
  39. ESX.UI.Menu.Open(
  40. 'default', GetCurrentResourceName(), 'shop',
  41. {
  42. title = _U('shop'),
  43. align = 'bottom-right',
  44. elements = elements
  45. },
  46. function(data, menu)
  47. TriggerServerEvent('esx_shops:buyItem', data.current.value, data.current.price)
  48. end,
  49. function(data, menu)
  50. menu.close()
  51. CurrentAction = 'shop_menu'
  52. CurrentActionMsg = _U('press_menu')
  53. CurrentActionData = {zone = zone}
  54. end
  55. )
  56. end
  57.  
  58. AddEventHandler('esx_shops:hasEnteredMarker', function(zone)
  59.  
  60. CurrentAction = 'shop_menu'
  61. CurrentActionMsg = _U('press_menu')
  62. CurrentActionData = {zone = zone}
  63.  
  64. end)
  65.  
  66. AddEventHandler('esx_shops:hasExitedMarker', function(zone)
  67.  
  68. CurrentAction = nil
  69. ESX.UI.Menu.CloseAll()
  70.  
  71. end)
  72.  
  73. -- Create Blips
  74. Citizen.CreateThread(function()
  75. for k,v in pairs(Config.Zones) do
  76. for i = 1, #v.Pos, 1 do
  77. local blip = AddBlipForCoord(v.Pos[i].x, v.Pos[i].y, v.Pos[i].z)
  78. SetBlipSprite (blip, 52)
  79. SetBlipDisplay(blip, 4)
  80. SetBlipScale (blip, 1.0)
  81. SetBlipColour (blip, 2)
  82. SetBlipAsShortRange(blip, true)
  83. BeginTextCommandSetBlipName("STRING")
  84. AddTextComponentString(_U('shops'))
  85. EndTextCommandSetBlipName(blip)
  86. end
  87. end
  88. end)
  89.  
  90. -- Display markers
  91. Citizen.CreateThread(function()
  92. while true do
  93. Citizen.Wait(10)
  94. local coords = GetEntityCoords(GetPlayerPed(-1))
  95. for k,v in pairs(Config.Zones) do
  96. for i = 1, #v.Pos, 1 do
  97. if(Config.Type ~= -1 and GetDistanceBetweenCoords(coords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true) < Config.DrawDistance) then
  98. 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)
  99. end
  100. end
  101. end
  102. end
  103. end)
  104.  
  105. -- Enter / Exit marker events
  106. Citizen.CreateThread(function()
  107. while true do
  108. Citizen.Wait(10)
  109. local coords = GetEntityCoords(GetPlayerPed(-1))
  110. local isInMarker = false
  111. local currentZone = nil
  112.  
  113. for k,v in pairs(Config.Zones) do
  114. for i = 1, #v.Pos, 1 do
  115. if(GetDistanceBetweenCoords(coords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true) < Config.Size.x) then
  116. isInMarker = true
  117. ShopItems = v.Items
  118. currentZone = k
  119. LastZone = k
  120. end
  121. end
  122. end
  123. if isInMarker and not HasAlreadyEnteredMarker then
  124. HasAlreadyEnteredMarker = true
  125. TriggerEvent('esx_shops:hasEnteredMarker', currentZone)
  126. end
  127. if not isInMarker and HasAlreadyEnteredMarker then
  128. HasAlreadyEnteredMarker = false
  129. TriggerEvent('esx_shops:hasExitedMarker', LastZone)
  130. end
  131. end
  132. end)
  133.  
  134. -- Key Controls
  135. Citizen.CreateThread(function()
  136. while true do
  137. Citizen.Wait(10)
  138. if CurrentAction ~= nil then
  139.  
  140. SetTextComponentFormat('STRING')
  141. AddTextComponentString(CurrentActionMsg)
  142. DisplayHelpTextFromStringLabel(0, 0, 1, -1)
  143.  
  144. if IsControlJustReleased(0, 38) then
  145.  
  146. if CurrentAction == 'shop_menu' then
  147. OpenShopMenu(CurrentActionData.zone)
  148. end
  149.  
  150. CurrentAction = nil
  151.  
  152. end
  153.  
  154. end
  155. end
  156. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement