Skip to content

Commit

Permalink
refactor: use callback event for giveItem
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 13, 2024
1 parent 8a0b920 commit e29e0c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 6 additions & 1 deletion client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,12 @@ local function giveItemToTarget(serverId, slotId, count)
end

Utils.PlayAnim(0, 'mp_common', 'givetake1_a', 1.0, 1.0, 2000, 50, 0.0, 0, 0, 0)
TriggerServerEvent('ox_inventory:giveItem', slotId, serverId, count or 0)

local notification = lib.callback.await('ox_inventory:giveItem', false, slotId, serverId, count or 0)

if notification then
lib.notify({ type = 'error', description = locale(table.unpack(notification)) })
end
end

exports('giveItemToTarget', giveItemToTarget)
Expand Down
15 changes: 9 additions & 6 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2408,8 +2408,8 @@ RegisterServerEvent('ox_inventory:closeInventory', function()
end
end)

RegisterServerEvent('ox_inventory:giveItem', function(slot, target, count)
local fromInventory = Inventories[source]
local function giveItem(playerId, slot, target, count)
local fromInventory = Inventories[playerId]
local toInventory = Inventories[target]

if count <= 0 then count = 1 end
Expand All @@ -2422,15 +2422,15 @@ RegisterServerEvent('ox_inventory:giveItem', function(slot, target, count)
local item = Items(data.name)

if not item or data.count < count or not Inventory.CanCarryItem(toInventory, item, count, data.metadata) or #(GetEntityCoords(fromInventory.player.ped) - GetEntityCoords(toInventory.player.ped)) > 15 then
return TriggerClientEvent('ox_lib:notify', fromInventory.id, { type = 'error', description = locale('cannot_give', count, data.label) })
return { 'cannot_give', count, data.label }
end

local toSlot = Inventory.GetSlotForItem(toInventory, data.name, data.metadata)
local fromRef = ('%s:%s'):format(fromInventory.id, slot)
local toRef = ('%s:%s'):format(toInventory.id, toSlot)

if activeSlots[fromRef] or activeSlots[toRef] then
return TriggerClientEvent('ox_lib:notify', fromInventory.id, { type = 'error', description = locale('cannot_give', count, data.label) })
return { 'cannot_give', count, data.label }
end

activeSlots[fromRef] = true
Expand Down Expand Up @@ -2463,9 +2463,12 @@ RegisterServerEvent('ox_inventory:giveItem', function(slot, target, count)
end
end

return TriggerClientEvent('ox_lib:notify', fromInventory.id, { type = 'error', description = locale('cannot_give', count, data.label) })
return { 'cannot_give', count, data.label }
end
end)
end

lib.callback.register('ox_inventory:giveItem', giveItem)
RegisterServerEvent('ox_inventory:giveItem', function(...) giveItem(source, ...) end)

local function updateWeapon(source, action, value, slot, specialAmmo)
local inventory = Inventories[source]
Expand Down

0 comments on commit e29e0c1

Please sign in to comment.