Skip to content

Commit

Permalink
refactor(server/inventory): set inv.changed for more functions
Browse files Browse the repository at this point in the history
If an inventory is never touched by a player but is being manipulated
by the server, a save might not trigger for a long period of time.
  • Loading branch information
thelindat committed Mar 10, 2023
1 parent ac0b748 commit 3b04ad9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,13 @@ exports('GetItem', Inventory.GetItem)
function Inventory.SwapSlots(fromInventory, toInventory, slot1, slot2)
local fromSlot = fromInventory.items[slot1] and table.clone(fromInventory.items[slot1]) or nil
local toSlot = toInventory.items[slot2] and table.clone(toInventory.items[slot2]) or nil

if fromSlot then fromSlot.slot = slot2 end
if toSlot then toSlot.slot = slot1 end

fromInventory.items[slot1], toInventory.items[slot2] = toSlot, fromSlot
fromInventory.changed, toInventory.changed = true, true

return fromSlot, toSlot
end
exports('SwapSlots', Inventory.SwapSlots)
Expand All @@ -717,9 +721,12 @@ end
---@param metadata? table
function Inventory.SetItem(inv, item, count, metadata)
if type(item) ~= 'table' then item = Items(item) end

if item and count >= 0 then
inv = Inventory(inv) --[[@as OxInventory]]

if inv then
inv.changed = true
local itemCount = Inventory.GetItem(inv, item.name, metadata, true) --[[@as number]]

if count > itemCount then
Expand Down Expand Up @@ -776,6 +783,7 @@ function Inventory.SetDurability(inv, slot, durability)
local slotData = inv and inv.items[slot]

if inv and slotData then
inv.changed = true
slotData.metadata.durability = durability

if inv.player then
Expand All @@ -794,6 +802,7 @@ function Inventory.SetMetadata(inv, slot, metadata)
local slotData = inv and type(slot) == 'number' and inv.items[slot]

if inv and slotData then
inv.changed = true
local imageurl = slotData.metadata.imageurl
slotData.metadata = type(metadata) == 'table' and metadata or { type = metadata or nil }

Expand Down Expand Up @@ -829,6 +838,7 @@ function Inventory.SetSlotCount(inv, slots)
if not inv then return end
if type(slots) ~= 'number' then return end

inv.changed = true
inv.slots = slots
end

Expand Down Expand Up @@ -906,6 +916,8 @@ function Inventory.AddItem(inv, item, count, metadata, slot, cb)
end

if toSlot then
inv.changed = true

local invokingResource = server.loglevel > 1 and GetInvokingResource()

if type(toSlot) == 'number' then
Expand Down Expand Up @@ -1108,6 +1120,8 @@ function Inventory.RemoveItem(inv, item, count, metadata, slot, ignoreTotal)
end

if removed > 0 then
inv.changed = true

if inv.player then
if server.syncInventory then server.syncInventory(inv) end

Expand Down Expand Up @@ -1631,11 +1645,15 @@ end)

function Inventory.Confiscate(source)
local inv = Inventories[source]

if inv?.player then
db.saveStash(inv.owner, inv.owner, json.encode(minimal(inv)))
table.wipe(inv.items)
inv.weight = 0
inv.changed = true

TriggerClientEvent('ox_inventory:inventoryConfiscated', inv.id)

if server.syncInventory then server.syncInventory(inv) end
end
end
Expand Down Expand Up @@ -1664,6 +1682,7 @@ function Inventory.Return(source)
end
end

inv.changed = true
inv.weight = totalWeight
inv.items = inventory

Expand Down

0 comments on commit 3b04ad9

Please sign in to comment.