⚙️Installation
ensure oxmysql
ensure es_extended or qb-core
ensure ox_lib
...
ensure p6_bridge
...Item Protection
ox_inventory
RegisterServerEvent('ox_inventory:giveItem', function(slot, target, count)
local fromInventory = Inventories[source]
local toInventory = Inventories[target]
if count <= 0 then count = 1 end
if toInventory?.player then
local data = fromInventory.items[slot]
if not data then return end
local item = Items(data.name)
-- Snippet to protect item swap
if exports['p6_bridge']:CheckIfProtectedItem(data.name) then
return TriggerClientEvent('ox_lib:notify', fromInventory.id, { type = 'error', description = "It's a protected item, cannot be transfer" })
end
-- Snippet to protect item swap
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) })
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) })
end
activeSlots[fromRef] = true
activeSlots[toRef] = true
local _ <close> = defer(function()
activeSlots[fromRef] = nil
activeSlots[toRef] = nil
end)
if TriggerEventHooks('swapItems', {
source = fromInventory.id,
fromInventory = fromInventory.id,
fromType = fromInventory.type,
toInventory = toInventory.id,
toType = toInventory.type,
count = data.count,
action = 'give',
fromSlot = data,
}) then
---@todo manually call swapItems or something?
if Inventory.AddItem(toInventory, item, count, data.metadata, toSlot) then
if Inventory.RemoveItem(fromInventory, item, count, data.metadata, slot) then
if server.loglevel > 0 then
lib.logger(fromInventory.owner, 'giveItem', ('"%s" gave %sx %s to "%s"'):format(fromInventory.label, count, data.name, toInventory.label))
end
return
end
end
end
return TriggerClientEvent('ox_lib:notify', fromInventory.id, { type = 'error', description = locale('cannot_give', count, data.label) })
end
end)Last updated