From abb9e1eb1d1758ff3919307ca330e2dc772eb836 Mon Sep 17 00:00:00 2001 From: EvilFactory Date: Fri, 26 May 2023 19:56:17 -0300 Subject: [PATCH] Added Util.GetItemGroup and Util.RegisterItemGroup --- .../Lua/DefaultLib/Utils/Util.lua | 44 +++++++++++++++++++ luacs-docs/lua/lua/Util.lua | 8 ++++ 2 files changed, 52 insertions(+) diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua index ed6f89dbf..d4372ef9e 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua @@ -1,8 +1,15 @@ Util = {} local itemDictionary = {} +local itemGroups = {} local function AddItem(item) + for _, itemGroup in pairs(itemGroups) do + if itemGroup.func(item) then + table.insert(itemGroup.items, item) + end + end + local id = item.Prefab.Identifier.Value if itemDictionary[id] == nil then itemDictionary[id] = {} @@ -17,12 +24,49 @@ end) Hook.Add("roundEnd", "luaSetup.util.itemDictionary", function (item) itemDictionary = {} + for _, itemGroup in pairs(itemGroups) do + itemGroup.items = {} + end end) for _, item in pairs(Item.ItemList) do AddItem(item) end +Util.RegisterItemGroup = function(groupName, func) + if type(groupName) ~= "string" then + error(string.format("bad argument #1 to 'RegisterItemGroup' (string expected, got %s)", type(groupName)), 2) + end + + if type(func) ~= "function" then + error(string.format("bad argument #2 to 'RegisterItemGroup' (function expected, got %s)", type(func)), 2) + end + + local items = {} + for _, item in pairs(Item.ItemList) do + if func(item) then + table.insert(items, item) + end + end + + itemGroups[groupName] = { + func = func, + items = items + } +end + +Util.GetItemGroup = function(groupName) + if type(groupName) ~= "string" then + error(string.format("bad argument #1 to 'GetItemGroup' (string expected, got %s)", type(groupName)), 2) + end + + if not itemGroups[groupName] then + error("bad argument #1 to 'GetItemGroup' couldn't find the specified groupName", 2) + end + + return itemGroups[groupName].items or {} +end + Util.GetItemsById = function(id) if id == nil then error(string.format("bad argument #1 to 'GetItemsById' (string expected, got %s)", type(id)), 2) diff --git a/luacs-docs/lua/lua/Util.lua b/luacs-docs/lua/lua/Util.lua index 6cac85e40..ca4ca7e16 100644 --- a/luacs-docs/lua/lua/Util.lua +++ b/luacs-docs/lua/lua/Util.lua @@ -12,6 +12,14 @@ local Util = {} -- @realm shared Util.GetItemsById = function (id) end +--- Registers a new item group, the given function will be called for every item and should return true if the item should be in the group. +-- @realm shared +Util.RegisterItemGroup = function(groupName, func) end + +--- Returns a table with all items associated with the item group. +-- @realm shared +Util.GetItemGroup = function(groupName) end + --- Returns the Client that is currently controlling the Character, returns nil if no Client is controlling the Character. -- @realm shared Util.FindClientCharacter = function (character) end \ No newline at end of file