Advertisement
Guest User

status

a guest
Feb 19th, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. ESX = nil
  2. local Status, isPaused = {}, false
  3.  
  4. Citizen.CreateThread(function()
  5. while ESX == nil do
  6. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  7. Citizen.Wait(0)
  8. end
  9. end)
  10.  
  11. function GetStatusData(minimal)
  12. local status = {}
  13.  
  14. for i=1, #Status, 1 do
  15. if minimal then
  16. table.insert(status, {
  17. name = Status[i].name,
  18. val = Status[i].val,
  19. percent = (Status[i].val / Config.StatusMax) * 100
  20. })
  21. else
  22. table.insert(status, {
  23. name = Status[i].name,
  24. val = Status[i].val,
  25. color = Status[i].color,
  26. visible = Status[i].visible(Status[i]),
  27. max = Status[i].max,
  28. percent = (Status[i].val / Config.StatusMax) * 100
  29. })
  30. end
  31. end
  32.  
  33. return status
  34. end
  35.  
  36. AddEventHandler('esx_status:registerStatus', function(name, default, color, visible, tickCallback)
  37. local status = CreateStatus(name, default, color, visible, tickCallback)
  38. table.insert(Status, status)
  39. end)
  40.  
  41. AddEventHandler('esx_status:unregisterStatus', function(name)
  42. for k,v in ipairs(Status) do
  43. if v.name == name then
  44. table.remove(Status, k)
  45. break
  46. end
  47. end
  48. end)
  49.  
  50. RegisterNetEvent('esx_status:load')
  51. AddEventHandler('esx_status:load', function(status)
  52. for i=1, #Status, 1 do
  53. for j=1, #status, 1 do
  54. if Status[i].name == status[j].name then
  55. Status[i].set(status[j].val)
  56. end
  57. end
  58. end
  59.  
  60. Citizen.CreateThread(function()
  61. while true do
  62. for i=1, #Status, 1 do
  63. Status[i].onTick()
  64. end
  65.  
  66. SendNUIMessage({
  67. update = true,
  68. status = GetStatusData()
  69. })
  70.  
  71. TriggerEvent('esx_customui:onTick', GetStatusData(true))
  72. Citizen.Wait(Config.TickTime)
  73. end
  74. end)
  75. end)
  76.  
  77. RegisterNetEvent('esx_status:set')
  78. AddEventHandler('esx_status:set', function(name, val)
  79. for i=1, #Status, 1 do
  80. if Status[i].name == name then
  81. Status[i].set(val)
  82. break
  83. end
  84. end
  85.  
  86. SendNUIMessage({
  87. update = true,
  88. status = GetStatusData()
  89. })
  90.  
  91. TriggerServerEvent('esx_status:update', GetStatusData(true))
  92. end)
  93.  
  94. RegisterNetEvent('esx_status:add')
  95. AddEventHandler('esx_status:add', function(name, val)
  96. for i=1, #Status, 1 do
  97. if Status[i].name == name then
  98. Status[i].add(val)
  99. break
  100. end
  101. end
  102.  
  103. SendNUIMessage({
  104. update = true,
  105. status = GetStatusData()
  106. })
  107.  
  108. TriggerServerEvent('esx_status:update', GetStatusData(true))
  109. end)
  110.  
  111. RegisterNetEvent('esx_status:remove')
  112. AddEventHandler('esx_status:remove', function(name, val)
  113. for i=1, #Status, 1 do
  114. if Status[i].name == name then
  115. Status[i].remove(val)
  116. break
  117. end
  118. end
  119.  
  120. SendNUIMessage({
  121. update = true,
  122. status = GetStatusData()
  123. })
  124.  
  125. TriggerServerEvent('esx_status:update', GetStatusData(true))
  126. end)
  127.  
  128. AddEventHandler('esx_status:getStatus', function(name, cb)
  129. for i=1, #Status, 1 do
  130. if Status[i].name == name then
  131. cb(Status[i])
  132. return
  133. end
  134. end
  135. end)
  136.  
  137. AddEventHandler('esx_status:setDisplay', function(val)
  138. SendNUIMessage({
  139. setDisplay = true,
  140. display = val
  141. })
  142. end)
  143.  
  144. -- Pause menu disable hud display
  145. Citizen.CreateThread(function()
  146. while true do
  147. Citizen.Wait(300)
  148.  
  149. if IsPauseMenuActive() and not isPaused then
  150. isPaused = true
  151. TriggerEvent('esx_status:setDisplay', 0.0)
  152. elseif not IsPauseMenuActive() and isPaused then
  153. isPaused = false
  154. TriggerEvent('esx_status:setDisplay', 0.5)
  155. end
  156. end
  157. end)
  158.  
  159. -- Loaded event
  160. Citizen.CreateThread(function()
  161. TriggerEvent('esx_status:loaded')
  162. end)
  163.  
  164. -- Update server
  165. Citizen.CreateThread(function()
  166. while true do
  167. Citizen.Wait(Config.UpdateInterval)
  168.  
  169. TriggerServerEvent('esx_status:update', GetStatusData(true))
  170. end
  171. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement