Files
community_bridge/modules/inventory/codem-inventory/server.lua
T
MrNewb 2a326c4811 GetPlayerInventory client side, event fires client now
community_bridge:client:inventory:updateInventory
2025-02-05 01:54:55 -05:00

102 lines
3.4 KiB
Lua

if GetResourceState('codem-inventory') ~= 'started' then return end
local codem = exports['codem-inventory']
Inventory = Inventory or {}
Inventory.AddItem = function(src, item, count, slot, metadata)
TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "add", item = item, count = count, slot = slot, metadata = metadata})
return codem:AddItem(src, item, count, slot, metadata)
end
Inventory.RemoveItem = function(src, item, count, slot, metadata)
if metadata == nil then
TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "remove", item = item, count = count, slot = slot, metadata = metadata})
return codem:RemoveItem(src, item, count, slot)
else
local items = codem:GetInventory(nil, src)
for _, itemInfo in pairs(items) do
if itemInfo.name == item then
if itemInfo.info == metadata then
TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "remove", item = item, count = count, slot = itemInfo.slot, metadata = metadata})
return codem:RemoveItem(src, item, count, itemInfo.slot)
end
end
end
end
end
Inventory.GetItemInfo = function(item)
local itemData = codem:GetItemList()
if not itemData[item] then return {} end
local repackedTable = {
name = itemData.name or "Missing Name",
label = itemData.label or "Missing Label",
stack = itemData.unique or "false",
weight = itemData.weight or "0",
description = itemData.description or "none",
image = itemData.image or Inventory.GetImagePath(item),
}
return repackedTable
end
Inventory.GetItemCount = function(src, item, metadata)
if metadata == nil then
return codem:GetItemsTotalAmount(src, item)
else
local count = 0
local items = codem:GetInventory(nil, src)
for _, itemInfo in pairs(items) do
if itemInfo.name == item and itemInfo.info == metadata then
count = count + itemInfo.amount
end
return count
end
end
end
Inventory.GetItemBySlot = function(src, slot)
local slotData = codem:GetItemBySlot(src, slot)
-- The keys are not documented, so this is a best guess
for _, v in pairs(slotData) do
return {
name = v.name,
label = v.name,
weight = v.weight,
slot = v.slot,
count = v.amount,
metadata = v.info,
stack = v.unique,
description = v.description
}
end
return {}
end
Inventory.GetPlayerInventory = function(src)
local items = codem:GetInventory(nil, src)
local repackedTable = {}
for _, v in pairs(items) do
table.insert(repackedTable, {
name = v.name,
count = v.amount,
metadata = v.info,
slot = v.slot,
})
end
return items
end
Inventory.SetMetadata = function(src, item, slot, metadata)
codem:SetItemMetadata(src, slot, metadata)
end
Inventory.OpenStash = function(src, id, label, slots, weight, owner, groups, coords)
TriggerEvent('codem-inventory:server:openstash', id, slots, weight, label)
end
Inventory.GetImagePath = function(item)
local imagePath = string.format("nui://codem-inventory/html/images/%s.png", item)
return imagePath or "https://avatars.githubusercontent.com/u/47620135"
end