Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit 7a6580a

Browse files
committed
fix(server/framework): handle item arrays for DoesPlayerHaveItem
1 parent a7dea12 commit 7a6580a

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

server/framework/es_extended.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ SetTimeout(0, function()
1414
if player then player.removeInventoryItem(item, 1) end
1515
end
1616

17-
function DoesPlayerHaveItem(player, items)
17+
---@param player table
18+
---@param items string[] | { name: string, remove?: boolean, metadata?: string }[]
19+
---@param removeItem? boolean
20+
---@return string?
21+
function DoesPlayerHaveItem(player, items, removeItem)
1822
for i = 1, #items do
1923
local item = items[i]
20-
local data = player.getInventoryItem(item.name)
24+
local itemName = item.name or item
25+
local data = player.getInventoryItem(itemName)
2126

2227
if data?.count > 0 then
23-
if item.remove then
24-
player.removeInventoryItem(item.name, 1)
28+
if removeItem or item.remove then
29+
player.removeInventoryItem(itemName, 1)
2530
end
2631

27-
return item.name
32+
return itemName
2833
end
2934
end
30-
31-
return false
3235
end
3336
end
3437
end)

server/framework/qb-core.lua

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,38 @@ SetTimeout(0, function()
1414
if player then player.Functions.RemoveItem(item, 1, slot) end
1515
end
1616

17-
function DoesPlayerHaveItem(player, items)
17+
---@param player table
18+
---@param items string[] | { name: string, remove?: boolean, metadata?: string }[]
19+
---@param removeItem? boolean
20+
---@return string?
21+
function DoesPlayerHaveItem(player, items, removeItem)
1822
for i = 1, #items do
1923
local item = items[i]
24+
local itemName = item.name or item
2025

2126
if item.metadata then
22-
local playerItems = player.Functions.GetItemsByName(item.name)
27+
local playerItems = player.Functions.GetItemsByName(itemName)
2328

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

2732
if data.info.type == item.metadata then
28-
if item.remove then
29-
player.Functions.RemoveItem(item.name, 1, data.slot)
33+
if removeItem or item.remove then
34+
player.Functions.RemoveItem(itemName, 1, data.slot)
3035
end
3136

32-
return item.name
37+
return itemName
3338
end
3439
end
3540
else
36-
local data = player.Functions.GetItemByName(item.name)
41+
local data = player.Functions.GetItemByName(itemName)
3742

3843
if data then
3944
if item.remove then
40-
player.Functions.RemoveItem(item.name, 1, data.slot)
45+
player.Functions.RemoveItem(itemName, 1, data.slot)
4146
end
4247

43-
return item.name
48+
return itemName
4449
end
4550
end
4651
end

server/main.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ end
173173

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

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

186186
if data and data.count > 0 then
187-
if alwaysRemove or item.remove then
187+
if removeItem or item.remove then
188188
ox_inventory:RemoveItem(playerId, itemName, 1, nil, data.slot)
189189
end
190190

0 commit comments

Comments
 (0)