Skip to content

Commit

Permalink
Account for different data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
GhzGarage committed Oct 17, 2024
1 parent ee02bf4 commit 9afdc87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
30 changes: 24 additions & 6 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ exports('CanAddItem', CanAddItem)
--- @param source number The player's server ID.
--- @return number - Returns the free weight of the players inventory. Error will return 0
function GetFreeWeight(source)
if not source then warn("Source was not passed into GetFreeWeight") return 0 end
if not source then
warn('Source was not passed into GetFreeWeight')
return 0
end
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return 0 end

Expand Down Expand Up @@ -559,9 +562,9 @@ function OpenInventory(source, identifier, data)
end

if not inventory then inventory = InitializeInventory(identifier, data) end
inventory.maxweight = (data and data.maxweight) or (inventory and inventory.maxweight) or Config.StashSize.maxweight
inventory.slots = (data and data.slots) or (inventory and inventory.slots) or Config.StashSize.slots
inventory.label = (data and data.label) or (inventory and inventory.label) or identifier
inventory.maxweight = (data and data.maxweight) or (inventory and inventory.maxweight) or Config.StashSize.maxweight
inventory.slots = (data and data.slots) or (inventory and inventory.slots) or Config.StashSize.slots
inventory.label = (data and data.label) or (inventory and inventory.label) or identifier
inventory.isOpen = source

local formattedInventory = {
Expand Down Expand Up @@ -699,6 +702,7 @@ function RemoveItem(identifier, item, amount, slot, reason)
print('RemoveItem: Invalid item')
return false
end

local inventory
local player = QBCore.Functions.GetPlayer(identifier)

Expand All @@ -722,7 +726,17 @@ function RemoveItem(identifier, item, amount, slot, reason)
return false
end

local inventoryItem = inventory[slot]
local inventoryItem = nil
local itemKey = nil

for key, invItem in pairs(inventory) do
if invItem.slot == slot then
inventoryItem = invItem
itemKey = key
break
end
end

if not inventoryItem or inventoryItem.name:lower() ~= item:lower() then
print('RemoveItem: Item not found in slot')
return false
Expand All @@ -736,13 +750,17 @@ function RemoveItem(identifier, item, amount, slot, reason)

inventoryItem.amount = inventoryItem.amount - amount
if inventoryItem.amount <= 0 then
inventory[slot] = nil
inventory[itemKey] = nil
else
inventory[itemKey] = inventoryItem
end

if player then player.Functions.SetPlayerData('items', inventory) end

local invName = player and GetPlayerName(identifier) .. ' (' .. identifier .. ')' or identifier
local removeReason = reason or 'No reason specified'
local resourceName = GetInvokingResource() or 'qb-inventory'

TriggerEvent(
'qb-log:server:CreateLog',
'playerinventory',
Expand Down
26 changes: 19 additions & 7 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -438,22 +438,34 @@ end)
-- Item move logic

local function getItem(inventoryId, src, slot)
local item
local items = {}
if inventoryId == 'player' then
local Player = QBCore.Functions.GetPlayer(src)
item = Player.PlayerData.items[slot]
if Player and Player.PlayerData.items then
items = Player.PlayerData.items
end
elseif inventoryId:find('otherplayer-') then
local targetId = tonumber(inventoryId:match('otherplayer%-(.+)'))
local targetPlayer = QBCore.Functions.GetPlayer(targetId)
if targetPlayer then
item = targetPlayer.PlayerData.items[slot]
if targetPlayer and targetPlayer.PlayerData.items then
items = targetPlayer.PlayerData.items
end
elseif inventoryId:find('drop-') == 1 then
item = Drops[inventoryId]['items'][slot]
if Drops[inventoryId] and Drops[inventoryId]['items'] then
items = Drops[inventoryId]['items']
end
else
item = Inventories[inventoryId]['items'][slot]
if Inventories[inventoryId] and Inventories[inventoryId]['items'] then
items = Inventories[inventoryId]['items']
end
end

for _, item in pairs(items) do
if item.slot == slot then
return item
end
end
return item
return nil
end

local function getIdentifier(inventoryId, src)
Expand Down

0 comments on commit 9afdc87

Please sign in to comment.