Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local slotsPerChest=27
- local chestsPerRow=5
- local turtleSide=
- local InputSide=
- local OutputSide=
- local m=peripheral.wrap("right")
- local function load(name)
- if fs.exists(name) then
- local f= fs.open(name,"r")
- local result=textutils.unserialize(f.readAll())
- f.close()
- return result
- else
- return {}
- end
- end
- storage=load("storage")
- names=load("names")
- local x,y=1,1
- local function go(tx,ty)
- for q=y+1,ty do
- repeat until turtle.up()
- end
- for q=ty+1,y do
- repeat until turtle.down()
- end
- if x<tx then
- turtle.turnRight()
- for q=x+1,tx do
- repeat until turtle.forward()
- end
- turtle.turnLeft()
- end
- if x>tx then
- turtle.turnLeft()
- for q=tx+1,x do
- repeat until turtle.forward()
- end
- turtle.turnRight()
- end
- x,y=tx,ty
- end
- local function save()
- local f=fs.open("storage","w")
- f.write(textutils.serialize(storage))
- f.close()
- local f=fs.open("names","w")
- f.write(textutils.serialize(names))
- f.close()
- end
- function outputAll()
- go(1,1)
- local sorter = peripheral.wrap("left")
- for i,j in pairs(sorter.list(turtleSide)) do
- sorter.extract(turtleSide,i,OutputSide,j)
- end
- end
- function getItem(id,amount)
- if countItem(id)<amount then
- return "Not enough items in storage"
- end
- turtle.select(1)
- for j,k in ipairs(storage) do
- for o=1,chestsPerRow do
- local w=(j%2==0 and chestsPerRow+1-o or o)
- for p=0,slotsPerChest-1 do
- if k[w][p] and k[w][p][1]==id then
- go(w,j)
- if k[w][p][2]<=amount then
- m.suck(p,k[w][p][2])
- amount=amount-k[w][p][2]
- k[w][p] = nil
- else
- m.suck(p,amount)
- k[w][p][2]=k[w][p][2]-amount
- amount=0
- end
- if amount<=0 then
- outputAll()
- return "Success"
- end
- if turtle.getItemCount(16)>0 then
- outputAll()
- end
- end
- end
- end
- end
- end
- function countItem(id)
- local result=0
- for j,k in ipairs(storage) do
- for o=1,chestsPerRow do
- for p=0,slotsPerChest-1 do
- if k[o][p] then
- if k[o][p][1]==id then
- result=result+k[o][p][2]
- end
- end
- end
- end
- end
- return result
- end
- print("name <name> to set a name for the item in slot 1\nget <name or id> <amount> to get items\ndo not change the chest manually!")
- local running=true
- while running do
- local emptySlots=0
- for i,j in pairs(storage) do
- for k,l in pairs(j) do
- for o=0,slotsPerChest-1 do
- if not l[o] then
- emptySlots=emptySlots+1
- end
- end
- end
- end
- if emptySlots<16 then
- turtle.select(1)
- local slotInv =0
- repeat
- m.suckDown(slotInv,chestsPerRow-turtle.getItemCount(1))
- slotInv=(slotInv+1)%slotsPerChest
- sleep(0)
- until turtle.getItemCount(1)==chestsPerRow
- for i=1,chestsPerRow do
- go(i,#storage+1)
- turtle.place()
- end
- storage[#storage+1]={}
- for i=1,chestsPerRow do
- table.insert(storage[#storage],{})
- end
- save()
- go(1,1)
- end
- local inputItems=peripheral.call("left","list",InputSide)
- local countItems=0
- local sortedInput={}
- local reverse={}
- for i,j in pairs(inputItems) do
- for k=j,1,-64 do
- countItems=countItems+1
- table.insert(sortedInput,i)
- reverse[i]=countItems
- end
- end
- if countItems>=16 then
- local inTurtle={}
- local itemsToBeSorted={}
- local sorter=peripheral.wrap("left")
- for i=1,16 do
- sorter.extract(InputSide,sortedInput[i],turtleSide,1)
- sorter.extract(InputSide,sortedInput[i],turtleSide,math.min(inputItems[sortedInput[i]]-1,turtle.getItemSpace(i)))
- if turtle.getItemCount(i)~=inputItems[sortedInput[i]] and turtle.getItemSpace(i)~=0 then
- error("Items did not reach turtle")
- end
- itemsToBeSorted[i]=turtle.getItemCount(i)
- end
- for j,k in ipairs(storage) do
- for o=1,chestsPerRow do
- for p=0,slotsPerChest-1 do
- if k[o][p] then
- for l=1,16 do
- if k[o][p][1]==sortedInput[l] then
- itemsToBeSorted[l]=itemsToBeSorted[l]-(turtle.getItemSpace(l)-k[o][p][2])
- break
- end
- end
- end
- end
- end
- end
- for j,k in ipairs(storage) do
- for o=1,chestsPerRow do
- local w=(j%2==0 and chestsPerRow+1-o or o)
- for p=0,slotsPerChest-1 do
- if k[w][p] then
- for l=1,16 do
- if k[w][p][1]==sortedInput[l] then
- go(w,j)
- local oldCount=turtle.getItemCount(l)
- turtle.select(l)
- m.drop(p,(turtle.getItemSpace(l)-k[w][p][2]))
- k[w][p][2]=k[w][p][2]+(oldCount-turtle.getItemCount(l))
- break
- end
- end
- else
- for l=1,16 do
- if itemsToBeSorted[l]>0 then
- go(w,j)
- k[w][p]={}
- k[w][p][1]=sortedInput[l]
- k[w][p][2]=turtle.getItemCount(l)
- turtle.select(l)
- m.drop(p,turtle.getItemCount(l))
- itemsToBeSorted[l]=0
- break
- end
- end
- end
- end
- end
- end
- go(1,1)
- turtle.select(1)
- save()
- end
- os.startTimer(30)
- term.setCursorBlink(true)
- local event={os.pullEvent()}
- if event[1] and event[1]=="char" then
- os.queueEvent(unpack(event))
- print("> ")
- local command=read()
- if command:sub(1,5)=="name " then
- local i=0
- for j=2,16 do
- i=i+turtle.getItemCount(j)
- end
- if turtle.getItemCount(1)==0 or i>0 then
- print("The only Item in the turtle need to be in Slot 1")
- else
- local sorter=peripheral.wrap("left")
- for i,j in pairs(sorter.list(turtleSide)) do
- names[command:sub(6)]=i
- print("Name set to: "..command:sub(6)or "")
- end
- end
- elseif command:sub(1,4) =="get " then
- local space=command:find(" ",5)
- local name=command:sub(5,space-1)
- print(getItem(names[name] or tonumber(name),tonumber(command:sub(space+1))))
- elseif command:sub(1,6) =="count " then
- print("There are "..tostring(countItem(names[command:sub(7)] or tonumber(command:sub(7)))).." "..(names[command:sub(7)] or "Items"))
- elseif command:sub(1,4) =="exit" then
- running=false
- else
- print("Unknown command")
- end
- end
- term.setCursorBlink(false)
- end
Advertisement
Add Comment
Please, Sign In to add comment