Skip to content

Commit

Permalink
fix(server): esx inventory doesn't load on resource restart
Browse files Browse the repository at this point in the history
Inventory is generally an array when first loading, but may become a
hashmap or mixed table when restarting the resource.
Since the table isn't an array as expected, it attempts to convert the item data (and fails).
  • Loading branch information
thelindat committed Aug 23, 2022
1 parent c870656 commit c99d6e1
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,31 @@ function server.setPlayerInventory(player, data)
local inventory = {}
local totalWeight = 0

if data and table.type(data) ~= 'empty' then
if data and next(data) then
local ostime = os.time()

if table.type(data) == 'array' then
for _, v in pairs(data) do
local item = Items(v.name)

if item then
if v.metadata then
v.metadata = Items.CheckMetadata(v.metadata, item, v.name, ostime)
end
for _, v in pairs(data) do
if type(v) == 'number' then
if server.convertInventory then
inventory, totalWeight = server.convertInventory(player.source, data)
break
else
return error(('Inventory for player.%s (%s) contains invalid data. Ensure you have converted inventories to the correct format.'):format(player.source, GetPlayerName(player.source)))
end
end

local weight = Inventory.SlotWeight(item, v)
totalWeight = totalWeight + weight
local item = Items(v.name)

inventory[v.slot] = {name = item.name, label = item.label, weight = weight, slot = v.slot, count = v.count, description = item.description, metadata = v.metadata, stack = item.stack, close = item.close}
if item then
if v.metadata then
v.metadata = Items.CheckMetadata(v.metadata, item, v.name, ostime)
end

local weight = Inventory.SlotWeight(item, v)
totalWeight = totalWeight + weight

inventory[v.slot] = {name = item.name, label = item.label, weight = weight, slot = v.slot, count = v.count, description = item.description, metadata = v.metadata, stack = item.stack, close = item.close}
end
elseif server.convertInventory then
inventory, totalWeight = server.convertInventory(player.source, data)
else
return error(('Inventory for player.%s (%s) contains invalid data. Ensure you have converted inventories to the correct format.'):format(player.source, GetPlayerName(player.source)))
end
end

Expand Down

0 comments on commit c99d6e1

Please sign in to comment.