Advertisement
CompCrafter

Untitled

Feb 11th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1.  
  2. local spawnedVehicles = {}
  3.  
  4. function OpenVehicleSpawnerMenu(type, station, part, partNum)
  5. local playerCoords = GetEntityCoords(PlayerPedId())
  6.  
  7. ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'vehicle', {
  8. title = _U('garage_title'),
  9. align = 'top-left',
  10. elements = {
  11. {label = _U('garage_storeditem'), action = 'garage'},
  12. {label = _U('garage_storeitem'), action = 'store_garage'},
  13. {label = _U('garage_buyitem'), action = 'buy_vehicle'}
  14. }}, function(data, menu)
  15. if data.current.action == 'buy_vehicle' then
  16. local shopElements = {}
  17. local shopCoords = Config.PoliceStations[station][part][partNum].InsideShop
  18. local authorizedVehicles = Config.AuthorizedVehicles[type][ESX.PlayerData.job.grade_name]
  19.  
  20. if #authorizedVehicles > 0 then
  21. for k,vehicle in ipairs(authorizedVehicles) do
  22. if IsModelInCdimage(vehicle.model) then
  23. local vehicleLabel = GetLabelText(GetDisplayNameFromVehicleModel(vehicle.model))
  24.  
  25. table.insert(shopElements, {
  26. label = ('%s - <span style="color:green;">%s</span>'):format(vehicleLabel, _U('shop_item', ESX.Math.GroupDigits(vehicle.price))),
  27. name = vehicleLabel,
  28. model = vehicle.model,
  29. price = vehicle.price,
  30. props = vehicle.props,
  31. type = type
  32. })
  33. end
  34. end
  35.  
  36. if #shopElements > 0 then
  37. OpenShopMenu(shopElements, playerCoords, shopCoords)
  38. else
  39. --ESX.ShowNotification(_U('garage_notauthorized'))
  40. exports['okokNotify']:Alert("Polizei Garage", "Du bist nicht berechtigt ein Fahrzeug zu kaufen", 5000, 'info')
  41. end
  42. else
  43. --ESX.ShowNotification(_U('garage_notauthorized'))
  44. exports['okokNotify']:Alert("Polizei Garage", "Du bist nicht berechtigt ein Fahrzeug zu kaufen", 5000, 'error')
  45. end
  46. elseif data.current.action == 'garage' then
  47. local garage = {}
  48.  
  49. ESX.TriggerServerCallback('esx_vehicleshop:retrieveJobVehicles', function(jobVehicles)
  50. if #jobVehicles > 0 then
  51. local allVehicleProps = {}
  52.  
  53. for k,v in ipairs(jobVehicles) do
  54. local props = json.decode(v.vehicle)
  55.  
  56. if IsModelInCdimage(props.model) then
  57. local vehicleName = GetLabelText(GetDisplayNameFromVehicleModel(props.model))
  58. local label = ('%s - <span style="color:darkgoldenrod;">%s</span>: '):format(vehicleName, props.plate)
  59.  
  60. if v.stored then
  61. label = label .. ('<span style="color:green;">%s</span>'):format(_U('garage_stored'))
  62. else
  63. label = label .. ('<span style="color:darkred;">%s</span>'):format(_U('garage_notstored'))
  64. end
  65.  
  66. table.insert(garage, {
  67. label = label,
  68. stored = v.stored,
  69. model = props.model,
  70. plate = props.plate
  71. })
  72.  
  73. allVehicleProps[props.plate] = props
  74. end
  75. end
  76.  
  77. if #garage > 0 then
  78. ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'vehicle_garage', {
  79. title = _U('garage_title'),
  80. align = 'top-left',
  81. elements = garage
  82. }, function(data2, menu2)
  83. if data2.current.stored then
  84. local foundSpawn, spawnPoint = GetAvailableVehicleSpawnPoint(station, part, partNum)
  85.  
  86. if foundSpawn then
  87. menu2.close()
  88.  
  89. ESX.Game.SpawnVehicle(data2.current.model, spawnPoint.coords, spawnPoint.heading, function(vehicle)
  90. local vehicleProps = allVehicleProps[data2.current.plate]
  91. ESX.Game.SetVehicleProperties(vehicle, vehicleProps)
  92.  
  93. TriggerServerEvent('esx_vehicleshop:setJobVehicleState', data2.current.plate, false)
  94. --ESX.ShowNotification(_U('garage_released'))
  95. exports['okokNotify']:Alert("Polizei Garage", "Fahrzeug ausgeparkt", 5000, 'success')
  96. end)
  97. end
  98. else
  99. --ESX.ShowNotification(_U('garage_notavailable'))
  100. exports['okokNotify']:Alert("Polizei Garage", "Dein Fahrzeug steht nicht in der Garage", 5000, 'info')
  101. end
  102. end, function(data2, menu2)
  103. menu2.close()
  104. end)
  105. else
  106. ESX.ShowNotification(_U('garage_empty'))
  107. exports['okokNotify']:Alert("Polizei Garage", "test", 5000, 'error')
  108. end
  109. else
  110. --ESX.ShowNotification(_U('garage_empty'))
  111. exports['okokNotify']:Alert("Polizei Garage", "Deine Garage ist leer", 5000, 'error')
  112. end
  113. end, type)
  114. elseif data.current.action == 'store_garage' then
  115. StoreNearbyVehicle(playerCoords)
  116. end
  117. end, function(data, menu)
  118. menu.close()
  119. end)
  120. end
  121.  
  122. function StoreNearbyVehicle(playerCoords)
  123. local vehicles, vehiclePlates = ESX.Game.GetVehiclesInArea(playerCoords, 30.0), {}
  124.  
  125. if #vehicles > 0 then
  126. for k,v in ipairs(vehicles) do
  127.  
  128. -- Make sure the vehicle we're saving is empty, or else it wont be deleted
  129. if GetVehicleNumberOfPassengers(v) == 0 and IsVehicleSeatFree(v, -1) then
  130. table.insert(vehiclePlates, {
  131. vehicle = v,
  132. plate = ESX.Math.Trim(GetVehicleNumberPlateText(v))
  133. })
  134. end
  135. end
  136. else
  137. --ESX.ShowNotification(_U('garage_store_nearby'))
  138. exports['okokNotify']:Alert("Polizei Garage", "Kein Fahrzeug in der Nähe", 5000, 'info')
  139. return
  140. end
  141.  
  142. ESX.TriggerServerCallback('esx_policejob:storeNearbyVehicle', function(storeSuccess, foundNum)
  143. if storeSuccess then
  144. local vehicleId = vehiclePlates[foundNum]
  145. local attempts = 0
  146. ESX.Game.DeleteVehicle(vehicleId.vehicle)
  147. IsBusy = true
  148.  
  149. Citizen.CreateThread(function()
  150. BeginTextCommandBusyspinnerOn('STRING')
  151. AddTextComponentSubstringPlayerName(_U('garage_storing'))
  152. EndTextCommandBusyspinnerOn(4)
  153.  
  154. while IsBusy do
  155. Citizen.Wait(100)
  156. end
  157.  
  158. BusyspinnerOff()
  159. end)
  160.  
  161. -- Workaround for vehicle not deleting when other players are near it.
  162. while DoesEntityExist(vehicleId.vehicle) do
  163. Citizen.Wait(500)
  164. attempts = attempts + 1
  165.  
  166. -- Give up
  167. if attempts > 30 then
  168. break
  169. end
  170.  
  171. vehicles = ESX.Game.GetVehiclesInArea(playerCoords, 30.0)
  172. if #vehicles > 0 then
  173. for k,v in ipairs(vehicles) do
  174. if ESX.Math.Trim(GetVehicleNumberPlateText(v)) == vehicleId.plate then
  175. ESX.Game.DeleteVehicle(v)
  176. break
  177. end
  178. end
  179. end
  180. end
  181.  
  182. IsBusy = false
  183. --ESX.ShowNotification(_U('garage_has_stored'))
  184. exports['okokNotify']:Alert("Polizei Garage", "Fahrzeug eingeparkt", 5000, 'success')
  185. else
  186. --ESX.ShowNotification(_U('garage_has_notstored'))
  187. exports['okokNotify']:Alert("Polizei Garage", "Kein Fahrzeug gefunden die dir gehören", 5000, 'info')
  188. end
  189. end, vehiclePlates)
  190. end
  191.  
  192. function GetAvailableVehicleSpawnPoint(station, part, partNum)
  193. local spawnPoints = Config.PoliceStations[station][part][partNum].SpawnPoints
  194. local found, foundSpawnPoint = false, nil
  195.  
  196. for i=1, #spawnPoints, 1 do
  197. if ESX.Game.IsSpawnPointClear(spawnPoints[i].coords, spawnPoints[i].radius) then
  198. found, foundSpawnPoint = true, spawnPoints[i]
  199. break
  200. end
  201. end
  202.  
  203. if found then
  204. return true, foundSpawnPoint
  205. else
  206. --ESX.ShowNotification(_U('vehicle_blocked'))
  207. exports['okokNotify']:Alert("Polizei Garage", "Alle freien Ausparkplätze sind belegt", 5000, 'error')
  208. return false
  209. end
  210. end
  211.  
  212. function OpenShopMenu(elements, restoreCoords, shopCoords)
  213. local playerPed = PlayerPedId()
  214. isInShopMenu = true
  215.  
  216. ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'vehicle_shop', {
  217. title = _U('vehicleshop_title'),
  218. align = 'top-left',
  219. elements = elements
  220. }, function(data, menu)
  221. ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'vehicle_shop_confirm', {
  222. title = _U('vehicleshop_confirm', data.current.name, data.current.price),
  223. align = 'top-left',
  224. elements = {
  225. {label = _U('confirm_no'), value = 'no'},
  226. {label = _U('confirm_yes'), value = 'yes'}
  227. }}, function(data2, menu2)
  228. if data2.current.value == 'yes' then
  229. local newPlate = exports['esx_vehicleshop']:GeneratePlate()
  230. local vehicle = GetVehiclePedIsIn(playerPed, false)
  231. local props = ESX.Game.GetVehicleProperties(vehicle)
  232. props.plate = newPlate
  233.  
  234. ESX.TriggerServerCallback('esx_policejob:buyJobVehicle', function (bought)
  235. if bought then
  236. --ESX.ShowNotification(_U('vehicleshop_bought', data.current.name, ESX.Math.GroupDigits(data.current.price)))
  237. exports['okokNotify']:Alert("Polizei Garage", data.current.name.. " für " ..ESX.Math.GroupDigits(data.current.price).. " gekauft", 5000, 'success')
  238.  
  239. isInShopMenu = false
  240. ESX.UI.Menu.CloseAll()
  241. DeleteSpawnedVehicles()
  242. FreezeEntityPosition(playerPed, false)
  243. SetEntityVisible(playerPed, true)
  244.  
  245. ESX.Game.Teleport(playerPed, restoreCoords)
  246. else
  247. --ESX.ShowNotification(_U('vehicleshop_money'))
  248. exports['okokNotify']:Alert("Polizei Garage", "Du kannst dir das Fahrzeug nicht leisten", 5000, 'info')
  249. menu2.close()
  250. end
  251. end, props, data.current.type)
  252. else
  253. menu2.close()
  254. end
  255. end, function(data2, menu2)
  256. menu2.close()
  257. end)
  258. end, function(data, menu)
  259. isInShopMenu = false
  260. ESX.UI.Menu.CloseAll()
  261.  
  262. DeleteSpawnedVehicles()
  263. FreezeEntityPosition(playerPed, false)
  264. SetEntityVisible(playerPed, true)
  265.  
  266. ESX.Game.Teleport(playerPed, restoreCoords)
  267. end, function(data, menu)
  268. DeleteSpawnedVehicles()
  269. WaitForVehicleToLoad(data.current.model)
  270.  
  271. ESX.Game.SpawnLocalVehicle(data.current.model, shopCoords, 0.0, function(vehicle)
  272. table.insert(spawnedVehicles, vehicle)
  273. TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
  274. FreezeEntityPosition(vehicle, true)
  275. SetModelAsNoLongerNeeded(data.current.model)
  276.  
  277. if data.current.props then
  278. ESX.Game.SetVehicleProperties(vehicle, data.current.props)
  279. end
  280. end)
  281. end)
  282.  
  283. WaitForVehicleToLoad(elements[1].model)
  284. ESX.Game.SpawnLocalVehicle(elements[1].model, shopCoords, 0.0, function(vehicle)
  285. table.insert(spawnedVehicles, vehicle)
  286. TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
  287. FreezeEntityPosition(vehicle, true)
  288. SetModelAsNoLongerNeeded(elements[1].model)
  289.  
  290. if elements[1].props then
  291. ESX.Game.SetVehicleProperties(vehicle, elements[1].props)
  292. end
  293. end)
  294. end
  295.  
  296. Citizen.CreateThread(function()
  297. while true do
  298. Citizen.Wait(0)
  299.  
  300. if isInShopMenu then
  301. DisableControlAction(0, 75, true) -- Disable exit vehicle
  302. DisableControlAction(27, 75, true) -- Disable exit vehicle
  303. else
  304. Citizen.Wait(500)
  305. end
  306. end
  307. end)
  308.  
  309. function DeleteSpawnedVehicles()
  310. while #spawnedVehicles > 0 do
  311. local vehicle = spawnedVehicles[1]
  312. ESX.Game.DeleteVehicle(vehicle)
  313. table.remove(spawnedVehicles, 1)
  314. end
  315. end
  316.  
  317. function WaitForVehicleToLoad(modelHash)
  318. modelHash = (type(modelHash) == 'number' and modelHash or GetHashKey(modelHash))
  319.  
  320. if not HasModelLoaded(modelHash) then
  321. RequestModel(modelHash)
  322.  
  323. BeginTextCommandBusyspinnerOn('STRING')
  324. AddTextComponentSubstringPlayerName(_U('vehicleshop_awaiting_model'))
  325. EndTextCommandBusyspinnerOn(4)
  326.  
  327. while not HasModelLoaded(modelHash) do
  328. Citizen.Wait(0)
  329. DisableAllControlActions(0)
  330. end
  331.  
  332. BusyspinnerOff()
  333. end
  334. end
  335.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement