rkinasz

itemUtils.lua

Feb 7th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. local delay = 0.5
  2.  
  3. --finds item in chest and returns the slot
  4. function findItem(container, item)
  5.   for k,v in pairs(container.list()) do
  6.     if v["name"] == item then
  7.       return k, v
  8.     end
  9.   end
  10. end
  11.  
  12. --generic push item function
  13. function pushItems(container1, container2, slot, amount)
  14.   while amount > 0 do
  15.     amount = amount - container1.pushItems(peripheral.getName(container2), slot, amount)
  16.     if amount > 0 then
  17.       os.sleep(delay)
  18.     end
  19.   end
  20. end
  21.  
  22. --pushes an item from multiple slots
  23. function pushMultipleItems (container1, container2, item, amount)
  24.   while amount > 0 do
  25.     for k,v in pairs(container1.list()) do
  26.       if v.name == item then
  27.         if v.count >= amount then
  28.           pushItems(container1, container2, k, amount)
  29.           amount = 0
  30.         else
  31.           pushItems(container1, container2, k, v.count)
  32.           amount = amount - v.count
  33.         end
  34.       end
  35.       if amount <= 0 then
  36.         break;
  37.       end
  38.     end
  39.   end
  40. end
  41.  
  42. --generic pull item function
  43. function pullItems(container1, container2, slot, amount, slot2)
  44.   while amount > 0 do
  45.     amount = amount - container1.pullItems(peripheral.getName(container2), slot, amount, slot2)
  46.     if amount > 0 then
  47.       os.sleep(delay)
  48.     end
  49.   end
  50. end
  51.  
  52. --pulls an item from multiple slots
  53. function pullMultipleItems (container1, container2, item, amount)
  54.   while amount > 0 do
  55.     for k,v in pairs(container1.list()) do
  56.       if v.name == item then
  57.         if v.count >= amount then
  58.           pullItems(container1, container2, k, amount)
  59.           amount = 0
  60.         else
  61.           pullItems(container1, container2, k, v.count)
  62.           amount = amount - v.count
  63.         end
  64.       end
  65.       if amount <= 0 then
  66.         break;
  67.       end
  68.     end
  69.   end
  70. end
  71.  
  72. function hasTag (item, tag)
  73.   if item.tags[tag] then
  74.     return true
  75.   else
  76.     return false
  77.   end
  78. end
  79.  
  80. --returns the total amount of a certain item in the inventory
  81. function getTotalItemCount (container, item)
  82.   local slots = {}
  83.   local amount = 0
  84.   for k,v in pairs(container.list()) do
  85.     if v.name == item then
  86.       amount = amount + v.count
  87.       table.insert(slots, {slot = k, amount = v.count})
  88.     end
  89.   end
  90.   return amount, slots
  91. end
  92.  
Add Comment
Please, Sign In to add comment