Advertisement
Guest User

esx shops : client/main.lua

a guest
Oct 20th, 2018
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 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_shop: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_shop: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_shop:hasEnteredMarker', function(zone)
  59.  
  60. CurrentAction = 'shop_menu'
  61. CurrentActionMsg = _U('press_menu')
  62. CurrentActionData = {zone = zone}
  63.  
  64. end)
  65.  
  66. AddEventHandler('esx_shop: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, 0.1)
  81. SetBlipColour (blip, 2)
  82. SetBlipAsShortRange(blip, true)
  83. BeginTextCommandSetBlipName("STRING")
  84. EndTextCommandSetBlipName(blip)
  85. end
  86. end
  87. end)
  88.  
  89. -- Display markers
  90. Citizen.CreateThread(function()
  91. while true do
  92. Citizen.Wait(10)
  93. local coords = GetEntityCoords(GetPlayerPed(-1))
  94. for k,v in pairs(Config.Zones) do
  95. for i = 1, #v.Pos, 1 do
  96. if(Config.Type ~= -1 and GetDistanceBetweenCoords(coords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true) < Config.DrawDistance) then
  97. 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)
  98. end
  99. end
  100. end
  101. end
  102. end)
  103.  
  104. -- Enter / Exit marker events
  105. Citizen.CreateThread(function()
  106. while true do
  107. Citizen.Wait(10)
  108. local coords = GetEntityCoords(GetPlayerPed(-1))
  109. local isInMarker = false
  110. local currentZone = nil
  111.  
  112. for k,v in pairs(Config.Zones) do
  113. for i = 1, #v.Pos, 1 do
  114. if(GetDistanceBetweenCoords(coords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true) < Config.Size.x) then
  115. isInMarker = true
  116. ShopItems = v.Items
  117. currentZone = k
  118. LastZone = k
  119. end
  120. end
  121. end
  122. if isInMarker and not HasAlreadyEnteredMarker then
  123. HasAlreadyEnteredMarker = true
  124. TriggerEvent('esx_shop:hasEnteredMarker', currentZone)
  125. end
  126. if not isInMarker and HasAlreadyEnteredMarker then
  127. HasAlreadyEnteredMarker = false
  128. TriggerEvent('esx_shop:hasExitedMarker', LastZone)
  129. end
  130. end
  131. end)
  132.  
  133. -- Key Controls
  134. Citizen.CreateThread(function()
  135. while true do
  136. Citizen.Wait(10)
  137. if CurrentAction ~= nil then
  138.  
  139. SetTextComponentFormat('STRING')
  140. AddTextComponentString(CurrentActionMsg)
  141. DisplayHelpTextFromStringLabel(0, 0, 1, -1)
  142.  
  143. if IsControlJustReleased(0, 38) then
  144.  
  145. if CurrentAction == 'shop_menu' then
  146. OpenShopMenu(CurrentActionData.zone)
  147. end
  148.  
  149. CurrentAction = nil
  150.  
  151. end
  152.  
  153. end
  154. end
  155. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement