Advertisement
LegoTurtle

Untitled

Jul 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. local output = peripheral.wrap('minecraft:chest_3')
  2. local input = peripheral.wrap('minecraft:chest_1')
  3.  
  4. local stock = {}
  5.  
  6. local function checkStock()
  7. local items = input.list()
  8. for slot, item in pairs(items) do
  9. if not stock[item.name] then
  10. stock[item.name] = {}
  11. stock[item.name]['slots'] = {}
  12. end
  13. table.insert(stock[item.name]['slots'], {
  14. ['slot'] = slot,
  15. ['count'] = item.count
  16. })
  17. end
  18.  
  19. for item, slots in pairs(stock) do
  20. local total = 0
  21. for _,slot in pairs(slots['slots']) do
  22. total = total + slot.count
  23. end
  24. stock[item]['stock'] = total
  25. end
  26. end
  27.  
  28. local function getSlots(item)
  29. local counts = {}
  30. for _, v in pairs(stock[item]['slots']) do
  31. table.insert(counts,v.count)
  32. end
  33. table.sort(counts)
  34. local new = {}
  35. for _, v in pairs(stock[item]['slots']) do
  36. for i, b in pairs(counts) do
  37. if b == v.count then
  38. new[i] = {
  39. ['count'] = b,
  40. ['slot'] = v.slot
  41. }
  42. end
  43. end
  44. end
  45. return new
  46. end
  47.  
  48. local function getItems(item,amount)
  49. if stock[item] and stock[item]['stock'] >= amount then
  50. print('yay')
  51. local remaining = amount
  52. repeat
  53. print(remaining)
  54. local slot = getSlots(item)[1]
  55. local amountToTake = math.min(slot.count, remaining)
  56. if amountToTake == slot.count then
  57. slot = nil
  58. else
  59. slot = {
  60. ['count'] = slot.count - amountToTake,
  61. ['slot'] = slot.slot
  62. }
  63. input.pushItems('minecraft:chest_3',slot.slot, amountToTake)
  64. remaining = remaining - amountToTake
  65. if remaining%32 == 0 then
  66. output.drop(1)
  67. end
  68. sleep(.1)
  69. until remaining == 0
  70. output.drop(1)
  71. else
  72. print('aw')
  73. end
  74. end
  75.  
  76. local function printStock()
  77. for item, slots in pairs(stock) do
  78. print(stock[item]['stock']..' x '..item.. ' in stock')
  79. end
  80. end
  81. updateStock()
  82. --print(textutils.serialize(stock))
  83. printStock()
  84. --print(textutils.serialize(getSlots('minecraft:cooked_beef')))
  85. getItems('minecraft:cooked_beef',65)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement