DaikiKaminari

lib/inventory

Jul 14th, 2020 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. -- return true if two items are identical
  2. function isItemIdentical(item1, item2, ignoreQty)
  3.     if not item1 then
  4.         error("First item cannot be nil.")
  5.     end
  6.     if not item2 then
  7.         error("Second item cannot be nil")
  8.     end
  9.     return compareItems(item1, item2, ignoreQty) and compareItems(item2, item1, ignoreQty)
  10. end
  11.  
  12. -- compare recursively two items
  13. function compareItems(item1, item2, ignoreQty)
  14.     for k,v in pairs(item1) do
  15.         if type(v) ~= type(item2[k]) then
  16.             return false
  17.         end
  18.         if type(v) == "table" then
  19.           if not compareItems(v, item2[k], ignoreQty) then
  20.             return false
  21.           end
  22.         else
  23.           if (not ignoreQty and v ~= item2[k]) or (k ~= "qty" and v ~= item2[k]) then
  24.             return false
  25.           end
  26.         end
  27.     end
  28.     return true
  29. end
  30.  
  31. -- check if an item is present in a list of items
  32. function isItemInList(item1, itemList, ignoreQty)
  33.     for k,item2 in pairs(itemList) do
  34.         if isItemIdentical(item1, item2, ignoreQty) then
  35.             return k
  36.         end
  37.     end
  38.     return false
  39. end
  40.  
  41. -- compare two list of items recursively
  42. function isItemListIdentical(items1, items2, ignoreQty)
  43.     for _,item1 in pairs(items1) do
  44.         if not isItemInList(item1, items2, ignoreQty) then
  45.             return false
  46.         end
  47.     end
  48.     for _,item2 in pairs(items2) do
  49.         if not isItemInList(item2, items1, ignoreQty) then
  50.             return false
  51.         end
  52.     end
  53.     return true
  54. end
  55.  
  56. -- returns true if the container contains an item, if no item specified then check for any item
  57. function containsItem(container, item)
  58.     if container == nil then
  59.         error("Container peripheral is nil")
  60.     end
  61.     for i=1,container.getInventorySize() do
  62.         local item2 = container.getStackInSlot(i)
  63.         if (item and isItemIdentical(item, item2, false)) or (not item and item2) then
  64.             return true
  65.         end
  66.     end
  67.     return false
  68. end
  69.  
  70. -- move all items from a container to another
  71. function moveAllItems(container, outputContainerSide, delay)
  72.     if container == nil then
  73.         error("Container peripheral is nil")
  74.     end
  75.     for i=1,container.getInventorySize() do
  76.         container.pushItem(outputContainerSide, i)
  77.         if delay ~= nil then
  78.             sleep(delay)
  79.         end
  80.     end
  81. end
Add Comment
Please, Sign In to add comment