Added Util.GetItemGroup and Util.RegisterItemGroup

This commit is contained in:
EvilFactory
2023-05-26 19:56:17 -03:00
parent be178715a8
commit abb9e1eb1d
2 changed files with 52 additions and 0 deletions

View File

@@ -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)

View File

@@ -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