Skip to content

Commit

Permalink
fix(server/framework): handle item arrays for DoesPlayerHaveItem
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 12, 2023
1 parent a7dea12 commit 7a6580a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
17 changes: 10 additions & 7 deletions server/framework/es_extended.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ SetTimeout(0, function()
if player then player.removeInventoryItem(item, 1) end
end

function DoesPlayerHaveItem(player, items)
---@param player table
---@param items string[] | { name: string, remove?: boolean, metadata?: string }[]
---@param removeItem? boolean
---@return string?
function DoesPlayerHaveItem(player, items, removeItem)
for i = 1, #items do
local item = items[i]
local data = player.getInventoryItem(item.name)
local itemName = item.name or item
local data = player.getInventoryItem(itemName)

if data?.count > 0 then
if item.remove then
player.removeInventoryItem(item.name, 1)
if removeItem or item.remove then
player.removeInventoryItem(itemName, 1)
end

return item.name
return itemName
end
end

return false
end
end
end)
Expand Down
21 changes: 13 additions & 8 deletions server/framework/qb-core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,38 @@ SetTimeout(0, function()
if player then player.Functions.RemoveItem(item, 1, slot) end
end

function DoesPlayerHaveItem(player, items)
---@param player table
---@param items string[] | { name: string, remove?: boolean, metadata?: string }[]
---@param removeItem? boolean
---@return string?
function DoesPlayerHaveItem(player, items, removeItem)
for i = 1, #items do
local item = items[i]
local itemName = item.name or item

if item.metadata then
local playerItems = player.Functions.GetItemsByName(item.name)
local playerItems = player.Functions.GetItemsByName(itemName)

for j = 1, #playerItems do
local data = playerItems[j]

if data.info.type == item.metadata then
if item.remove then
player.Functions.RemoveItem(item.name, 1, data.slot)
if removeItem or item.remove then
player.Functions.RemoveItem(itemName, 1, data.slot)
end

return item.name
return itemName
end
end
else
local data = player.Functions.GetItemByName(item.name)
local data = player.Functions.GetItemByName(itemName)

if data then
if item.remove then
player.Functions.RemoveItem(item.name, 1, data.slot)
player.Functions.RemoveItem(itemName, 1, data.slot)
end

return item.name
return itemName
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ end

---@param player table
---@param items string[] | { name: string, remove?: boolean, metadata?: string }[]
---@param alwaysRemove? boolean
---@param removeItem? boolean
---@return string?
function DoesPlayerHaveItem(player, items, alwaysRemove)
function DoesPlayerHaveItem(player, items, removeItem)
local playerId = player.source or player.PlayerData.source

for i = 1, #items do
Expand All @@ -184,7 +184,7 @@ function DoesPlayerHaveItem(player, items, alwaysRemove)
local data = ox_inventory:Search(playerId, 'slots', itemName, item.metadata)[1]

if data and data.count > 0 then
if alwaysRemove or item.remove then
if removeItem or item.remove then
ox_inventory:RemoveItem(playerId, itemName, 1, nil, data.slot)
end

Expand Down

0 comments on commit 7a6580a

Please sign in to comment.