Skip to content

Commit

Permalink
Merge pull request #479 from DoopieWop/fix-fallback-code-item-remove
Browse files Browse the repository at this point in the history
Changed ITEM:Remove() fallback code to work and (hopefully) be more reliable
  • Loading branch information
alexgrist authored Jan 19, 2025
2 parents 418947a + 2dc0235 commit d5f26fa
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions gamemode/core/meta/sh_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,9 @@ end
-- @treturn bool Whether the item was successfully deleted or not
function ITEM:Remove(bNoReplication, bNoDelete)
local inv = ix.item.inventories[self.invID]
local bFailed = false

if (self.invID > 0 and inv) then
local failed = false

for x = self.gridX, self.gridX + (self.width - 1) do
if (inv.slots[x]) then
for y = self.gridY, self.gridY + (self.height - 1) do
Expand All @@ -429,29 +428,31 @@ function ITEM:Remove(bNoReplication, bNoDelete)
if (item and item.id == self.id) then
inv.slots[x][y] = nil
else
failed = true
bFailed = true
end
end
else
bFailed = true
end
end

if (failed) then
inv.slots = {}
for k, _ in inv:Iter() do
if (k.invID == inv:GetID()) then
for x = self.gridX, self.gridX + (self.width - 1) do
for y = self.gridY, self.gridY + (self.height - 1) do
inv.slots[x][y] = k.id
end
end
if (bFailed) then
local items = {}
for _, v in pairs(ix.item.instances) do
if (v.invID == self.invID and v.id != self.id) then
items[#items + 1] = v
end
end

if (IsValid(inv.owner) and inv.owner:IsPlayer()) then
inv:Sync(inv.owner, true)
inv.slots = {}
for _, v in ipairs(items) do
for x = v.gridX, v.gridX + (v.width - 1) do
for y = v.gridY, v.gridY + (v.height - 1) do
inv.slots[x] = inv.slots[x] or {}
inv.slots[x][y] = v
end
end
end

return false
end
else
-- @todo definition probably isn't needed
Expand All @@ -472,10 +473,14 @@ function ITEM:Remove(bNoReplication, bNoDelete)
local receivers = inv.GetReceivers and inv:GetReceivers()

if (self.invID != 0 and istable(receivers)) then
net.Start("ixInventoryRemove")
net.WriteUInt(self.id, 32)
net.WriteUInt(self.invID, 32)
net.Send(receivers)
if (bFailed) then
inv:Sync(receivers)
else
net.Start("ixInventoryRemove")
net.WriteUInt(self.id, 32)
net.WriteUInt(self.invID, 32)
net.Send(receivers)
end
end

if (!bNoDelete) then
Expand Down

0 comments on commit d5f26fa

Please sign in to comment.