fatboychummy

BulkCobblestone

Sep 15th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.83 KB | None | 0 0
  1. --[[
  2. Made by fatmanchummy
  3. (pastebin.com/u/fatboychummy)
  4. ]]
  5.  
  6.  
  7. --Variable declarations
  8.  
  9. local myTurtleName = ... -- call this program with the turtle's name!
  10.  
  11. local chests = {}
  12. local turtles = {}
  13.  
  14. local pc = ""
  15. local other = ""
  16. local cobbleCount = 0
  17. local maxCobble = 0
  18. local mon = peripheral.find("monitor")
  19. assert(mon,"No monitor found")
  20. mon.setTextScale(0.5)
  21. local mX,mY = mon.getSize()
  22. local itm = 0
  23. local max = 0
  24. local alternate
  25. local shulkerLoaded = false
  26.  
  27. local function refreshMon()
  28.   mX,mY = mon.getSize()
  29. end
  30.  
  31. term.clear()
  32. local function writeAt(x,y,wr,fg,bg)
  33.   fg = fg or colors.black
  34.   bg = bg or colors.white
  35.   local clr = string.rep(" ",mX)
  36.   term.setCursorPos(1,y)
  37.   term.write(clr)
  38.   term.setCursorPos(x,y)
  39.   term.write(wr)
  40. end
  41.  
  42. local function checkSingleTurtle(x)
  43.   local a = peripheral.wrap(x)
  44.   return a.isOn()
  45. end
  46.  
  47. local function refreshTurtles()
  48.   local all = peripheral.getNames()
  49.   local new = {}
  50.  
  51.   for i = 1,#all do
  52.     local cur = all[i]
  53.     if cur:find("turtle") then
  54.       table.insert(new,cur)
  55.     elseif cur:find("computer") then
  56.       pc = cur
  57.     end
  58.   end
  59.  
  60.   turtles = {}
  61.  
  62.   for i = 1,#new do
  63.     local k = new[i]
  64.     turtles[i] = k
  65.   end
  66. end
  67. --[[
  68. local function checkTurtles()
  69.   for i = 1,#turtles do
  70.     tStatus[ turtles[i] ] = checkSingleTurtle( turtles[i] )
  71.   end
  72. end]]
  73.  
  74. local function rebootTurtle(x)
  75.   if type(x) == "string" then
  76.     x = peripheral.wrap(x)
  77.     pcall(x.turnOn)
  78.   end
  79. end
  80.  
  81. local function refreshChests()
  82.   local all = peripheral.getNames()
  83.   local new = {}
  84.  
  85.   for k,v in pairs(all) do
  86.     if v:find("chest") or v:find("cache") then
  87.       table.insert(new,v)
  88.     end
  89.   end
  90.   chests = {}
  91.   for i = 1,#new do
  92.     chests[i] = new[i]
  93.   end
  94. end
  95.  
  96. local function refreshItems()
  97.   refreshChests()
  98.   cobbleCount = 0
  99.   maxCobble = 0
  100.   for i = 1,#chests do
  101.     local cChest = peripheral.wrap(chests[i])
  102.     local items = cChest.list()
  103.     if chests[i]:find("cache") then
  104.       maxCobble = maxCobble + 20000
  105.       if items[1] then
  106.         cobbleCount = cobbleCount + items[1].count
  107.       end
  108.     else
  109.       local sz = cChest.size()
  110.       maxCobble = maxCobble+(sz*64)
  111.       local chestEmptyCount = 0
  112.       for o = 1,sz do
  113.         if items[o] then
  114.           if items[o].name == "minecraft:cobblestone" and items[o].damage == 0 then
  115.             cobbleCount = cobbleCount + items[o].count
  116.           end
  117.         end
  118.       end
  119.     end
  120.   end
  121. end
  122.  
  123. local function grabCobble(to,amot,last)
  124.   local grabbed = 0
  125.   last = last or 1
  126.   local total = 0
  127.   for i = last,#chests do
  128.     local cur = peripheral.wrap(chests[i])
  129.     if cur then
  130.       for o = 1,cur.size() do
  131.         local current = cur.pushItems(to,o,64-total)
  132.         total = total + current
  133.         if current > 0 then
  134.           last = i
  135.           os.sleep(0.2)
  136.           if total >= 64 then
  137.             grabbed = grabbed + 1
  138.             if grabbed >= amot then
  139.               writeAt(1,5,"Got all items.")
  140.               return true,grabbed,i
  141.             end
  142.             total = 0
  143.           end
  144.         end
  145.       end
  146.     end
  147.   end
  148.   writeAt(1,5,"Failed to grab items " .. tostring(#chests) .. "(" .. tostring(grabbed) .. ")")
  149.   return false,grabbed
  150. end
  151.  
  152. local function drawBox(x1,y1,x2,y2,color,outline_only)
  153.   if type(color) == "number" then mon.setBackgroundColor(color) end
  154.   local wall = string.rep(" ",x2-x1)
  155.   if outline_only then
  156.     mon.setCursorPos(x1,y1)
  157.     mon.write(wall)
  158.     mon.setCursorPos(x1,y2)
  159.     mon.write(wall)
  160.     for i = y1,y2 do
  161.       mon.setCursorPos(x1,i)
  162.       mon.write(" ")
  163.     end
  164.     for i = y1,y2 do
  165.       mon.setCursorPos(x2,i)
  166.       mon.write(" ")
  167.     end
  168.   else
  169.     for i = y1,y2 do
  170.       mon.setCursorPos(x1,i)
  171.       mon.write(wall)
  172.     end
  173.   end
  174. end
  175.  
  176.  
  177.  
  178. local function drawBG()
  179.   refreshMon()
  180.   mon.setBackgroundColor(colors.yellow)
  181.   mon.clear()
  182. end
  183.  
  184.  
  185. local state = "no_shulker"
  186.  
  187. local states = {
  188.   right = {
  189.     no_shulker = function()
  190.       drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
  191.       drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.gray,false)
  192.       drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
  193.       drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,colors.red,false)
  194.     end,
  195.     shulker_percent = function(itm,mxitm)
  196.       drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
  197.       drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.gray,false)
  198.       local topHeight = math.floor(mY-(itm/mxitm*12)) --a number out of 12
  199.       drawBox(math.ceil(mX*(2/3))+2,topHeight,mX,mY-1,colors.green,false)
  200.       drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
  201.       drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,colors.green,false)
  202.     end,
  203.     wrong_inventory = function()
  204.       drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
  205.       drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.gray,false)
  206.       drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
  207.       if alternate ~= colors.orange then
  208.         alternate = colors.orange
  209.       else
  210.         alternate = colors.red
  211.       end
  212.       drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,alternate,false)
  213.     end,
  214.     error = function()
  215.       drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
  216.       drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.red,false)
  217.       drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
  218.       drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,colors.orange,false)
  219.     end
  220.   },
  221. }
  222.  
  223. local function refreshSingleInventory(inv)
  224.   local total = 0
  225.   if inv:find("cache") then
  226.     local inv = peripheral.wrap(inv)
  227.     local itm = inv.list()
  228.     if itm[1] then
  229.       return itm[1].count, 20000
  230.     end
  231.     return 0,0
  232.   else
  233.     local inv = peripheral.wrap(inv)
  234.     if inv then
  235.       local itms = inv.list()
  236.       for i = 1,inv.size() do
  237.         if itms[i] then
  238.           total = total + itms[i].count
  239.         end
  240.       end
  241.       return total,inv.size()*64
  242.     end
  243.     return 0,0
  244.   end
  245. end
  246.  
  247.  
  248.  
  249. local function eventHandler()
  250.   while true do
  251.     local ev = {os.pullEvent()}
  252.     local eventLine = ""
  253.     for i = 1,#ev do
  254.       eventLine = eventLine.." "..tostring(ev[i])
  255.     end
  256.     local event = ev[1]
  257.     if event == "peripheral" then
  258.       other = ev[2]
  259.       if other:find("shulker") then
  260.         state = "shulker_percent"
  261.         shulkerLoaded = true
  262.       else
  263.         state = "wrong_inventory"
  264.         shulkerLoaded = false
  265.       end
  266.     elseif event == "peripheral_detach" then
  267.       other = ""
  268.       state = "no_shulker"
  269.       shulkerLoaded = false
  270.     end
  271.   end
  272. end
  273.  
  274. local function inBetween(x1,y1,x2,y2,x,y)
  275.   return x1 <= x and x2 >= x and y1 <= y and y2 >= y
  276. end
  277.  
  278. local function dispense()
  279.   grabCobble(myTurtleName,1)
  280.   turtle.dropUp()
  281. end
  282.  
  283. local function voidInventory()
  284.   for i = 1,16 do
  285.     turtle.select(i)
  286.     turtle.drop()
  287.   end
  288.   turtle.select(1)
  289. end
  290. voidInventory()
  291.  
  292. local function fillShulker()
  293.   writeAt(1,2,"Filling a shulker box.")
  294.   local spaces = math.ceil(itm/64)
  295.   local maxSpaces = math.ceil(max/64)
  296.   local spLeft = maxSpaces - spaces
  297.   local shulk = peripheral.wrap(other)
  298.   local last = 1
  299.   while spLeft > 0 do
  300.     local toGrab = spLeft
  301.     if toGrab > 16 then
  302.       toGrab = 16
  303.     end
  304.     local isGrabbed,grabbed,lst = grabCobble(myTurtleName,toGrab,last)
  305.     last = lst
  306.     shulk = peripheral.wrap(other)
  307.     if isGrabbed or grabbed > 0 then
  308.       if shulk then
  309.         for i = 1,grabbed do
  310.           if shulk.pullItems(myTurtleName,i,64) == 0 then
  311.             shulk = peripheral.wrap(other)
  312.           end
  313.         end
  314.       else
  315.         writeAt(1,6,"Shulker box is gone, voiding inventory.")
  316.         voidInventory()
  317.         writeAt(1,6,"")
  318.         writeAt(1,2,"")
  319.         return
  320.       end
  321.       spLeft = spLeft - grabbed
  322.     else
  323.       voidInventory()
  324.       writeAt(1,2,"")
  325.       return
  326.     end
  327.   end
  328.   writeAt(1,2,"")
  329. end
  330.  
  331. local function dropHandler()
  332.   while true do
  333.     local event,monitor,x,y = os.pullEvent("monitor_touch")
  334.     local posX = math.floor(mX/3)-4
  335.     local posY = math.floor(mY/2)
  336.     if inBetween(posX,posY,posX+9,posY+2,x,y) then
  337.       if shulkerLoaded then
  338.         fillShulker()
  339.         voidInventory()
  340.       else
  341.         dispense(64)
  342.       end
  343.     end
  344.   end
  345. end
  346.  
  347.  
  348. local function drawCobbleCount() --also turtle status.
  349.   while true do
  350.     --TURTLE STATUS
  351.     local tsposx = 2
  352.     local tsposy = 5
  353.     local lines = math.ceil(#turtles/5)+tsposy+5
  354.     local width = tsposx+11
  355.     drawBox(tsposx,tsposy,width,lines,colors.black,false)
  356.     mon.setTextColor(colors.white)
  357.     mon.setCursorPos(tsposx+2,5) --"STATUS"
  358.     mon.write("STATUS:")
  359.     for i = 1,#turtles do
  360.       mon.setCursorPos(tsposx+(((i-1)%5)*2)+1,tsposy+2+(math.ceil(i/5)-1)*2)
  361.       local c1 = "0"
  362.       local c2 = "e"
  363.       if checkSingleTurtle(turtles[i]) then
  364.         c2 = "d"
  365.       end
  366.       mon.blit(tostring(i),c1,c2)
  367.     end
  368.     if pc ~= "" then
  369.       mon.setCursorPos(tsposx+2,lines-1)
  370.       local c1 = "0000000"
  371.       local c2 = "eeeeeee"
  372.       if checkSingleTurtle(pc) then
  373.         c2 = "ddddddd"
  374.       end
  375.       mon.blit("CONTROL",c1,c2)
  376.     end
  377.     --cobblecount
  378.     local ok,err = pcall(refreshItems)
  379.     if ok then
  380.       drawBox(math.floor((mX*(2/3)))+1,1,mX,math.floor(mY*(2/3))-1,colors.black,true)
  381.       drawBox(math.ceil((mX*(2/3)))+2,2,mX,math.floor(mY*(2/3))-2,colors.lightGray,false)
  382.       --cobbleCount,maxCobble, OUT OF 22
  383.       local clrHeight = math.floor(math.floor(mY*(2/3))-1-cobbleCount/maxCobble * 22)
  384.       local color = colors.red
  385.       if clrHeight == 2 then
  386.         color = colors.blue
  387.       elseif clrHeight < 7 then
  388.         color = colors.green
  389.       elseif clrHeight < 16 then
  390.         color = colors.orange
  391.       end
  392.       drawBox(math.ceil((mX*(2/3)))+2,clrHeight,mX,math.floor(mY*(2/3))-2,color,false)
  393.       local a = tostring(cobbleCount)
  394.       local b = tostring(maxCobble)
  395.       local longest = b:len()
  396.       if a:len() > longest then
  397.         longest = a:len()
  398.       end
  399.       mon.setCursorPos(mX-6-a:len(),12)
  400.       mon.setTextColor(colors.black)
  401.       mon.write(a)
  402.       mon.setCursorPos(mX-6-longest,13)
  403.       mon.write(string.rep("-",longest))
  404.       mon.setCursorPos(mX-6-b:len(),14)
  405.       mon.write(b)
  406.       writeAt(1,4,"Item count read successfully.")
  407.     else
  408.       writeAt(1,4,"Error reading item count")
  409.     end
  410.  
  411.     if not checkSingleTurtle(pc) then
  412.       rebootTurtle(pc)
  413.     end
  414.  
  415.     sleep(10)
  416.   end
  417.  
  418. end
  419.  
  420.  
  421.  
  422.  
  423. local function peripheralWatch()
  424.   while true do
  425.     local ok
  426.     local bad = false
  427.     if other then
  428.       if state == "shulker_percent" then
  429.         bad = false
  430.         ok,itm,max = pcall(refreshSingleInventory,other)
  431.         if ok then
  432.           states.right[state](itm,max)
  433.         else
  434.           states.right.error()
  435.         end
  436.       else
  437.         if state == "wrong_inventory" then
  438.           writeAt(1,3,"Wrong inventory connected.")
  439.           bad = true
  440.         else
  441.           writeAt(1,3,"")
  442.           bad = false
  443.         end
  444.         states.right[state]()
  445.         if not bad then
  446.           os.sleep(1)
  447.         end
  448.       end
  449.     else
  450.       states.right.no_shulker()
  451.       os.sleep(1)
  452.     end
  453.     if not bad then
  454.       os.sleep(1)
  455.     else
  456.       os.sleep(0.5)
  457.     end
  458.   end
  459. end
  460.  
  461.  
  462. local function drawMain()
  463.   while true do
  464.     local function swrite(x,y,text)
  465.       mon.setCursorPos(x,y)
  466.       mon.write(text)
  467.     end
  468.     local posX = math.floor(mX/3)-4
  469.     local posY = math.floor(mY/2)
  470.     mon.setCursorPos(posX,posY)
  471.     mon.setBackgroundColor(colors.orange)
  472.     mon.setTextColor(colors.black)
  473.     mon.write("          ")
  474.     mon.setCursorPos(posX,posY+1)
  475.     mon.write(" DISPENSE ")
  476.     mon.setCursorPos(posX,posY+2)
  477.     mon.write("          ")
  478.     mon.setBackgroundColor(colors.yellow)
  479.  
  480.     local hw = 24
  481.     swrite(1,1,"Bulk Cobblestone Dispenser v2.0.0")
  482.     swrite(1,hw,"HOW TO USE:")
  483.     swrite(3,hw+1,"IF YOU HAVE A SHULKER BOX:")
  484.     swrite(5,hw+2,"1. Place shulker box on modem")
  485.     swrite(5,hw+3,"2. Press dispense")
  486.     swrite(5,hw+4,"The shulker box will be filled")
  487.     swrite(3,hw+6,"IF YOU DON'T HAVE A SHULKER BOX:")
  488.     swrite(5,hw+7,"1. Press dispense")
  489.     swrite(5,hw+8,"The turtle drops 64 cobblestone.")
  490.     mon.setTextColor(colors.red)
  491.     swrite(1,hw+11,"Note: Since chests do not hold items")
  492.     swrite(1,hw+12,"after being broken, they do not work.")
  493.     swrite(1,hw+13,"(Shulker boxes only)")
  494.     os.sleep(10)
  495.   end
  496. end
  497.  
  498. local function watchTurtles()
  499.   while true do --Bruh this function is bad, but basically it used to boot the turtles as well, now it only refreshes.
  500.     --for i = 1,3 do
  501.       refreshTurtles()
  502.       os.sleep(5)
  503.   --  end
  504.     --[[for i = 1,#turtles do
  505.       if not checkSingleTurtle(turtles[i]) then
  506.         print("Booting "..turtles[i])
  507.         rebootTurtle(turtles[i])
  508.       end
  509.     end
  510.     if not checkSingleTurtle(pc) then
  511.       print("Booting "..pc)
  512.       rebootTurtle(pc)
  513.     end]]
  514.     --os.sleep(5)
  515.   end
  516. end
  517.  
  518.  
  519.  
  520. refreshItems()
  521. drawBG()
  522.  
  523. writeAt(1,1,"Max: " .. tostring(maxCobble))
  524.  
  525. local function par()
  526.   parallel.waitForAny(
  527.     peripheralWatch,
  528.     eventHandler,
  529.     dropHandler,
  530.     drawCobbleCount,
  531.     drawMain,
  532.     watchTurtles
  533.   )
  534. end
  535.  
  536. local ok,err = pcall(par)
  537.  
  538. if not ok and not err:find("Terminated") then
  539.   writeAt(1,10,err)
  540.   mon.setBackgroundColor(colors.blue)
  541.   mon.clear()
  542.   mon.setTextColor(colors.black)
  543.   mon.setCursorPos(1,1)
  544.   mon.write("Crashed.")
  545.   mon.setCursorPos(1,2)
  546.   mon.write(err)
  547.   mon.setCursorPos(1,3)
  548.   mon.write("Auto reboot in 30 seconds.")
  549.   os.sleep(30)
  550.   os.reboot()
  551. end
Add Comment
Please, Sign In to add comment