Advertisement
fatboychummy

cobbleStoneFarmItemHandler

Aug 6th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. local chests = {}
  2. local cobbleCount = 0
  3. local maxCobble = 0
  4. local outputTurtle = "570"
  5. local turtles = {}
  6. local modem = peripheral.wrap("right")
  7. modem.open(1)
  8. local mX,mY = term.getSize()
  9.  
  10. term.clear()
  11. local function writeAt(x,y,wr,fg,bg)
  12.   fg = fg or colors.black
  13.   bg = bg or colors.white
  14.   local clr = string.rep(" ",mX)
  15.   term.setCursorPos(1,y)
  16.   term.write(clr)
  17.   term.setCursorPos(x,y)
  18.   term.write(wr)
  19. end
  20.  
  21. local function refreshTurtles()
  22.   turtles = {}
  23.   for k,v in pairs(peripheral.getNames()) do
  24.     if v:find("turtle") and not v:find(outputTurtle) then
  25.       table.insert(turtles,v)
  26.     end
  27.   end
  28. end
  29.  
  30. local function startTurtle(t)
  31.   t = peripheral.wrap(t)
  32.   if type(t) == "table" and type(t.turnOn) == "function" then
  33.     t.turnOn()
  34.   end
  35. end
  36.  
  37. local function refreshChests()
  38.   local all = peripheral.getNames()
  39.   chests = {}
  40.   for k,v in pairs(all) do
  41.     if v:find("chest") or v:find("cache") then
  42.       table.insert(chests,v)
  43.     end
  44.   end
  45. end
  46.  
  47. local function refreshItems()
  48.   refreshChests()
  49.   cobbleCount = 0
  50.   maxCobble = 0
  51.   for i = 1,#chests do
  52.     if chests[i]:find("cache") then
  53.       maxCobble = maxCobble + 20000
  54.       local cChest = peripheral.wrap(chests[i])
  55.       local itm = cChest.list()[1]
  56.       if itm then
  57.         cobbleCount = cobbleCount + itm.count
  58.       end
  59.     else
  60.       local cChest = peripheral.wrap(chests[i])
  61.       local items = cChest.list()
  62.       local sz = cChest.size()
  63.       maxCobble = maxCobble+(sz*64)
  64.       local chestEmptyCount = 0
  65.       for o = 1,sz do
  66.         if items[o] then
  67.           if items[o].name == "minecraft:cobblestone" and items[o].damage == 0 then
  68.             cobbleCount = cobbleCount + items[o].count
  69.           end
  70.         end
  71.       end
  72.     end
  73.   end
  74. end
  75.  
  76. local function wait(t)
  77.   for i = t,0,-1 do
  78.     writeAt(1,1,"Waiting " .. tostring(i) .. " second(s).")
  79.     os.sleep(1)
  80.   end
  81. end
  82.  
  83. local function main()
  84.   while true do
  85.     local lastEmptyChest = 1
  86.     writeAt(1,1,"Starting move cycle...")
  87.     refreshTurtles()
  88.     refreshItems()
  89.     writeAt(1,2,tostring(cobbleCount) .. "/" .. tostring(maxCobble))
  90.     if cobbleCount < maxCobble-256 then
  91.       for i = 1,#turtles do
  92.         startTurtle(turtles[i])
  93.         local cTurt = peripheral.wrap(turtles[i])
  94.         local items = cTurt.list()
  95.         for k,v in pairs(items) do
  96.           for o = lastEmptyChest,#chests do
  97.             writeAt(1,3,"Last empty chest: " .. tostring(chests[lastEmptyChest]))
  98.             writeAt(1,4,", index " .. tostring(lastEmptyChest))
  99.             if cTurt.pushItems(chests[o],k,64) > 0 then
  100.               break
  101.             else
  102.               lastEmptyChest = o+1
  103.             end
  104.           end
  105.         end
  106.         for i = 3,4 do
  107.           writeAt(1,i,"")
  108.         end
  109.       end
  110.  
  111.       writeAt(1,1,"End of move cycle...")
  112.       refreshItems()
  113.       writeAt(1,2,tostring(cobbleCount) .. "/" .. tostring(maxCobble))
  114.     else
  115.       writeAt(1,1,"Inventory full, no move.")
  116.       writeAt(1,3,"Shutting down turtles.")
  117.       modem.transmit(1,1,"shutdown")
  118.     end
  119.     wait(30)
  120.   end
  121. end
  122.  
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement