Skip to content

Commit d9c9c86

Browse files
authored
Merge branch 'main' into main
2 parents 117cdf3 + 9afdc87 commit d9c9c86

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

server/functions.lua

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ exports('CanAddItem', CanAddItem)
393393
--- @param source number The player's server ID.
394394
--- @return number - Returns the free weight of the players inventory. Error will return 0
395395
function GetFreeWeight(source)
396-
if not source then warn("Source was not passed into GetFreeWeight") return 0 end
396+
if not source then
397+
warn('Source was not passed into GetFreeWeight')
398+
return 0
399+
end
397400
local Player = QBCore.Functions.GetPlayer(source)
398401
if not Player then return 0 end
399402

@@ -604,9 +607,9 @@ function OpenInventory(source, identifier, data)
604607
end
605608

606609
if not inventory then inventory = InitializeInventory(identifier, data) end
607-
inventory.maxweight = (data and data.maxweight) or (inventory and inventory.maxweight) or Config.StashSize.maxweight
608-
inventory.slots = (data and data.slots) or (inventory and inventory.slots) or Config.StashSize.slots
609-
inventory.label = (data and data.label) or (inventory and inventory.label) or identifier
610+
inventory.maxweight = (data and data.maxweight) or (inventory and inventory.maxweight) or Config.StashSize.maxweight
611+
inventory.slots = (data and data.slots) or (inventory and inventory.slots) or Config.StashSize.slots
612+
inventory.label = (data and data.label) or (inventory and inventory.label) or identifier
610613
inventory.isOpen = source
611614

612615
local formattedInventory = {
@@ -774,6 +777,7 @@ function RemoveItem(identifier, item, amount, slot, reason)
774777
print('RemoveItem: Invalid item')
775778
return false
776779
end
780+
777781
local inventory
778782
local player = QBCore.Functions.GetPlayer(identifier)
779783

@@ -797,7 +801,17 @@ function RemoveItem(identifier, item, amount, slot, reason)
797801
return false
798802
end
799803

800-
local inventoryItem = inventory[slot]
804+
local inventoryItem = nil
805+
local itemKey = nil
806+
807+
for key, invItem in pairs(inventory) do
808+
if invItem.slot == slot then
809+
inventoryItem = invItem
810+
itemKey = key
811+
break
812+
end
813+
end
814+
801815
if not inventoryItem or inventoryItem.name:lower() ~= item:lower() then
802816
print('RemoveItem: Item not found in slot')
803817
return false
@@ -811,13 +825,17 @@ function RemoveItem(identifier, item, amount, slot, reason)
811825

812826
inventoryItem.amount = inventoryItem.amount - amount
813827
if inventoryItem.amount <= 0 then
814-
inventory[slot] = nil
828+
inventory[itemKey] = nil
829+
else
830+
inventory[itemKey] = inventoryItem
815831
end
816832

817833
if player then player.Functions.SetPlayerData('items', inventory) end
834+
818835
local invName = player and GetPlayerName(identifier) .. ' (' .. identifier .. ')' or identifier
819836
local removeReason = reason or 'No reason specified'
820837
local resourceName = GetInvokingResource() or 'qb-inventory'
838+
821839
TriggerEvent(
822840
'qb-log:server:CreateLog',
823841
'playerinventory',

server/main.lua

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,22 +438,34 @@ end)
438438
-- Item move logic
439439

440440
local function getItem(inventoryId, src, slot)
441-
local item
441+
local items = {}
442442
if inventoryId == 'player' then
443443
local Player = QBCore.Functions.GetPlayer(src)
444-
item = Player.PlayerData.items[slot]
444+
if Player and Player.PlayerData.items then
445+
items = Player.PlayerData.items
446+
end
445447
elseif inventoryId:find('otherplayer-') then
446448
local targetId = tonumber(inventoryId:match('otherplayer%-(.+)'))
447449
local targetPlayer = QBCore.Functions.GetPlayer(targetId)
448-
if targetPlayer then
449-
item = targetPlayer.PlayerData.items[slot]
450+
if targetPlayer and targetPlayer.PlayerData.items then
451+
items = targetPlayer.PlayerData.items
450452
end
451453
elseif inventoryId:find('drop-') == 1 then
452-
item = Drops[inventoryId]['items'][slot]
454+
if Drops[inventoryId] and Drops[inventoryId]['items'] then
455+
items = Drops[inventoryId]['items']
456+
end
453457
else
454-
item = Inventories[inventoryId]['items'][slot]
458+
if Inventories[inventoryId] and Inventories[inventoryId]['items'] then
459+
items = Inventories[inventoryId]['items']
460+
end
461+
end
462+
463+
for _, item in pairs(items) do
464+
if item.slot == slot then
465+
return item
466+
end
455467
end
456-
return item
468+
return nil
457469
end
458470

459471
local function getIdentifier(inventoryId, src)

0 commit comments

Comments
 (0)