From 3360d820592de4b78f0df96826483c2155d1eb5e Mon Sep 17 00:00:00 2001 From: Winkarst-cpu <74284083+Winkarst-cpu@users.noreply.github.com> Date: Mon, 6 Jan 2025 23:07:27 +0300 Subject: [PATCH] Use inventory:Iter() instead of pairs(inventory:GetItems()) --- gamemode/core/derma/cl_inventory.lua | 2 +- gamemode/core/hooks/sh_hooks.lua | 2 +- gamemode/core/hooks/sv_hooks.lua | 4 ++-- gamemode/core/libs/sh_storage.lua | 8 ++++---- gamemode/core/meta/sh_inventory.lua | 17 ++++++----------- gamemode/core/meta/sh_item.lua | 4 +--- gamemode/items/base/sh_bags.lua | 2 +- gamemode/items/base/sh_outfit.lua | 3 +-- gamemode/items/base/sh_pacoutfit.lua | 3 +-- gamemode/items/base/sh_weapons.lua | 6 ++---- plugins/logging.lua | 4 ++-- plugins/pac.lua | 4 ++-- plugins/vendor/sh_plugin.lua | 2 +- 13 files changed, 25 insertions(+), 36 deletions(-) diff --git a/gamemode/core/derma/cl_inventory.lua b/gamemode/core/derma/cl_inventory.lua index afb0374e8..22505e407 100644 --- a/gamemode/core/derma/cl_inventory.lua +++ b/gamemode/core/derma/cl_inventory.lua @@ -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 diff --git a/gamemode/core/hooks/sh_hooks.lua b/gamemode/core/hooks/sh_hooks.lua index 41b045839..99fb790d9 100644 --- a/gamemode/core/hooks/sh_hooks.lua +++ b/gamemode/core/hooks/sh_hooks.lua @@ -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() diff --git a/gamemode/core/hooks/sv_hooks.lua b/gamemode/core/hooks/sv_hooks.lua index baf7b698c..dd96c2f0e 100644 --- a/gamemode/core/hooks/sv_hooks.lua +++ b/gamemode/core/hooks/sv_hooks.lua @@ -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 @@ -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 diff --git a/gamemode/core/libs/sh_storage.lua b/gamemode/core/libs/sh_storage.lua index 9b8a4c056..f83dc1353 100644 --- a/gamemode/core/libs/sh_storage.lua +++ b/gamemode/core/libs/sh_storage.lua @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/gamemode/core/meta/sh_inventory.lua b/gamemode/core/meta/sh_inventory.lua index c0613ddb7..1b1d139d7 100644 --- a/gamemode/core/meta/sh_inventory.lua +++ b/gamemode/core/meta/sh_inventory.lua @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/gamemode/core/meta/sh_item.lua b/gamemode/core/meta/sh_item.lua index 9cbc68197..2f4bf982a 100644 --- a/gamemode/core/meta/sh_item.lua +++ b/gamemode/core/meta/sh_item.lua @@ -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 diff --git a/gamemode/items/base/sh_bags.lua b/gamemode/items/base/sh_bags.lua index 6dc2a1c20..660726c12 100644 --- a/gamemode/items/base/sh_bags.lua +++ b/gamemode/items/base/sh_bags.lua @@ -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 diff --git a/gamemode/items/base/sh_outfit.lua b/gamemode/items/base/sh_outfit.lua index 5b297f95e..8c4e687de 100644 --- a/gamemode/items/base/sh_outfit.lua +++ b/gamemode/items/base/sh_outfit.lua @@ -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] diff --git a/gamemode/items/base/sh_pacoutfit.lua b/gamemode/items/base/sh_pacoutfit.lua index 9a58fcfe4..a9e8b2e63 100644 --- a/gamemode/items/base/sh_pacoutfit.lua +++ b/gamemode/items/base/sh_pacoutfit.lua @@ -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] diff --git a/gamemode/items/base/sh_weapons.lua b/gamemode/items/base/sh_weapons.lua index 82c0835c3..8ef671e83 100644 --- a/gamemode/items/base/sh_weapons.lua +++ b/gamemode/items/base/sh_weapons.lua @@ -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] @@ -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) diff --git a/plugins/logging.lua b/plugins/logging.lua index 422c6314a..1df095c32 100644 --- a/plugins/logging.lua +++ b/plugins/logging.lua @@ -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 @@ -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 diff --git a/plugins/pac.lua b/plugins/pac.lua index a7ac31dc5..f89cd2c90 100644 --- a/plugins/pac.lua +++ b/plugins/pac.lua @@ -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 @@ -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 diff --git a/plugins/vendor/sh_plugin.lua b/plugins/vendor/sh_plugin.lua index 08f1f1250..115833fd4 100644 --- a/plugins/vendor/sh_plugin.lua +++ b/plugins/vendor/sh_plugin.lua @@ -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