Given a table of structure:
local tbl = { [3] = { count = 32, name = "minecraft:cobblestone", damage = 0, },[7] = { count = 32, name = "minecraft:stone", damage = 0, },[10] = { count = 13, name = "minecraft:cobblestone", damage = 0, }, }
You could do something like this:
local lookupTbl = {} for k,v in pairs(tbl) do --# look through each index in the table. k will be the key, v will be the value if not lookupTbl[v.name] then --# check to see if we have information about the item in our lookup table already lookupTbl[v.name] = {} --# we haven't seen the item before so initialize the table to hold the item end table.insert(lookupTbl[v.name],{slot=k,amount=v.count} --# insert the slot and amount information into the table for fast lookup end
This would build a table that looks something like this:
{ [minecraft:cobblestone] = { { slot = 3, amount = 32 }, { slot = 10, amount = 13 } }, ["minecraft:stone"] = { slot = 7, amount = 32 } }
Using this new table you can lookup all locations of each item instantly. It then becomes a problem of looping through all of the slots to figure out how much you have.
print(lookupTbl["minecraft:cobblestone"][1].slot) --# prints "3" print(lookupTbl["minecraft:cobblestone"][1].amount) --# prints "32"