Advertisement
melzneni

Inventory search lib

Sep 13th, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. local api = {}
  2.  
  3. function api.getStorageCounts(itemCounts)
  4. local storageCounts = {}
  5. for key, _ in pairs(toLuaTable(itemCounts)) do
  6. storageCounts[key] = 0
  7. end
  8. local storage = peripheral.wrap("bottom")
  9. for _, item in pairs(storage.list()) do
  10. if storageCounts[item.name] then
  11. storageCounts[item.name] = storageCounts[item.name] + item.count
  12. end
  13. end
  14. return toTurtiTable(storageCounts)
  15. end
  16.  
  17. function api.pullCraftingItems(config, targetCount)
  18. config = toLuaTable(config)
  19.  
  20. local pulled = {}
  21. for key, _ in pairs(config) do
  22. pulled[key] = 0
  23. end
  24.  
  25. local storage = peripheral.wrap("bottom")
  26. local chest = peripheral.wrap("left")
  27. for sourceSlot, item in pairs(storage.list()) do
  28. for targetSlot, name in pairs(config) do
  29. if item.name == name and pulled[targetSlot] < targetCount then
  30. local toPull = targetCount - pulled[targetSlot]
  31. --storage.pullItems(peripheral.getName(storage), )
  32. end
  33. end
  34. end
  35.  
  36.  
  37. end
  38.  
  39. function api.printItemCounts()
  40. local counts = {}
  41. local itemNames = {}
  42. local foundOne = false
  43. for i = 1, 16 do
  44. local detail = turtle.getItemDetail(i, true)
  45. if detail ~= nil then
  46. if counts[detail.name] == nil then
  47. counts[detail.name] = detail.count
  48. else
  49. counts[detail.name] = counts[detail.name] + detail.count
  50. end
  51.  
  52. itemNames[detail.name] = detail.displayName
  53. foundOne = true
  54. end
  55. end
  56. if not foundOne then
  57. return
  58. end
  59.  
  60. local storage = peripheral.wrap("bottom")
  61. for _, item in pairs(storage.list()) do
  62. if counts[item.name] then
  63. counts[item.name] = counts[item.name] + item.count
  64. end
  65. end
  66.  
  67. local c = 0
  68. term.clear()
  69. term.setCursorPos(1, 1)
  70. for name, count in pairs(counts) do
  71. if c == 12 then
  72. c = 0
  73. term.setTextColor(256)
  74. io.write("Press Enter to continue")
  75. term.setTextColor(1)
  76. io.read()
  77. term.clear()
  78. term.setCursorPos(1, 1)
  79. end
  80. io.write(itemNames[name] .. " ")
  81. term.setTextColor(8192)
  82. io.write(tostring(count))
  83. term.setTextColor(1)
  84. print("")
  85. c = c + 1
  86. end
  87. term.setTextColor(256)
  88. term.setCursorPos(1, 13)
  89. io.write("Press Enter to continue")
  90. term.setTextColor(1)
  91. io.read()
  92. end
  93.  
  94. return {
  95. name = "searchLib",
  96. api = api
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement