Anonymous

Module:Sandbox/AuronTest: Difference between revisions

From Melvor Idle
m
Revert to previous revision
m (getItems: Allow returning IDs only)
m (Revert to previous revision)
Line 58: Line 58:


function p.getItemByID(ID)
function p.getItemByID(ID)
   return ItemData.Items[ID + 1]
   local result = Shared.clone(ItemData.Items[ID + 1])
  if result ~= nil then
    result.id = ID
  end
  return result
end
end


Line 68: Line 72:
   for i, item in pairs(ItemData.Items) do
   for i, item in pairs(ItemData.Items) do
     local itemName = string.gsub(item.name, '#', '')
     local itemName = string.gsub(item.name, '#', '')
     if (name == itemName) then return item end
     if(name == itemName) then
      result = Shared.clone(item)
      --Make sure every item has an id, and account for Lua being 1-index
      result.id = i - 1
      break
    end
   end
   end
  return result
end
end


function p.getItems(checkFunc, IDsOnly)
function p.getItems(checkFunc)
   local result = {}
   local result = {}
  local returnID = (IDsOnly == nil and false or IDsOnly)
   for i, item in pairs(ItemData.Items) do
   for i, item in pairs(ItemData.Items) do
     if checkFunc(item) then
     if checkFunc(item) then
       if returnID then
       local newItem = Shared.clone(item)
        table.insert(result, item.id)
      newItem.id = i - 1
       else
       table.insert(result, newItem)
        table.insert(result, item)
      end
     end
     end
   end
   end