sasaa86

fill hopper

Feb 13th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. function getItemsCount(target)
  2.     -- if no target specified break
  3.     if not target then
  4.         print("No target specified")
  5.         print("use top, bottom, front, back, left, right")
  6.         print("or use 'minecraft:hopper', 'minecraft:dropper', 'minecraft:chest' etc.")
  7.         print("example getItemCount(\"minecraft:dropper\")")
  8.         break
  9.     end
  10.    
  11.     -- determin as side has been given or a container to search for
  12.     if (target == "top") or (target == "bottom") or (target == "front") or (target == "back") or (target == "left") or (target == "right") then
  13.         local container = peripheral.wrap(target)
  14.     else
  15.         local container = peripheral.find(target)
  16.     end
  17.    
  18.     totalItems = 0
  19.     local slots = container.size()
  20.    
  21.     for slot=1, slots do
  22.         item = container.getItemDetail(slot)
  23.         if item ~= nil then
  24.             -- getting count
  25.             amount = item.count
  26.             totalItems = totalItems + amount
  27.         end
  28.    
  29.     -- result
  30.     return totalItems
  31. end
  32.  
  33. function getItemsMaxCount(target)
  34.     -- if no target specified break
  35.     if not target then
  36.         print("No target specified")
  37.         print("use top, bottom, front, back, left, right")
  38.         print("or use 'minecraft:hopper', 'minecraft:dropper', 'minecraft:chest' etc.")
  39.         print("example getItemsMaxCount(\"minecraft:dropper\")")
  40.         break
  41.     end
  42.    
  43.     -- determin as side has been given or a container to search for
  44.     if (target == "top") or (target == "bottom") or (target == "front") or (target == "back") or (target == "left") or (target == "right") then
  45.         local container = peripheral.wrap(target)
  46.     else
  47.         local container = peripheral.find(target)
  48.     end
  49.    
  50.     totalMaxItems = 0
  51.     local slots = container.size()
  52.    
  53.     for slot=1, slots do
  54.         item = container.getItemDetail(slot)
  55.         -- totalMaxItems
  56.         if item ~= nil then
  57.             -- getting maxCount
  58.             totalMaxItems = totalMaxItems + item.maxCount
  59.         else
  60.             -- assuming stackable to 64
  61.             totalMaxItems = totalMaxItems + 64
  62.         end
  63.    
  64.     -- result
  65.     return totalMaxItems
  66. end
  67.  
  68.  
  69. -- loop
  70. while true do
  71.     turtle.dig()
  72.     term.clear()
  73.     term.setCursorPos(1, 1)
  74.     print("container: "..getItemsInHopper().."/"..getMaxItemsInHopper())
  75.     os.sleep(0.2)
  76.    
  77.     -- wait if hopper is full and inventory has atleast 16 items
  78.     if (getItemsCount() == getItemsMaxCount() and turtle.getItemCount() >= 16) then
  79.         term.clear()
  80.         term.setCursorPos(1, 1)
  81.         print("container: "..getItemsInHopper().."/"..getMaxItemsInHopper().." -> +16 -> stopped minning")
  82.         -- while getItemsInHopper() == getMaxItemsInHopper() do
  83.         while turtle.getItemCount() >= 16 do
  84.             os.sleep(1)
  85.         end
  86.     end
  87. end
Add Comment
Please, Sign In to add comment