CC: Tweaked

CC: Tweaked

42M Downloads

Ore Dictionary Table Improvment on getItem().getMetadata()

Doug4347 opened this issue ยท 4 comments

commented

Currently when using a getMetadata() method, you get a table with a ores table as well. For example, you might use:

inv = --[[ Some inventory wrapping ]]--
md = inv.getItem(1).getMetadata -- will of course crash if there is no item in slot 1

The resulting md table will contain md.ores

However, md.ores is a table where the key is the oreDict name, and the value is always true (At least, I've never personally found one set to false)

For example, copper ore might result in:

md.ores = {
"oreCopper" = true
}

and no other information.

What I'd like to suggest is instead turning it into a numeric table that can be more easily looped through, such as:

md.ores = {
oreCopper,
oreIron,
oreOsmium
}

allowing users (such as myself, a basic user) to simply loop through the indexes and find if a specific oreDictionary value exists.

If it is at all possible for the current table to result in false as a value, perhaps a second table would be useful for users that don't want to check that value? Currently my code to get around this requires taking the table and creating a second table out of it, then taking a look at the values. Mostly because I'm looking for anything that contains "ore" and then doing something with it.

commented

You're correct than it always maps to true - the intent here is it makes it easy to see if an item has a specific tag (you can just do if item.tags[tagName] then).

commented

That code wouldn't allow me to check for multiple ores without multiple statements.

I could list every ore in the dictionary in a single if statement, but it would be far easier to just check if item.ore['ore*'] exists

As an example, the app I've made empties my inventory and sorts ores between what's smeltable and what's not. Iron Ore goes to the smelter, logs go directly to storage.

To have an if statement for each and every ore I want to smelt would be incredibly tedious, even if combined into a single if statement such as if item.ore["oreIron"] or item.ore["oreCopper"] or item.ore["oreTin"] or ....

It would be far easier to just have a single statement as if string.match("ore", item.ore[index]) then

At the moment I have to take that table, convert it to a numeric table with the original keys as the new values, then do the above statement in a loop which makes the system less efficient when it has to convert a table for every item in my inventory, or worse, a diamond chest.

commented

You shouldn't need to convert to a separate loop just to loop through it again though - the following will also work:

for tag in pairs(item.tags) do
  if string.match("ore", tag) then
    ..
  end
end
commented

Ah. I'm dumb. Thank you very much, I'll be using that snippet from now on.