Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Made by fatmanchummy
- (pastebin.com/u/fatboychummy)
- ]]
- --Variable declarations
- local myTurtleName = ... -- call this program with the turtle's name!
- local chests = {}
- local turtles = {}
- local pc = ""
- local other = ""
- local cobbleCount = 0
- local maxCobble = 0
- local mon = peripheral.find("monitor")
- assert(mon,"No monitor found")
- mon.setTextScale(0.5)
- local mX,mY = mon.getSize()
- local itm = 0
- local max = 0
- local alternate
- local shulkerLoaded = false
- local function refreshMon()
- mX,mY = mon.getSize()
- end
- term.clear()
- local function writeAt(x,y,wr,fg,bg)
- fg = fg or colors.black
- bg = bg or colors.white
- local clr = string.rep(" ",mX)
- term.setCursorPos(1,y)
- term.write(clr)
- term.setCursorPos(x,y)
- term.write(wr)
- end
- local function checkSingleTurtle(x)
- local a = peripheral.wrap(x)
- return a.isOn()
- end
- local function refreshTurtles()
- local all = peripheral.getNames()
- local new = {}
- for i = 1,#all do
- local cur = all[i]
- if cur:find("turtle") then
- table.insert(new,cur)
- elseif cur:find("computer") then
- pc = cur
- end
- end
- turtles = {}
- for i = 1,#new do
- local k = new[i]
- turtles[i] = k
- end
- end
- --[[
- local function checkTurtles()
- for i = 1,#turtles do
- tStatus[ turtles[i] ] = checkSingleTurtle( turtles[i] )
- end
- end]]
- local function rebootTurtle(x)
- if type(x) == "string" then
- x = peripheral.wrap(x)
- pcall(x.turnOn)
- end
- end
- local function refreshChests()
- local all = peripheral.getNames()
- local new = {}
- for k,v in pairs(all) do
- if v:find("chest") or v:find("cache") then
- table.insert(new,v)
- end
- end
- chests = {}
- for i = 1,#new do
- chests[i] = new[i]
- end
- end
- local function refreshItems()
- refreshChests()
- cobbleCount = 0
- maxCobble = 0
- for i = 1,#chests do
- local cChest = peripheral.wrap(chests[i])
- local items = cChest.list()
- if chests[i]:find("cache") then
- maxCobble = maxCobble + 20000
- if items[1] then
- cobbleCount = cobbleCount + items[1].count
- end
- else
- local sz = cChest.size()
- maxCobble = maxCobble+(sz*64)
- local chestEmptyCount = 0
- for o = 1,sz do
- if items[o] then
- if items[o].name == "minecraft:cobblestone" and items[o].damage == 0 then
- cobbleCount = cobbleCount + items[o].count
- end
- end
- end
- end
- end
- end
- local function grabCobble(to,amot,last)
- local grabbed = 0
- last = last or 1
- local total = 0
- for i = last,#chests do
- local cur = peripheral.wrap(chests[i])
- if cur then
- for o = 1,cur.size() do
- local current = cur.pushItems(to,o,64-total)
- total = total + current
- if current > 0 then
- last = i
- os.sleep(0.2)
- if total >= 64 then
- grabbed = grabbed + 1
- if grabbed >= amot then
- writeAt(1,5,"Got all items.")
- return true,grabbed,i
- end
- total = 0
- end
- end
- end
- end
- end
- writeAt(1,5,"Failed to grab items " .. tostring(#chests) .. "(" .. tostring(grabbed) .. ")")
- return false,grabbed
- end
- local function drawBox(x1,y1,x2,y2,color,outline_only)
- if type(color) == "number" then mon.setBackgroundColor(color) end
- local wall = string.rep(" ",x2-x1)
- if outline_only then
- mon.setCursorPos(x1,y1)
- mon.write(wall)
- mon.setCursorPos(x1,y2)
- mon.write(wall)
- for i = y1,y2 do
- mon.setCursorPos(x1,i)
- mon.write(" ")
- end
- for i = y1,y2 do
- mon.setCursorPos(x2,i)
- mon.write(" ")
- end
- else
- for i = y1,y2 do
- mon.setCursorPos(x1,i)
- mon.write(wall)
- end
- end
- end
- local function drawBG()
- refreshMon()
- mon.setBackgroundColor(colors.yellow)
- mon.clear()
- end
- local state = "no_shulker"
- local states = {
- right = {
- no_shulker = function()
- drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.gray,false)
- drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,colors.red,false)
- end,
- shulker_percent = function(itm,mxitm)
- drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.gray,false)
- local topHeight = math.floor(mY-(itm/mxitm*12)) --a number out of 12
- drawBox(math.ceil(mX*(2/3))+2,topHeight,mX,mY-1,colors.green,false)
- drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,colors.green,false)
- end,
- wrong_inventory = function()
- drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.gray,false)
- drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
- if alternate ~= colors.orange then
- alternate = colors.orange
- else
- alternate = colors.red
- end
- drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,alternate,false)
- end,
- error = function()
- drawBox(math.ceil(mX*(2/3))+1,math.floor(mY*(2/3)),mX,mY,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+2,math.floor(mY*(2/3))+1,mX,mY-1,colors.red,false)
- drawBox(math.ceil(mX*(2/3))+7,math.floor(mY*(2/3))+5,mX-6,mY-5,colors.black,true)
- drawBox(math.ceil(mX*(2/3))+8,math.floor(mY*(2/3))+6,mX-6,mY-6,colors.orange,false)
- end
- },
- }
- local function refreshSingleInventory(inv)
- local total = 0
- if inv:find("cache") then
- local inv = peripheral.wrap(inv)
- local itm = inv.list()
- if itm[1] then
- return itm[1].count, 20000
- end
- return 0,0
- else
- local inv = peripheral.wrap(inv)
- if inv then
- local itms = inv.list()
- for i = 1,inv.size() do
- if itms[i] then
- total = total + itms[i].count
- end
- end
- return total,inv.size()*64
- end
- return 0,0
- end
- end
- local function eventHandler()
- while true do
- local ev = {os.pullEvent()}
- local eventLine = ""
- for i = 1,#ev do
- eventLine = eventLine.." "..tostring(ev[i])
- end
- local event = ev[1]
- if event == "peripheral" then
- other = ev[2]
- if other:find("shulker") then
- state = "shulker_percent"
- shulkerLoaded = true
- else
- state = "wrong_inventory"
- shulkerLoaded = false
- end
- elseif event == "peripheral_detach" then
- other = ""
- state = "no_shulker"
- shulkerLoaded = false
- end
- end
- end
- local function inBetween(x1,y1,x2,y2,x,y)
- return x1 <= x and x2 >= x and y1 <= y and y2 >= y
- end
- local function dispense()
- grabCobble(myTurtleName,1)
- turtle.dropUp()
- end
- local function voidInventory()
- for i = 1,16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- voidInventory()
- local function fillShulker()
- writeAt(1,2,"Filling a shulker box.")
- local spaces = math.ceil(itm/64)
- local maxSpaces = math.ceil(max/64)
- local spLeft = maxSpaces - spaces
- local shulk = peripheral.wrap(other)
- local last = 1
- while spLeft > 0 do
- local toGrab = spLeft
- if toGrab > 16 then
- toGrab = 16
- end
- local isGrabbed,grabbed,lst = grabCobble(myTurtleName,toGrab,last)
- last = lst
- shulk = peripheral.wrap(other)
- if isGrabbed or grabbed > 0 then
- if shulk then
- for i = 1,grabbed do
- if shulk.pullItems(myTurtleName,i,64) == 0 then
- shulk = peripheral.wrap(other)
- end
- end
- else
- writeAt(1,6,"Shulker box is gone, voiding inventory.")
- voidInventory()
- writeAt(1,6,"")
- writeAt(1,2,"")
- return
- end
- spLeft = spLeft - grabbed
- else
- voidInventory()
- writeAt(1,2,"")
- return
- end
- end
- writeAt(1,2,"")
- end
- local function dropHandler()
- while true do
- local event,monitor,x,y = os.pullEvent("monitor_touch")
- local posX = math.floor(mX/3)-4
- local posY = math.floor(mY/2)
- if inBetween(posX,posY,posX+9,posY+2,x,y) then
- if shulkerLoaded then
- fillShulker()
- voidInventory()
- else
- dispense(64)
- end
- end
- end
- end
- local function drawCobbleCount() --also turtle status.
- while true do
- --TURTLE STATUS
- local tsposx = 2
- local tsposy = 5
- local lines = math.ceil(#turtles/5)+tsposy+5
- local width = tsposx+11
- drawBox(tsposx,tsposy,width,lines,colors.black,false)
- mon.setTextColor(colors.white)
- mon.setCursorPos(tsposx+2,5) --"STATUS"
- mon.write("STATUS:")
- for i = 1,#turtles do
- mon.setCursorPos(tsposx+(((i-1)%5)*2)+1,tsposy+2+(math.ceil(i/5)-1)*2)
- local c1 = "0"
- local c2 = "e"
- if checkSingleTurtle(turtles[i]) then
- c2 = "d"
- end
- mon.blit(tostring(i),c1,c2)
- end
- if pc ~= "" then
- mon.setCursorPos(tsposx+2,lines-1)
- local c1 = "0000000"
- local c2 = "eeeeeee"
- if checkSingleTurtle(pc) then
- c2 = "ddddddd"
- end
- mon.blit("CONTROL",c1,c2)
- end
- --cobblecount
- local ok,err = pcall(refreshItems)
- if ok then
- drawBox(math.floor((mX*(2/3)))+1,1,mX,math.floor(mY*(2/3))-1,colors.black,true)
- drawBox(math.ceil((mX*(2/3)))+2,2,mX,math.floor(mY*(2/3))-2,colors.lightGray,false)
- --cobbleCount,maxCobble, OUT OF 22
- local clrHeight = math.floor(math.floor(mY*(2/3))-1-cobbleCount/maxCobble * 22)
- local color = colors.red
- if clrHeight == 2 then
- color = colors.blue
- elseif clrHeight < 7 then
- color = colors.green
- elseif clrHeight < 16 then
- color = colors.orange
- end
- drawBox(math.ceil((mX*(2/3)))+2,clrHeight,mX,math.floor(mY*(2/3))-2,color,false)
- local a = tostring(cobbleCount)
- local b = tostring(maxCobble)
- local longest = b:len()
- if a:len() > longest then
- longest = a:len()
- end
- mon.setCursorPos(mX-6-a:len(),12)
- mon.setTextColor(colors.black)
- mon.write(a)
- mon.setCursorPos(mX-6-longest,13)
- mon.write(string.rep("-",longest))
- mon.setCursorPos(mX-6-b:len(),14)
- mon.write(b)
- writeAt(1,4,"Item count read successfully.")
- else
- writeAt(1,4,"Error reading item count")
- end
- if not checkSingleTurtle(pc) then
- rebootTurtle(pc)
- end
- sleep(10)
- end
- end
- local function peripheralWatch()
- while true do
- local ok
- local bad = false
- if other then
- if state == "shulker_percent" then
- bad = false
- ok,itm,max = pcall(refreshSingleInventory,other)
- if ok then
- states.right[state](itm,max)
- else
- states.right.error()
- end
- else
- if state == "wrong_inventory" then
- writeAt(1,3,"Wrong inventory connected.")
- bad = true
- else
- writeAt(1,3,"")
- bad = false
- end
- states.right[state]()
- if not bad then
- os.sleep(1)
- end
- end
- else
- states.right.no_shulker()
- os.sleep(1)
- end
- if not bad then
- os.sleep(1)
- else
- os.sleep(0.5)
- end
- end
- end
- local function drawMain()
- while true do
- local function swrite(x,y,text)
- mon.setCursorPos(x,y)
- mon.write(text)
- end
- local posX = math.floor(mX/3)-4
- local posY = math.floor(mY/2)
- mon.setCursorPos(posX,posY)
- mon.setBackgroundColor(colors.orange)
- mon.setTextColor(colors.black)
- mon.write(" ")
- mon.setCursorPos(posX,posY+1)
- mon.write(" DISPENSE ")
- mon.setCursorPos(posX,posY+2)
- mon.write(" ")
- mon.setBackgroundColor(colors.yellow)
- local hw = 24
- swrite(1,1,"Bulk Cobblestone Dispenser v2.0.0")
- swrite(1,hw,"HOW TO USE:")
- swrite(3,hw+1,"IF YOU HAVE A SHULKER BOX:")
- swrite(5,hw+2,"1. Place shulker box on modem")
- swrite(5,hw+3,"2. Press dispense")
- swrite(5,hw+4,"The shulker box will be filled")
- swrite(3,hw+6,"IF YOU DON'T HAVE A SHULKER BOX:")
- swrite(5,hw+7,"1. Press dispense")
- swrite(5,hw+8,"The turtle drops 64 cobblestone.")
- mon.setTextColor(colors.red)
- swrite(1,hw+11,"Note: Since chests do not hold items")
- swrite(1,hw+12,"after being broken, they do not work.")
- swrite(1,hw+13,"(Shulker boxes only)")
- os.sleep(10)
- end
- end
- local function watchTurtles()
- while true do --Bruh this function is bad, but basically it used to boot the turtles as well, now it only refreshes.
- --for i = 1,3 do
- refreshTurtles()
- os.sleep(5)
- -- end
- --[[for i = 1,#turtles do
- if not checkSingleTurtle(turtles[i]) then
- print("Booting "..turtles[i])
- rebootTurtle(turtles[i])
- end
- end
- if not checkSingleTurtle(pc) then
- print("Booting "..pc)
- rebootTurtle(pc)
- end]]
- --os.sleep(5)
- end
- end
- refreshItems()
- drawBG()
- writeAt(1,1,"Max: " .. tostring(maxCobble))
- local function par()
- parallel.waitForAny(
- peripheralWatch,
- eventHandler,
- dropHandler,
- drawCobbleCount,
- drawMain,
- watchTurtles
- )
- end
- local ok,err = pcall(par)
- if not ok and not err:find("Terminated") then
- writeAt(1,10,err)
- mon.setBackgroundColor(colors.blue)
- mon.clear()
- mon.setTextColor(colors.black)
- mon.setCursorPos(1,1)
- mon.write("Crashed.")
- mon.setCursorPos(1,2)
- mon.write(err)
- mon.setCursorPos(1,3)
- mon.write("Auto reboot in 30 seconds.")
- os.sleep(30)
- os.reboot()
- end
Add Comment
Please, Sign In to add comment