Advertisement
BritishBrohood

one of 15

Jan 10th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.10 KB | None | 0 0
  1. local openInventory = {}
  2. loadedInventories = {}
  3.  
  4. RegisterServerEvent('disc-inventoryhud:openInventory')
  5. AddEventHandler('disc-inventoryhud:openInventory', function(inventory)
  6.     if inventory.type == 'shop' then
  7.         return
  8.     end
  9.     if openInventory[inventory.owner] == nil then
  10.         openInventory[inventory.owner] = {}
  11.     end
  12.     openInventory[inventory.owner][source] = true
  13. end)
  14.  
  15. RegisterServerEvent('disc-inventoryhud:closeInventory')
  16. AddEventHandler('disc-inventoryhud:closeInventory', function(inventory)
  17.     if inventory.type == 'shop' then
  18.         return
  19.     end
  20.     if openInventory[inventory.owner] == nil then
  21.         openInventory[inventory.owner] = {}
  22.     end
  23.     if openInventory[inventory.owner][source] then
  24.         openInventory[inventory.owner][source] = nil
  25.     end
  26. end)
  27.  
  28. function closeAllOpenInventoriesForSource(source)
  29.     for k, inv in pairs(openInventory) do
  30.         openInventory[k][source] = nil
  31.     end
  32. end
  33.  
  34. RegisterServerEvent('disc-inventoryhud:refreshInventory')
  35. AddEventHandler('disc-inventoryhud:refreshInventory', function(owner)
  36.     if openInventory[owner] == nil then
  37.         openInventory[owner] = {}
  38.     end
  39.  
  40.     for k, v in pairs(openInventory[owner]) do
  41.         TriggerClientEvent('disc-inventoryhud:refreshInventory', k)
  42.     end
  43. end)
  44.  
  45. function dumpInventory(inventory)
  46.     for k, v in pairs(inventory) do
  47.         print(k .. ' ' .. v.name)
  48.     end
  49. end
  50.  
  51. RegisterServerEvent("disc-inventoryhud:MoveToEmpty")
  52. AddEventHandler("disc-inventoryhud:MoveToEmpty", function(data)
  53.     local source = source
  54.     handleWeaponRemoval(data, source)
  55.     if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  56.         local originInvHandler = InvType[data.originTier.name]
  57.         originInvHandler.applyToInventory(data.originOwner, function(inventory)
  58.             inventory[tostring(data.destinationSlot)] = inventory[tostring(data.originSlot)]
  59.             inventory[tostring(data.originSlot)] = nil
  60.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  61.         end)
  62.     else
  63.         local originInvHandler = InvType[data.originTier.name]
  64.         local destinationInvHandler = InvType[data.destinationTier.name]
  65.         if data.originTier.name == 'shop' then
  66.             local player = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  67.             if player.getMoney() >= data.originItem.price * data.originItem.qty then
  68.                 player.removeMoney(data.originItem.price * data.originItem.qty)
  69.             else
  70.                 TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  71.                 TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  72.                 return
  73.             end
  74.         end
  75.  
  76.         if data.destinationTier.name == 'shop' then
  77.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  78.             TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  79.             print('Attempt to sell')
  80.             return
  81.         end
  82.  
  83.         originInvHandler.applyToInventory(data.originOwner, function(originInventory)
  84.             destinationInvHandler.applyToInventory(data.destinationOwner, function(destinationInventory)
  85.  
  86.                 destinationInventory[tostring(data.destinationSlot)] = originInventory[tostring(data.originSlot)]
  87.                 originInventory[tostring(data.originSlot)] = nil
  88.                 destinationInvHandler.saveInventory(data.destinationOwner, destinationInventory)
  89.                 originInvHandler.saveInventory(data.originOwner, originInventory)
  90.  
  91.                 if data.originTier.name == 'player' then
  92.                     data.originItem.block = true
  93.                     local ownerPlayer = ESX.GetPlayerFromIdentifier(data.originOwner)
  94.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.originItem.qty, ownerPlayer.source)
  95.                     ownerPlayer.removeInventoryItem(data.originItem.id, data.originItem.qty)
  96.                 end
  97.  
  98.                 if data.destinationTier.name == 'player' then
  99.                     data.originItem.block = true
  100.                     local destinationPlayer = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  101.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.originItem.qty, destinationPlayer.source)
  102.                     destinationPlayer.addInventoryItem(data.originItem.id, data.originItem.qty)
  103.                 end
  104.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  105.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  106.             end)
  107.         end)
  108.     end
  109. end)
  110.  
  111. RegisterServerEvent("disc-inventoryhud:SwapItems")
  112. AddEventHandler("disc-inventoryhud:SwapItems", function(data)
  113.     local source = source
  114.  
  115.     handleWeaponRemoval(data, source)
  116.     if data.originTier.name == 'shop' then
  117.         print('Attempt to Swap in Store')
  118.         TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  119.         TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  120.         return
  121.     end
  122.  
  123.     if data.destinationTier.name == 'shop' then
  124.         print('Attempt to Swap in Store')
  125.         TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  126.         TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  127.         return
  128.     end
  129.  
  130.     if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  131.         local originInvHandler = InvType[data.originTier.name]
  132.         originInvHandler.applyToInventory(data.originOwner, function(inventory)
  133.             local tempItem = inventory[tostring(data.originSlot)]
  134.             inventory[tostring(data.originSlot)] = inventory[tostring(data.destinationSlot)]
  135.             inventory[tostring(data.destinationSlot)] = tempItem
  136.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  137.         end)
  138.     else
  139.         local originInvHandler = InvType[data.originTier.name]
  140.         local destinationInvHandler = InvType[data.destinationTier.name]
  141.         originInvHandler.applyToInventory(data.originOwner, function(originInventory)
  142.             destinationInvHandler.applyToInventory(data.destinationOwner, function(destinationInventory)
  143.                 local tempItem = originInventory[tostring(data.originSlot)]
  144.                 originInventory[tostring(data.originSlot)] = destinationInventory[tostring(data.destinationSlot)]
  145.                 destinationInventory[tostring(data.destinationSlot)] = tempItem
  146.  
  147.                 if data.originTier.name == 'player' then
  148.                     data.originItem.block = true
  149.                     data.destinationItem.block = true
  150.                     local originPlayer = ESX.GetPlayerFromIdentifier(data.originOwner)
  151.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.originItem.qty, originPlayer.source)
  152.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.destinationItem, data.destinationItem.qty, originPlayer.source)
  153.                     originPlayer.addInventoryItem(data.originItem.id, data.originItem.qty)
  154.                     originPlayer.removeInventoryItem(data.destinationItem.id, data.destinationItem.qty)
  155.                 end
  156.  
  157.                 if data.destinationTier.name == 'player' then
  158.                     data.originItem.block = true
  159.                     data.destinationItem.block = true
  160.                     local destinationPlayer = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  161.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.originItem.qty, destinationPlayer.source)
  162.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.destinationItem, data.destinationItem.qty, destinationPlayer.source)
  163.                     destinationPlayer.removeInventoryItem(data.originItem.id, data.originItem.qty)
  164.                     destinationPlayer.addInventoryItem(data.destinationItem.id, data.destinationItem.qty)
  165.                 end
  166.  
  167.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  168.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  169.             end)
  170.         end)
  171.     end
  172. end)
  173.  
  174. RegisterServerEvent("disc-inventoryhud:CombineStack")
  175. AddEventHandler("disc-inventoryhud:CombineStack", function(data)
  176.     local source = source
  177.  
  178.     handleWeaponRemoval(data, source)
  179.     if data.originTier.name == 'shop' then
  180.         local player = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  181.         if player.getMoney() >= data.originItem.price * data.originQty then
  182.             player.removeMoney(data.originItem.price * data.originQty)
  183.         else
  184.             TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  185.             TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  186.             return
  187.         end
  188.     end
  189.  
  190.     if data.destinationTier.name == 'shop' then
  191.         TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  192.         TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  193.         print('Attempt to sell')
  194.         return
  195.     end
  196.  
  197.     if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  198.         local originInvHandler = InvType[data.originTier.name]
  199.         originInvHandler.applyToInventory(data.originOwner, function(inventory)
  200.             inventory[tostring(data.originSlot)] = nil
  201.             inventory[tostring(data.destinationSlot)].count = data.destinationQty
  202.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  203.         end)
  204.     else
  205.         local originInvHandler = InvType[data.originTier.name]
  206.         local destinationInvHandler = InvType[data.destinationTier.name]
  207.         originInvHandler.applyToInventory(data.originOwner, function(originInventory)
  208.             destinationInvHandler.applyToInventory(data.destinationOwner, function(destinationInventory)
  209.                 originInventory[tostring(data.originSlot)] = nil
  210.                 destinationInventory[tostring(data.destinationSlot)].count = data.destinationQty
  211.  
  212.                 if data.originTier.name == 'player' then
  213.                     data.originItem.block = true
  214.                     local originPlayer = ESX.GetPlayerFromIdentifier(data.originOwner)
  215.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.originItem.qty, originPlayer.source)
  216.                     originPlayer.removeInventoryItem(data.originItem.id, data.originItem.qty)
  217.                 end
  218.  
  219.                 if data.destinationTier.name == 'player' then
  220.                     data.originItem.block = true
  221.                     local destinationPlayer = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  222.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.originItem.qty, destinationPlayer.source)
  223.                     destinationPlayer.addInventoryItem(data.originItem.id, data.originItem.qty)
  224.                 end
  225.  
  226.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  227.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  228.             end)
  229.         end)
  230.     end
  231. end)
  232.  
  233. RegisterServerEvent("disc-inventoryhud:TopoffStack")
  234. AddEventHandler("disc-inventoryhud:TopoffStack", function(data)
  235.     local source = source
  236.  
  237.     handleWeaponRemoval(data, source)
  238.     if data.originTier.name == 'shop' then
  239.         local player = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  240.         if player.getMoney() >= data.originItem.price * data.originQty then
  241.             player.removeMoney(data.originItem.price * data.originQty)
  242.         else
  243.             TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  244.             TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  245.             return
  246.         end
  247.     end
  248.  
  249.     if data.destinationTier.name == 'shop' then
  250.         TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  251.         TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  252.         print('Attempt to sell')
  253.         return
  254.     end
  255.  
  256.     if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  257.         local originInvHandler = InvType[data.originTier.name]
  258.         originInvHandler.applyToInventory(data.originOwner, function(inventory)
  259.             inventory[tostring(data.originSlot)].count = data.originItem.qty
  260.             inventory[tostring(data.destinationSlot)].count = data.destinationItem.qty
  261.             print('Refreshing')
  262.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  263.         end)
  264.     else
  265.         local originInvHandler = InvType[data.originTier.name]
  266.         local destinationInvHandler = InvType[data.destinationTier.name]
  267.         originInvHandler.applyToInventory(data.originOwner, function(originInventory)
  268.             destinationInvHandler.applyToInventory(data.destinationOwner, function(destinationInventory)
  269.                 originInventory[tostring(data.originSlot)].count = data.originItem.qty
  270.                 destinationInventory[tostring(data.destinationSlot)].count = data.destinationItem.qty
  271.  
  272.                 if data.originTier.name == 'player' then
  273.                     data.originItem.block = true
  274.                     local originPlayer = ESX.GetPlayerFromIdentifier(data.originOwner)
  275.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.originItem.qty, originPlayer.source)
  276.                     originPlayer.removeInventoryItem(data.originItem.id, data.originItem.qty)
  277.                 end
  278.  
  279.                 if data.destinationTier.name == 'player' then
  280.                     data.originItem.block = true
  281.                     local destinationPlayer = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  282.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.originItem.qty, destinationPlayer.source)
  283.                     destinationPlayer.addInventoryItem(data.originItem.id, data.originItem.qty)
  284.                 end
  285.  
  286.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  287.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  288.             end)
  289.         end)
  290.     end
  291. end)
  292.  
  293. RegisterServerEvent("disc-inventoryhud:EmptySplitStack")
  294. AddEventHandler("disc-inventoryhud:EmptySplitStack", function(data)
  295.  
  296.     handleWeaponRemoval(data, source)
  297.     if data.originTier.name == 'shop' then
  298.         local player = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  299.         if player.getMoney() >= data.originItem.price * data.moveQty then
  300.             player.removeMoney(data.originItem.price * data.moveQty)
  301.         else
  302.             TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  303.             TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  304.             return
  305.         end
  306.     end
  307.  
  308.     if data.destinationTier.name == 'shop' then
  309.         TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  310.         TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  311.         print('Attempt to sell')
  312.         return
  313.     end
  314.  
  315.     local source = source
  316.     if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  317.         local originInvHandler = InvType[data.originTier.name]
  318.         originInvHandler.applyToInventory(data.originOwner, function(inventory)
  319.             inventory[tostring(data.originSlot)].count = inventory[tostring(data.originSlot)].count - data.moveQty
  320.             local item = inventory[tostring(data.originSlot)]
  321.             inventory[tostring(data.destinationSlot)] = {
  322.                 name = item.name,
  323.                 count = data.moveQty
  324.             }
  325.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  326.         end)
  327.     else
  328.         local originInvHandler = InvType[data.originTier.name]
  329.         local destinationInvHandler = InvType[data.destinationTier.name]
  330.         originInvHandler.applyToInventory(data.originOwner, function(originInventory)
  331.             destinationInvHandler.applyToInventory(data.destinationOwner, function(destinationInventory)
  332.                 originInventory[tostring(data.originSlot)].count = originInventory[tostring(data.originSlot)].count - data.moveQty
  333.                 local item = originInventory[tostring(data.originSlot)]
  334.                 destinationInventory[tostring(data.destinationSlot)] = {
  335.                     name = item.name,
  336.                     count = data.moveQty
  337.                 }
  338.  
  339.                 if data.originTier.name == 'player' then
  340.                     local originPlayer = ESX.GetPlayerFromIdentifier(data.originOwner)
  341.                     data.originItem.block = true
  342.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.moveQty, originPlayer.source)
  343.                     originPlayer.removeInventoryItem(data.originItem.id, data.moveQty)
  344.                 end
  345.  
  346.                 if data.destinationTier.name == 'player' then
  347.                     local destinationPlayer = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  348.                     data.originItem.block = true
  349.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.moveQty, destinationPlayer.source)
  350.                     destinationPlayer.addInventoryItem(data.originItem.id, data.moveQty)
  351.                 end
  352.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  353.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  354.             end)
  355.         end)
  356.     end
  357. end)
  358.  
  359. RegisterServerEvent("disc-inventoryhud:SplitStack")
  360. AddEventHandler("disc-inventoryhud:SplitStack", function(data)
  361.     local source = source
  362.     handleWeaponRemoval(data, source)
  363.  
  364.     if data.originTier.name == 'shop' then
  365.         local player = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  366.         if player.getMoney() >= data.originItem.price * data.moveQty then
  367.             player.removeMoney(data.originItem.price * data.moveQty)
  368.         else
  369.             TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  370.             TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  371.             return
  372.         end
  373.     end
  374.  
  375.     if data.destinationTier.name == 'shop' then
  376.         TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  377.         TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  378.         print('Attempt to sell')
  379.         return
  380.     end
  381.  
  382.     if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  383.         local originInvHandler = InvType[data.originTier.name]
  384.         originInvHandler.applyToInventory(data.originOwner, function(inventory)
  385.             inventory[tostring(data.originSlot)].count = inventory[tostring(data.originSlot)].count - data.moveQty
  386.             inventory[tostring(data.destinationSlot)].count = inventory[tostring(data.destinationSlot)].count + data.moveQty
  387.             TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  388.         end)
  389.     else
  390.         local originInvHandler = InvType[data.originTier.name]
  391.         local destinationInvHandler = InvType[data.destinationTier.name]
  392.         originInvHandler.applyToInventory(data.originOwner, function(originInventory)
  393.             destinationInvHandler.applyToInventory(data.destinationOwner, function(destinationInventory)
  394.                 originInventory[tostring(data.originSlot)].count = originInventory[tostring(data.originSlot)].count - data.moveQty
  395.                 destinationInventory[tostring(data.destinationSlot)].count = destinationInventory[tostring(data.destinationSlot)].count + data.moveQty
  396.  
  397.                 if data.originTier.name == 'player' then
  398.                     data.originItem.block = true
  399.                     local originPlayer = ESX.GetPlayerFromIdentifier(data.originOwner)
  400.                     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.moveQty, originPlayer.source)
  401.                     originPlayer.removeInventoryItem(data.originItem.id, data.moveQty)
  402.                 end
  403.  
  404.                 if data.destinationTier.name == 'player' then
  405.                     data.originItem.block = true
  406.                     local destinationPlayer = ESX.GetPlayerFromIdentifier(data.destinationOwner)
  407.                     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.moveQty, destinationPlayer.source)
  408.                     destinationPlayer.addInventoryItem(data.originItem.id, data.moveQty)
  409.                 end
  410.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.originOwner)
  411.                 TriggerEvent('disc-inventoryhud:refreshInventory', data.destinationOwner)
  412.             end)
  413.         end)
  414.     end
  415. end)
  416.  
  417. RegisterServerEvent("disc-inventoryhud:GiveItem")
  418. AddEventHandler("disc-inventoryhud:GiveItem", function(data)
  419.     handleGiveWeaponRemoval(data, source)
  420.     TriggerEvent('disc-inventoryhud:notifyImpendingRemoval', data.originItem, data.count, source)
  421.     TriggerEvent('disc-inventoryhud:notifyImpendingAddition', data.originItem, data.count, data.target)
  422.     local targetPlayer = ESX.GetPlayerFromId(data.target)
  423.     targetPlayer.addInventoryItem(data.originItem.id, data.count)
  424.     local sourcePlayer = ESX.GetPlayerFromId(source)
  425.     sourcePlayer.removeInventoryItem(data.originItem.id, data.count)
  426.     TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  427.     TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  428. end)
  429.  
  430. RegisterServerEvent("disc-inventoryhud:GiveCash")
  431. AddEventHandler("disc-inventoryhud:GiveCash", function(data)
  432.     local sourcePlayer = ESX.GetPlayerFromId(source)
  433.     if data.item == 'cash' then
  434.         if sourcePlayer.getMoney() >= data.count then
  435.             sourcePlayer.removeMoney(data.count)
  436.             local targetPlayer = ESX.GetPlayerFromId(data.target)
  437.             targetPlayer.addMoney(data.count)
  438.             TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  439.             TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  440.         end
  441.  
  442.     elseif data.item == 'black_money' then
  443.         if sourcePlayer.getAccount('black_money').money >= data.count then
  444.             sourcePlayer.removeAccountMoney('black_money', data.count)
  445.             local targetPlayer = ESX.GetPlayerFromId(data.target)
  446.             targetPlayer.addAccountMoney('black_money', data.count)
  447.             TriggerClientEvent('disc-inventoryhud:refreshInventory', source)
  448.             TriggerClientEvent('disc-inventoryhud:refreshInventory', data.target)
  449.         end
  450.     end
  451. end)
  452.  
  453. function debugData(data)
  454.     for k, v in pairs(data) do
  455.         print(k .. ' ' .. v)
  456.     end
  457. end
  458.  
  459. function removeItemFromSlot(inventory, slot, count)
  460.     if inventory[tostring(slot)] ~= nill and inventory[tostring(slot)].count - count > 0 then
  461.         inventory[tostring(slot)].count = inventory[tostring(slot)].count - count
  462.         return
  463.     else
  464.         inventory[tostring(slot)] = nil
  465.         return
  466.     end
  467. end
  468.  
  469. function removeItemFromInventory(item, count, inventory)
  470.     for k, v in pairs(inventory) do
  471.         if v.name == item.name then
  472.             if v.count - count < 0 then
  473.                 local tempCount = inventory[k].count
  474.                 inventory[k] = nil
  475.                 count = count - tempCount
  476.             elseif v.count - count > 0 then
  477.                 inventory[k].count = inventory[k].count - count
  478.                 return
  479.             elseif v.count - count == 0 then
  480.                 inventory[k] = nil
  481.                 return
  482.             else
  483.                 print('Missing Remove condition')
  484.             end
  485.         end
  486.     end
  487. end
  488.  
  489. function addToInventory(item, type, inventory, max)
  490.     if max == -1 then
  491.         max = 9999
  492.     end
  493.     local toAdd = item.count
  494.     while toAdd > 0 do
  495.         toAdd = AttemptMerge(item, inventory, toAdd, max)
  496.         if toAdd > 0 then
  497.             toAdd = AddToEmpty(item, type, inventory, toAdd, max)
  498.         else
  499.             toAdd = 0
  500.         end
  501.     end
  502. end
  503.  
  504. function AttemptMerge(item, inventory, count)
  505.     local max = getItemDataProperty(item.name, 'max') or 100
  506.     for k, v in pairs(inventory) do
  507.         if v.name == item.name then
  508.             if v.count + count > max then
  509.                 local tempCount = max - inventory[k].count
  510.                 inventory[tostring(k)].count = max
  511.                 count = count - tempCount
  512.             elseif v.count + count <= max then
  513.                 inventory[tostring(k)].count = v.count + count
  514.                 return 0
  515.             else
  516.                 print('Missing MERGE condition')
  517.             end
  518.         end
  519.     end
  520.     return count
  521. end
  522.  
  523.  
  524. function AddToEmpty(item, type, inventory, count)
  525.     local max = getItemDataProperty(item.name, 'max') or 100
  526.     for i = 1, InvType[type].slots, 1 do
  527.         if inventory[tostring(i)] == nil then
  528.             if count > max then
  529.                 inventory[tostring(i)] = item
  530.                 inventory[tostring(i)].count = max
  531.                 return count - max
  532.             else
  533.                 inventory[tostring(i)] = item
  534.                 return 0
  535.             end
  536.         end
  537.     end
  538.     print('Inventory Overflow!')
  539.     return 0
  540. end
  541.  
  542. function createDisplayItem(item, esxItem, slot, price, type)
  543.     local max = 100
  544.     return {
  545.         id = esxItem.name,
  546.         itemId = esxItem.name,
  547.         qty = item.count,
  548.         slot = slot,
  549.         label = esxItem.label,
  550.         type = type or 'item',
  551.         max = getItemDataProperty(esxItem.name, 'max') or max,
  552.         stackable = true,
  553.         unique = esxItem.rare,
  554.         usable = esxItem.usable,
  555.         giveable = true,
  556.         description = getItemDataProperty(esxItem.name, 'description'),
  557.         weight = getItemDataProperty(esxItem.name, 'weight'),
  558.         metadata = {},
  559.         staticMeta = {},
  560.         canRemove = esxItem.canRemove,
  561.         price = price or 0,
  562.         needs = false,
  563.         closeUi = getItemDataProperty(esxItem.name, 'closeonuse'),
  564.     }
  565. end
  566.  
  567.  
  568. function createItem(name, count)
  569.     return { name = name, count = count }
  570. end
  571.  
  572. ESX.RegisterServerCallback('disc-inventoryhud:canOpenInventory', function(source, cb, type, identifier)
  573.     cb(not (table.length(openInventory[identifier]) > 0) or openInventory[identifier][source])
  574. end)
  575.  
  576. ESX.RegisterServerCallback('disc-inventoryhud:getSecondaryInventory', function(source, cb, type, identifier)
  577.     InvType[type].getDisplayInventory(identifier, cb, source)
  578. end)
  579.  
  580. --[[ Citizen.CreateThread(function()
  581.     while true do
  582.         Citizen.Wait(30 * 60 * 1000)
  583.         saveInventories()
  584.     end
  585. end)  ]]
  586.  
  587. function saveInventories()
  588.     for type, inventories in pairs(loadedInventories) do
  589.         for identifier, inventory in pairs(inventories) do
  590.             if inventory ~= nil then
  591.                 if table.length(inventory) > 0 then
  592.                     saveLoadedInventory(identifier, type, inventory)
  593.                 else
  594.                     deleteInventory(identifier, type)
  595.                 end
  596.             end
  597.         end
  598.     end
  599.     RconPrint('[Disc-InventoryHud][SAVED] All Inventories' .. "\n")
  600. end
  601.  
  602. function saveInventory(identifier, type)
  603.     saveLoadedInventory(identifier, type, loadedInventories[type][identifier])
  604. end
  605.  
  606. function saveLoadedInventory(identifier, type, data)
  607.     if table.length(data) > 0 then
  608.         MySQL.Async.execute('UPDATE disc_inventory SET data = @data WHERE owner = @owner AND type = @type', {
  609.             ['@owner'] = identifier,
  610.             ['@type'] = type,
  611.             ['@data'] = json.encode(data)
  612.         }, function(result)
  613.             if result == 0 then
  614.                 createInventory(identifier, type, data)
  615.             end
  616.             loadedInventories[type][identifier] = nil
  617.             TriggerEvent('disc-inventoryhud:savedInventory', identifier, type, data)
  618.         end)
  619.     end
  620. end
  621.  
  622. function createInventory(identifier, type, data)
  623.     MySQL.Async.execute('INSERT INTO disc_inventory (owner, type, data) VALUES (@owner, @type, @data)', {
  624.         ['@owner'] = identifier,
  625.         ['@type'] = type,
  626.         ['@data'] = json.encode(data)
  627.     }, function()
  628.         TriggerEvent('disc-inventoryhud:createdInventory', identifier, type, data)
  629.     end)
  630. end
  631.  
  632. function deleteInventory(identifier, type)
  633.     MySQL.Async.execute('DELETE FROM disc_inventory WHERE owner = @owner AND type = @type', {
  634.         ['@owner'] = identifier,
  635.         ['@type'] = type
  636.     }, function()
  637.         TriggerEvent('disc-inventoryhud:deletedInventory', identifier, type)
  638.     end)
  639. end
  640.  
  641. function getDisplayInventory(identifier, type, cb, source)
  642.     local player = ESX.GetPlayerFromId(source)
  643.     InvType[type].getInventory(identifier, function(inventory)
  644.         local itemsObject = {}
  645.  
  646.         for k, v in pairs(inventory) do
  647.             local esxItem = player.getInventoryItem(v.name)
  648.             local item = createDisplayItem(v, esxItem, tonumber(k))
  649.             item.usable = false
  650.             item.giveable = false
  651.             item.canRemove = false
  652.             table.insert(itemsObject, item)
  653.         end
  654.  
  655.         local inv
  656.         if type == 'player' then
  657.             local targetPlayer = ESX.GetPlayerFromIdentifier(identifier)
  658.             inv = {
  659.                 invId = identifier,
  660.                 invTier = InvType[type],
  661.                 inventory = itemsObject,
  662.                 cash = targetPlayer.getMoney(),
  663.                 black_money = targetPlayer.getAccount('black_money').money
  664.             }
  665.         else
  666.             inv = {
  667.                 invId = identifier,
  668.                 invTier = InvType[type],
  669.                 inventory = itemsObject,
  670.                 cash = inventory['cash'] or 0,
  671.                 black_money = inventory['black_money'] or 0
  672.             }
  673.         end
  674.         cb(inv)
  675.     end)
  676. end
  677.  
  678. function getInventory(identifier, type, cb)
  679.     if loadedInventories[type][identifier] ~= nil then
  680.         cb(loadedInventories[type][identifier])
  681.     else
  682.         loadInventory(identifier, type, cb)
  683.     end
  684. end
  685.  
  686. function applyToInventory(identifier, type, f)
  687.     if loadedInventories[type][identifier] ~= nil then
  688.         f(loadedInventories[type][identifier])
  689.     else
  690.         loadInventory(identifier, type, function()
  691.             applyToInventory(identifier, type, f)
  692.         end)
  693.     end
  694.     if loadedInventories[type][identifier] and table.length(loadedInventories[type][identifier]) > 0 then
  695.         TriggerEvent('disc-inventoryhud:modifiedInventory', identifier, type, loadedInventories[type][identifier])
  696.     else
  697.         TriggerEvent('disc-inventoryhud:modifiedInventory', identifier, type, nil)
  698.     end
  699. end
  700.  
  701. function loadInventory(identifier, type, cb)
  702.     MySQL.Async.fetchAll('SELECT data FROM disc_inventory WHERE owner = @owner and type = @type', {
  703.         ['@owner'] = identifier,
  704.         ['@type'] = type
  705.     }, function(result)
  706.         if #result == 0 then
  707.             loadedInventories[type][identifier] = {}
  708.             cb({})
  709.             return
  710.         end
  711.         inventory = json.decode(result[1].data)
  712.         loadedInventories[type][identifier] = inventory
  713.         cb(inventory)
  714.         TriggerEvent('disc-inventoryhud:loadedInventory', identifier, type, inventory)
  715.     end)
  716. end
  717.  
  718. function handleWeaponRemoval(data, source)
  719.     if isWeapon(data.originItem.id) then
  720.         if data.originOwner == data.destinationOwner and data.originTier.name == data.destinationTier.name then
  721.             if data.destinationSlot > 5 then
  722.                 TriggerClientEvent('disc-inventoryhud:removeCurrentWeapon', source)
  723.             end
  724.         else
  725.             TriggerClientEvent('disc-inventoryhud:removeCurrentWeapon', source)
  726.         end
  727.     end
  728. end
  729.  
  730. function handleGiveWeaponRemoval(data, source)
  731.     if isWeapon(data.originItem.id) then
  732.         TriggerClientEvent('disc-inventoryhud:removeCurrentWeapon', source)
  733.     end
  734. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement