Skip to content

Commit

Permalink
Use inventory:Iter() instead of pairs(inventory:GetItems())
Browse files Browse the repository at this point in the history
  • Loading branch information
Winkarst-cpu committed Jan 6, 2025
1 parent 4e49199 commit 3360d82
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 36 deletions.
2 changes: 1 addition & 1 deletion gamemode/core/derma/cl_inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ hook.Add("CreateMenuButtons", "ixInventory", function(tabs)
ix.gui.inv1 = panel

if (ix.option.Get("openBags", true)) then
for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (!v.isBag) then
continue
end
Expand Down
2 changes: 1 addition & 1 deletion gamemode/core/hooks/sh_hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ function GM:CanTransferItem(itemObject, curInv, inventory)

-- don't allow transferring items that are in use
if (inventory) then
for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (v:GetData("equip") == true) then
local owner = itemObject:GetOwner()

Expand Down
4 changes: 2 additions & 2 deletions gamemode/core/hooks/sv_hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ function GM:PostPlayerLoadout(client)
local character = client:GetCharacter()

if (character:GetInventory()) then
for _, v in pairs(character:GetInventory():GetItems()) do
for _, v in character:GetInventory():Iter() do
v:Call("OnLoadout", client)

if (v:GetData("equip") and v.attribBoosts) then
Expand Down Expand Up @@ -870,7 +870,7 @@ end
function GM:CharacterPreSave(character)
local client = character:GetPlayer()

for _, v in pairs(character:GetInventory():GetItems()) do
for _, v in character:GetInventory():Iter() do
if (v.OnSave) then
v:Call("OnSave", client)
end
Expand Down
8 changes: 4 additions & 4 deletions gamemode/core/libs/sh_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ if (SERVER) then
inventory.storageInfo = info

-- remove context from any bags this inventory might have
for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (v.isBag and v:GetInventory()) then
ix.storage.CreateContext(v:GetInventory(), table.Copy(info))
end
Expand All @@ -110,7 +110,7 @@ if (SERVER) then
inventory.storageInfo = nil

-- remove context from any bags this inventory might have
for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (v.isBag and v:GetInventory()) then
ix.storage.RemoveContext(v:GetInventory())
end
Expand Down Expand Up @@ -159,7 +159,7 @@ if (SERVER) then
client.ixOpenStorage = inventory

-- update receivers for any bags this inventory might have
for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (v.isBag and v:GetInventory()) then
v:GetInventory():AddReceiver(client)
end
Expand Down Expand Up @@ -192,7 +192,7 @@ if (SERVER) then
inventory:RemoveReceiver(client)

-- update receivers for any bags this inventory might have
for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (v.isBag and v:GetInventory()) then
v:GetInventory():RemoveReceiver(client)
end
Expand Down
17 changes: 6 additions & 11 deletions gamemode/core/meta/sh_inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ end

-- this is pretty good to debug/develop function to use.
function META:Print(printPos)
for k, v in pairs(self:GetItems()) do
for k, v in self:Iter() do
local str = k .. ": " .. v.name

if (printPos) then
Expand All @@ -102,7 +102,7 @@ end
-- This function can be helpful for getting rid of those pesky errors.
-- @realm shared
function META:FindError()
for _, v in pairs(self:GetItems()) do
for _, v in self:Iter() do
if (v.width == 1 and v.height == 1) then
continue
end
Expand Down Expand Up @@ -619,9 +619,7 @@ end
-- -- do something with the item table
-- end
function META:HasItem(targetID, data)
local items = self:GetItems()

for _, v in pairs(items) do
for _, v in self:Iter() do
if (v.uniqueID == targetID) then
if (data) then
local itemData = v.data
Expand Down Expand Up @@ -660,11 +658,10 @@ end
-- if not Entity(1):GetCharacter():GetInventory():HasItems(itemFilter) then return end
-- -- Filters out if this player has both a water, and a sparkling water.
function META:HasItems(targetIDs)
local items = self:GetItems()
local count = #targetIDs -- assuming array
targetIDs = table.Copy(targetIDs)

for _, v in pairs(items) do
for _, v in self:Iter() do
for k, targetID in ipairs(targetIDs) do
if (v.uniqueID == targetID) then
table.remove(targetIDs, k)
Expand Down Expand Up @@ -695,9 +692,7 @@ end
-- end
-- -- Notifies the player that they should get some more guns.
function META:HasItemOfBase(baseID, data)
local items = self:GetItems()

for _, v in pairs(items) do
for _, v in self:Iter() do
if (v.base == baseID) then
if (data) then
local itemData = v.data
Expand Down Expand Up @@ -960,7 +955,7 @@ if (SERVER) then
net.WriteTable(self.vars or {})
net.Send(receiver)

for _, v in pairs(self:GetItems()) do
for _, v in self:Iter() do
v:Call("OnSendData", receiver)
end
end
Expand Down
4 changes: 1 addition & 3 deletions gamemode/core/meta/sh_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,8 @@ function ITEM:Remove(bNoReplication, bNoDelete)
end

if (failed) then
local items = inv:GetItems()

inv.slots = {}
for _, v in pairs(items) do
for _, v in inv:Iter() do
if (v.invID == inv:GetID()) then
for x = self.gridX, self.gridX + (self.width - 1) do
for y = self.gridY, self.gridY + (self.height - 1) do
Expand Down
2 changes: 1 addition & 1 deletion gamemode/items/base/sh_bags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function ITEM:CanTransfer(oldInventory, newInventory)
return false
end

for _, v in pairs(self:GetInventory():GetItems()) do
for _, v in self:GetInventory():Iter() do
if (v:GetData("id") == index2) then
return false
end
Expand Down
3 changes: 1 addition & 2 deletions gamemode/items/base/sh_outfit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ ITEM.functions.Equip = {
OnRun = function(item)
local client = item.player
local char = client:GetCharacter()
local items = char:GetInventory():GetItems()

for _, v in pairs(items) do
for _, v in char:GetInventory():Iter() do
if (v.id != item.id) then
local itemTable = ix.item.instances[v.id]

Expand Down
3 changes: 1 addition & 2 deletions gamemode/items/base/sh_pacoutfit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ ITEM.functions.Equip = {
icon = "icon16/tick.png",
OnRun = function(item)
local char = item.player:GetCharacter()
local items = char:GetInventory():GetItems()

for _, v in pairs(items) do
for _, v in char:GetInventory():Iter() do
if (v.id != item.id) then
local itemTable = ix.item.instances[v.id]

Expand Down
6 changes: 2 additions & 4 deletions gamemode/items/base/sh_weapons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ function ITEM:RemovePAC(client)
end

function ITEM:Equip(client, bNoSelect, bNoSound)
local items = client:GetCharacter():GetInventory():GetItems()

client.carryWeapons = client.carryWeapons or {}

for _, v in pairs(items) do
for _, v in client:GetCharacter():GetInventory():Iter() do
if (v.id != self.id) then
local itemTable = ix.item.instances[v.id]

Expand Down Expand Up @@ -289,7 +287,7 @@ end
hook.Add("PlayerDeath", "ixStripClip", function(client)
client.carryWeapons = {}

for _, v in pairs(client:GetCharacter():GetInventory():GetItems()) do
for _, v in client:GetCharacter():GetInventory():Iter() do
if (v.isWeapon and v:GetData("equip")) then
v:SetData("ammo", nil)
v:SetData("equip", nil)
Expand Down
4 changes: 2 additions & 2 deletions plugins/logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ if (SERVER) then
return
end

for _, v in pairs(bagInventory:GetItems()) do
for _, v in bagInventory:Iter() do
ix.log.Add(character:GetPlayer(), "inventoryAdd", character:GetName(), v:GetName(), v:GetID())
end
end
Expand All @@ -220,7 +220,7 @@ if (SERVER) then
ix.log.Add(character:GetPlayer(), "inventoryRemove", character:GetName(), item:GetName(), item:GetID())

if (item.isBag) then
for _, v in pairs(item:GetInventory():GetItems()) do
for _, v in item:GetInventory():Iter() do
ix.log.Add(character:GetPlayer(), "inventoryRemove", character:GetName(), v:GetName(), v:GetID())
end
end
Expand Down
4 changes: 2 additions & 2 deletions plugins/pac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ if (SERVER) then
if (curChar) then
local inv = curChar:GetInventory()

for _, v in pairs(inv:GetItems()) do
for _, v in inv:Iter() do
if (v:GetData("equip") == true and v.pacData) then
client:AddPart(v.uniqueID, v)
end
Expand Down Expand Up @@ -156,7 +156,7 @@ if (SERVER) then
local character = client:GetCharacter()
local inventory = character:GetInventory()

for _, v in pairs(inventory:GetItems()) do
for _, v in inventory:Iter() do
if (v:GetData("equip") == true and v.pacData) then
client:AddPart(v.uniqueID, v)
end
Expand Down
2 changes: 1 addition & 1 deletion plugins/vendor/sh_plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ if (SERVER) then

local invOkay = true

for _, v in pairs(client:GetCharacter():GetInventory():GetItems()) do
for _, v in client:GetCharacter():GetInventory():Iter() do
if (v.uniqueID == uniqueID and v:GetID() != 0 and ix.item.instances[v:GetID()] and v:GetData("equip", false) == false) then
invOkay = v:Remove()
found = true
Expand Down

0 comments on commit 3360d82

Please sign in to comment.