Advertisement
seishin77

Untitled

Jun 8th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.59 KB | None | 0 0
  1. AddEventHandler('es:playerLoaded', function(source, _player)
  2.  
  3. local _source = source
  4. local tasks = {}
  5.  
  6. local userData = {
  7. accounts = {},
  8. inventory = {},
  9. job = {},
  10. job2 = {},
  11. loadout = {},
  12. playerName = GetPlayerName(_source),
  13. lastPosition = nil
  14. }
  15.  
  16. TriggerEvent('es:getPlayerFromId', _source, function(player)
  17. -- Update user name in DB
  18. table.insert(tasks, function(cb)
  19. MySQL.Async.execute('UPDATE `users` SET `name` = @name WHERE `identifier` = @identifier', {
  20. ['@identifier'] = player.getIdentifier(),
  21. ['@name'] = userData.playerName
  22. }, function(rowsChanged)
  23. cb()
  24. end)
  25. end)
  26.  
  27. -- Get accounts
  28. table.insert(tasks, function(cb)
  29. MySQL.Async.fetchAll('SELECT * FROM `user_accounts` WHERE `identifier` = @identifier', {
  30. ['@identifier'] = player.getIdentifier()
  31. }, function(accounts)
  32. for i=1, #Config.Accounts, 1 do
  33. for j=1, #accounts, 1 do
  34. if accounts[j].name == Config.Accounts[i] then
  35. table.insert(userData.accounts, {
  36. name = accounts[j].name,
  37. money = accounts[j].money,
  38. label = Config.AccountLabels[accounts[j].name]
  39. })
  40. end
  41.  
  42. break
  43. end
  44. end
  45.  
  46. cb()
  47. end)
  48. end)
  49.  
  50. -- Get inventory
  51. table.insert(tasks, function(cb)
  52.  
  53. MySQL.Async.fetchAll('SELECT * FROM `user_inventory` WHERE `identifier` = @identifier', {
  54. ['@identifier'] = player.getIdentifier()
  55. }, function(inventory)
  56. local tasks2 = {}
  57.  
  58. for i=1, #inventory do
  59. local item = ESX.Items[inventory[i].item]
  60.  
  61. if item then
  62. table.insert(userData.inventory, {
  63. name = inventory[i].item,
  64. count = inventory[i].count,
  65. label = item.label,
  66. limit = item.limit,
  67. usable = ESX.UsableItemsCallbacks[inventory[i].item] ~= nil,
  68. rare = item.rare,
  69. canRemove = item.canRemove
  70. })
  71. else
  72. print(('es_extended: invalid item "%s" ignored!'):format(inventory[i].item))
  73. end
  74. end
  75.  
  76. for k,v in pairs(ESX.Items) do
  77. local found = false
  78.  
  79. for j=1, #userData.inventory do
  80. if userData.inventory[j].name == k then
  81. found = true
  82. break
  83. end
  84. end
  85.  
  86. if not found then
  87. table.insert(userData.inventory, {
  88. name = k,
  89. count = 0,
  90. label = ESX.Items[k].label,
  91. limit = ESX.Items[k].limit,
  92. usable = ESX.UsableItemsCallbacks[k] ~= nil,
  93. rare = ESX.Items[k].rare,
  94. canRemove = ESX.Items[k].canRemove
  95. })
  96.  
  97. local scope = function(item, identifier)
  98. table.insert(tasks2, function(cb2)
  99. MySQL.Async.execute('INSERT INTO user_inventory (identifier, item, count) VALUES (@identifier, @item, @count)', {
  100. ['@identifier'] = identifier,
  101. ['@item'] = item,
  102. ['@count'] = 0
  103. }, function(rowsChanged)
  104. cb2()
  105. end)
  106. end)
  107. end
  108.  
  109. scope(k, player.getIdentifier())
  110. end
  111.  
  112. end
  113.  
  114. Async.parallelLimit(tasks2, 5, function(results) end)
  115.  
  116. table.sort(userData.inventory, function(a,b)
  117. return a.label < b.label
  118. end)
  119.  
  120. cb()
  121. end)
  122.  
  123. end)
  124.  
  125. -- Get job and loadout
  126. table.insert(tasks, function(cb)
  127.  
  128. local tasks2 = {}
  129.  
  130. -- Get job name, grade and last position
  131. table.insert(tasks2, function(cb2)
  132.  
  133. MySQL.Async.fetchAll(
  134. 'SELECT * FROM `users` WHERE `identifier` = @identifier',
  135. {
  136. ['@identifier'] = player.getIdentifier()
  137. },
  138. function(result)
  139.  
  140. userData.job['name'] = result[1].job
  141. userData.job2['name'] = result[1].job2
  142. userData.job['grade'] = result[1].job_grade
  143. userData.job2['grade'] = result[1].job2_grade
  144.  
  145. if result[1].loadout ~= nil then
  146. userData.loadout = json.decode(result[1].loadout)
  147. end
  148.  
  149. if result[1].position ~= nil then
  150. userData.lastPosition = json.decode(result[1].position)
  151. end
  152.  
  153. cb2()
  154.  
  155. end
  156. )
  157.  
  158. end)
  159.  
  160. -- Run Tasks
  161. Async.parallel(tasks, function(results)
  162.  
  163. local xPlayer = CreateExtendedPlayer(player, userData.accounts, userData.inventory, userData.job, userData.job2, userData.loadout, userData.playerName, userData.lastPosition)
  164.  
  165. xPlayer.getMissingAccounts(function(missingAccounts)
  166.  
  167. if #missingAccounts > 0 then
  168.  
  169. for i=1, #missingAccounts, 1 do
  170. table.insert(xPlayer.accounts, {
  171. name = missingAccounts[i],
  172. money = 0,
  173. label = Config.AccountLabels[missingAccounts[i]]
  174. })
  175. end
  176.  
  177. xPlayer.createAccounts(missingAccounts)
  178. end
  179.  
  180. ESX.Players[_source] = xPlayer
  181.  
  182. TriggerEvent('esx:playerLoaded', _source)
  183.  
  184. TriggerClientEvent('esx:playerLoaded', _source, {
  185. identifier = xPlayer.identifier,
  186. accounts = xPlayer.getAccounts(),
  187. inventory = xPlayer.getInventory(),
  188. job = xPlayer.getJob(),
  189. job2 = xPlayer.getJob2(),
  190. loadout = xPlayer.getLoadout(),
  191. lastPosition = xPlayer.getLastPosition(),
  192. money = xPlayer.get('money')
  193. })
  194.  
  195. xPlayer.player.displayMoney(xPlayer.get('money'))
  196.  
  197. end)
  198.  
  199. end)
  200.  
  201. end)
  202.  
  203. end)
  204.  
  205. table.insert(tasks2, function(cb2)
  206.  
  207. MySQL.Async.fetchAll(
  208. 'SELECT * FROM `jobs` WHERE `name` = @name',
  209. {
  210. ['@name'] = userData.job.name
  211. },
  212. function(result)
  213.  
  214. userData.job['label'] = result[1].label
  215.  
  216. cb2()
  217.  
  218. end
  219. )
  220.  
  221. end)
  222. -- Get job2 label
  223. table.insert(tasks2, function(cb2)
  224.  
  225. MySQL.Async.fetchAll(
  226. 'SELECT * FROM `jobs` WHERE `name` = @name',
  227. {
  228. ['@name'] = userData.job2.name
  229. },
  230. function(result)
  231.  
  232. userData.job2['label'] = result[1].label
  233.  
  234. cb2()
  235.  
  236. end
  237. )
  238.  
  239. end)
  240.  
  241. -- Get job grade data
  242. table.insert(tasks2, function(cb2)
  243.  
  244. MySQL.Async.fetchAll(
  245. 'SELECT * FROM `job_grades` WHERE `job_name` = @job_name AND `grade` = @grade',
  246. {
  247. ['@job_name'] = userData.job.name,
  248. ['@grade'] = userData.job.grade
  249. },
  250. function(result)
  251.  
  252. userData.job['grade_name'] = result[1].name
  253. userData.job['grade_label'] = result[1].label
  254. userData.job['grade_salary'] = result[1].salary
  255.  
  256. userData.job['skin_male'] = {}
  257. userData.job['skin_female'] = {}
  258.  
  259. if result[1].skin_male ~= nil then
  260. userData.job['skin_male'] = json.decode(result[1].skin_male)
  261. end
  262.  
  263. if result[1].skin_female ~= nil then
  264. userData.job['skin_female'] = json.decode(result[1].skin_female)
  265. end
  266.  
  267. cb2()
  268.  
  269. end
  270. )
  271.  
  272. end)
  273.  
  274. table.insert(tasks2, function(cb2)
  275.  
  276. MySQL.Async.fetchAll(
  277. 'SELECT * FROM `job_grades` WHERE `job_name` = @job_name AND `grade` = @grade',
  278. {
  279. ['@job_name'] = userData.job2.name,
  280. ['@grade'] = userData.job2.grade
  281. },
  282. function(result)
  283.  
  284. userData.job2['grade_name'] = result[1].name
  285. userData.job2['grade_label'] = result[1].label
  286. userData.job2['grade_salary'] = result[1].salary
  287.  
  288. userData.job2['skin_male'] = {}
  289. userData.job2['skin_female'] = {}
  290.  
  291. if result[1].skin_male ~= nil then
  292. userData.job2['skin_male'] = json.decode(result[1].skin_male)
  293. end
  294.  
  295. if result[1].skin_female ~= nil then
  296. userData.job2['skin_female'] = json.decode(result[1].skin_female)
  297. end
  298.  
  299. cb2()
  300.  
  301. end
  302. )
  303.  
  304. end)
  305.  
  306. Async.series(tasks2, cb)
  307.  
  308. end)
  309.  
  310. AddEventHandler('playerDropped', function(reason)
  311. local _source = source
  312. local xPlayer = ESX.GetPlayerFromId(_source)
  313.  
  314. if xPlayer then
  315. TriggerEvent('esx:playerDropped', _source, reason)
  316.  
  317. ESX.SavePlayer(xPlayer, function()
  318. ESX.Players[_source] = nil
  319. ESX.LastPlayerData[_source] = nil
  320. end)
  321. end
  322. end)
  323.  
  324. RegisterServerEvent('esx:updateLoadout')
  325. AddEventHandler('esx:updateLoadout', function(loadout)
  326. local xPlayer = ESX.GetPlayerFromId(source)
  327. xPlayer.loadout = loadout
  328. end)
  329.  
  330. RegisterServerEvent('esx:updateLastPosition')
  331. AddEventHandler('esx:updateLastPosition', function(position)
  332. local xPlayer = ESX.GetPlayerFromId(source)
  333. xPlayer.setLastPosition(position)
  334. end)
  335.  
  336. RegisterServerEvent('esx:giveInventoryItem')
  337. AddEventHandler('esx:giveInventoryItem', function(target, type, itemName, itemCount)
  338. local _source = source
  339.  
  340. local sourceXPlayer = ESX.GetPlayerFromId(_source)
  341. local targetXPlayer = ESX.GetPlayerFromId(target)
  342.  
  343. if type == 'item_standard' then
  344.  
  345. local sourceItem = sourceXPlayer.getInventoryItem(itemName)
  346. local targetItem = targetXPlayer.getInventoryItem(itemName)
  347.  
  348. if itemCount > 0 and sourceItem.count >= itemCount then
  349.  
  350. if targetItem.limit ~= -1 and (targetItem.count + itemCount) > targetItem.limit then
  351. TriggerClientEvent('esx:showNotification', _source, _U('ex_inv_lim', targetXPlayer.name))
  352. else
  353. sourceXPlayer.removeInventoryItem(itemName, itemCount)
  354. targetXPlayer.addInventoryItem (itemName, itemCount)
  355.  
  356. TriggerClientEvent('esx:showNotification', _source, _U('gave_item', itemCount, ESX.Items[itemName].label, targetXPlayer.name))
  357. TriggerClientEvent('esx:showNotification', target, _U('received_item', itemCount, ESX.Items[itemName].label, sourceXPlayer.name))
  358.  
  359. TriggerEvent("esx:giveitemalert",sourceXPlayer.name,targetXPlayer.name,ESX.Items[itemName].label,itemCount)
  360. end
  361.  
  362.  
  363. else
  364. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_quantity'))
  365. end
  366.  
  367. elseif type == 'item_money' then
  368.  
  369. if itemCount > 0 and sourceXPlayer.getMoney() >= itemCount then
  370. sourceXPlayer.removeMoney(itemCount)
  371. targetXPlayer.addMoney (itemCount)
  372.  
  373. TriggerClientEvent('esx:showNotification', _source, _U('gave_money', ESX.Math.GroupDigits(itemCount), targetXPlayer.name))
  374. TriggerClientEvent('esx:showNotification', target, _U('received_money', ESX.Math.GroupDigits(itemCount), sourceXPlayer.name))
  375.  
  376. TriggerEvent("esx:givemoneyalert",sourceXPlayer.name,targetXPlayer.name,itemCount)
  377. else
  378. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
  379. end
  380.  
  381. elseif type == 'item_account' then
  382.  
  383. if itemCount > 0 and sourceXPlayer.getAccount(itemName).money >= itemCount then
  384. sourceXPlayer.removeAccountMoney(itemName, itemCount)
  385. targetXPlayer.addAccountMoney (itemName, itemCount)
  386.  
  387. TriggerClientEvent('esx:showNotification', _source, _U('gave_account_money', ESX.Math.GroupDigits(itemCount), Config.AccountLabels[itemName], targetXPlayer.name))
  388. TriggerClientEvent('esx:showNotification', target, _U('received_account_money', ESX.Math.GroupDigits(itemCount), Config.AccountLabels[itemName], sourceXPlayer.name))
  389.  
  390. TriggerEvent("esx:givemoneybankalert",sourceXPlayer.name,targetXPlayer.name,itemCount)
  391. else
  392. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
  393. end
  394.  
  395. elseif type == 'item_weapon' then
  396.  
  397. if not targetXPlayer.hasWeapon(itemName) then
  398. sourceXPlayer.removeWeapon(itemName)
  399. targetXPlayer.addWeapon(itemName, itemCount)
  400.  
  401. local weaponLabel = ESX.GetWeaponLabel(itemName)
  402.  
  403. if itemCount > 0 then
  404. TriggerClientEvent('esx:showNotification', _source, _U('gave_weapon_ammo', weaponLabel, itemCount, targetXPlayer.name))
  405. TriggerClientEvent('esx:showNotification', target, _U('received_weapon_ammo', weaponLabel, itemCount, sourceXPlayer.name))
  406. else
  407. TriggerClientEvent('esx:showNotification', _source, _U('gave_weapon', weaponLabel, targetXPlayer.name))
  408. TriggerClientEvent('esx:showNotification', target, _U('received_weapon', weaponLabel, sourceXPlayer.name))
  409.  
  410. TriggerEvent("esx:giveweaponalert",sourceXPlayer.name,targetXPlayer.name,weaponLabel)
  411. end
  412. else
  413. TriggerClientEvent('esx:showNotification', _source, _U('gave_weapon_hasalready', targetXPlayer.name, weaponLabel))
  414. TriggerClientEvent('esx:showNotification', _source, _U('received_weapon_hasalready', sourceXPlayer.name, weaponLabel))
  415. end
  416.  
  417. end
  418. end)
  419.  
  420. RegisterServerEvent('esx:removeInventoryItem')
  421. AddEventHandler('esx:removeInventoryItem', function(type, itemName, itemCount)
  422. local _source = source
  423.  
  424. if type == 'item_standard' then
  425.  
  426. if itemCount == nil or itemCount < 1 then
  427. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_quantity'))
  428. else
  429. local xPlayer = ESX.GetPlayerFromId(source)
  430. local xItem = xPlayer.getInventoryItem(itemName)
  431.  
  432. if (itemCount > xItem.count or xItem.count < 1) then
  433. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_quantity'))
  434. else
  435. xPlayer.removeInventoryItem(itemName, itemCount)
  436.  
  437. local pickupLabel = ('~y~%s~s~ [~b~%s~s~]'):format(xItem.label, itemCount)
  438. ESX.CreatePickup('item_standard', itemName, itemCount, pickupLabel, _source)
  439. TriggerClientEvent('esx:showNotification', _source, _U('threw_standard', itemCount, xItem.label))
  440. end
  441. end
  442.  
  443. elseif type == 'item_money' then
  444.  
  445. if itemCount == nil or itemCount < 1 then
  446. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
  447. else
  448. local xPlayer = ESX.GetPlayerFromId(source)
  449. local playerCash = xPlayer.getMoney()
  450.  
  451. if (itemCount > playerCash or playerCash < 1) then
  452. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
  453. else
  454. xPlayer.removeMoney(itemCount)
  455.  
  456. local pickupLabel = ('~y~%s~s~ [~g~%s~s~]'):format(_U('cash'), _U('locale_currency', ESX.Math.GroupDigits(itemCount)))
  457. ESX.CreatePickup('item_money', 'money', itemCount, pickupLabel, _source)
  458. TriggerClientEvent('esx:showNotification', _source, _U('threw_money', ESX.Math.GroupDigits(itemCount)))
  459. end
  460. end
  461.  
  462. elseif type == 'item_account' then
  463.  
  464. if itemCount == nil or itemCount < 1 then
  465. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
  466. else
  467. local xPlayer = ESX.GetPlayerFromId(source)
  468. local account = xPlayer.getAccount(itemName)
  469.  
  470. if (itemCount > account.money or account.money < 1) then
  471. TriggerClientEvent('esx:showNotification', _source, _U('imp_invalid_amount'))
  472. else
  473. xPlayer.removeAccountMoney(itemName, itemCount)
  474.  
  475. local pickupLabel = ('~y~%s~s~ [~g~%s~s~]'):format(account.label, _U('locale_currency', ESX.Math.GroupDigits(itemCount)))
  476. ESX.CreatePickup('item_account', itemName, itemCount, pickupLabel, _source)
  477. TriggerClientEvent('esx:showNotification', _source, _U('threw_account', ESX.Math.GroupDigits(itemCount), string.lower(account.label)))
  478. end
  479. end
  480.  
  481. elseif type == 'item_weapon' then
  482.  
  483. local xPlayer = ESX.GetPlayerFromId(source)
  484. local loadout = xPlayer.getLoadout()
  485.  
  486. for i=1, #loadout, 1 do
  487. if loadout[i].name == itemName then
  488. itemCount = loadout[i].ammo
  489. break
  490. end
  491. end
  492.  
  493. if xPlayer.hasWeapon(itemName) then
  494. local weaponLabel, weaponPickup = ESX.GetWeaponLabel(itemName), 'PICKUP_' .. string.upper(itemName)
  495.  
  496. xPlayer.removeWeapon(itemName)
  497.  
  498. if itemCount > 0 then
  499. TriggerClientEvent('esx:pickupWeapon', _source, weaponPickup, itemName, itemCount)
  500. TriggerClientEvent('esx:showNotification', _source, _U('threw_weapon_ammo', weaponLabel, itemCount))
  501. else
  502. -- workaround for CreateAmbientPickup() giving 30 rounds of ammo when you drop the weapon with 0 ammo
  503. TriggerClientEvent('esx:pickupWeapon', _source, weaponPickup, itemName, 1)
  504. TriggerClientEvent('esx:showNotification', _source, _U('threw_weapon', weaponLabel))
  505. end
  506. end
  507.  
  508. end
  509. end)
  510.  
  511. RegisterServerEvent('esx:useItem')
  512. AddEventHandler('esx:useItem', function(itemName)
  513. local xPlayer = ESX.GetPlayerFromId(source)
  514. local count = xPlayer.getInventoryItem(itemName).count
  515.  
  516. if count > 0 then
  517. ESX.UseItem(source, itemName)
  518. else
  519. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('act_imp'))
  520. end
  521. end)
  522.  
  523. RegisterServerEvent('esx:onPickup')
  524. AddEventHandler('esx:onPickup', function(id)
  525. local _source = source
  526. local pickup = ESX.Pickups[id]
  527. local xPlayer = ESX.GetPlayerFromId(_source)
  528.  
  529. if pickup.type == 'item_standard' then
  530.  
  531. local item = xPlayer.getInventoryItem(pickup.name)
  532. local canTake = ((item.limit == -1) and (pickup.count)) or ((item.limit - item.count > 0) and (item.limit - item.count)) or 0
  533. local total = pickup.count < canTake and pickup.count or canTake
  534. local remaining = pickup.count - total
  535.  
  536. TriggerClientEvent('esx:removePickup', -1, id)
  537.  
  538. if total > 0 then
  539. xPlayer.addInventoryItem(pickup.name, total)
  540. end
  541.  
  542. if remaining > 0 then
  543. TriggerClientEvent('esx:showNotification', _source, _U('cannot_pickup_room', item.label))
  544.  
  545. local pickupLabel = ('~y~%s~s~ [~b~%s~s~]'):format(item.label, remaining)
  546. ESX.CreatePickup('item_standard', pickup.name, remaining, pickupLabel, _source)
  547. end
  548.  
  549. elseif pickup.type == 'item_money' then
  550. TriggerClientEvent('esx:removePickup', -1, id)
  551. xPlayer.addMoney(pickup.count)
  552. elseif pickup.type == 'item_account' then
  553. TriggerClientEvent('esx:removePickup', -1, id)
  554. xPlayer.addAccountMoney(pickup.name, pickup.count)
  555. end
  556. end)
  557.  
  558. ESX.RegisterServerCallback('esx:getPlayerData', function(source, cb)
  559.  
  560. local xPlayer = ESX.GetPlayerFromId(source)
  561.  
  562. cb({
  563. identifier = xPlayer.identifier,
  564. accounts = xPlayer.getAccounts(),
  565. inventory = xPlayer.getInventory(),
  566. job = xPlayer.getJob(),
  567. job2 =xPlayer.getJob2(),
  568. loadout = xPlayer.getLoadout(),
  569. lastPosition = xPlayer.getLastPosition(),
  570. money = xPlayer.get('money')
  571. })
  572.  
  573. end)
  574.  
  575. ESX.RegisterServerCallback('esx:getOtherPlayerData', function(source, cb, target)
  576.  
  577. local xPlayer = ESX.GetPlayerFromId(target)
  578.  
  579. cb({
  580. identifier = xPlayer.identifier,
  581. accounts = xPlayer.getAccounts(),
  582. inventory = xPlayer.getInventory(),
  583. job = xPlayer.getJob(),
  584. job2 = xPlayer.getJob2(),
  585. loadout = xPlayer.getLoadout(),
  586. lastPosition = xPlayer.getLastPosition(),
  587. money = xPlayer.get('money')
  588. })
  589.  
  590. end)
  591.  
  592. TriggerEvent("es:addGroup", "jobmaster", "user", function(group) end)
  593.  
  594. ESX.StartDBSync()
  595. ESX.StartPayCheck()
  596.  
  597. ESX.GetItemLabel = function(name)
  598. if ESX.Items[name] ~= nil then
  599. return ESX.Items[name].label
  600. end
  601. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement