Advertisement
Guest User

client/main.lua

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