Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem1 = 6565
- modem2 = 6566
- modem3 = 6567
- modem4 = 6568
- local item = "" -- Item ID. Do not include minecraft:; this will be added later
- if fs.exists(".item") and item == "" then
- local f = fs.open(".item","r")
- if f then
- item = f.readLine()
- f.close()
- end
- end
- if turtle.getItemDetail(16) == nil or not (turtle.getItemDetail(16).name == "minecraft:chest" or turtle.getItemDetail(16).name == "minecraft:trapped_chest") then
- error("missing chest/trapped chest on slot 16")
- end
- local count = 0
- local modems = { peripheral.find("modem") }
- if #modems == 0 then error("No modems found") end
- item = item:lower()
- local busy = false
- if item == nil or item == "" then
- os.setComputerLabel("Row : Empty")
- else
- os.setComputerLabel("Row : " .. item:sub(1,1):upper() .. item:sub(2))
- end
- if item ~= "" then
- item = "minecraft:"..item
- print(item)
- else
- print("Empty")
- end
- local modem = modems[1]
- modem.open(modem1) --Send Info
- modem.open(modem2) --Recount Listener
- modem.open(modem3) --Request Listener
- modem.open(modem4) --Setup listener
- while turtle.up() do end
- local inputReady = true
- local function selSlot(slot) --Might save time, due to less turtle.select()s?
- if turtle.getSelectedSlot() ~= slot then turtle.select(slot) end
- end
- function detectItemCount()
- inputReady = false
- busy = true
- count = 0
- while turtle.up() do end
- for sl = 1,15 do
- if turtle.getItemCount(sl) > 0 then
- turtle.select(sl)
- turtle.dropUp()
- end
- end
- while turtle.down() do
- if turtle.getItemDetail(16) == nil or not (turtle.getItemDetail(16).name == "minecraft:chest" or turtle.getItemDetail(16).name == "minecraft:trapped_chest") then
- error("missing chest/trapped chest on slot 16")
- end
- selSlot(16)
- turtle.placeUp()
- selSlot(1)
- while turtle.suck() do
- selSlot(1)
- count = count + turtle.getItemCount(1)
- turtle.dropUp()
- end
- while turtle.suckUp() do
- selSlot(1)
- turtle.drop()
- end
- selSlot(16)
- turtle.digUp()
- end
- while turtle.up() do end
- inputReady = true
- busy = false
- print("Finished count: "..count)
- end
- function countItms()
- local tuCount = 0
- for s = 1,15 do
- tuCount = tuCount + turtle.getItemCount(s)
- end
- return tuCount
- end
- function modemListener()
- while true do
- local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
- if chnl == modem2 then
- print("Detecting item count")
- detectItemCount()
- elseif chnl == modem3 and item ~= "" then
- if type(msg) == "table" and type(msg.name) == "string" and type(msg.count) == "number" then
- if msg.name == item then
- if msg.count <= count then
- inputReady = false
- busy = true
- modem.transmit(rChnl,chnl,{type="requestResponse",success=true})
- print("Request: success")
- while turtle.down() do
- if msg.count - countItms() <= 0 then
- break
- end
- while turtle.suck(msg.count - countItms() >= 64 and 64 or msg.count - countItms()) and countItms() < msg.count do end
- end
- while turtle.down() do end
- count = count - countItms()
- print("New count: "..count.." (subtracted "..countItms().." )")
- for slot = 1,15 do
- if turtle.getItemCount(slot) > 0 then
- selSlot(slot)
- turtle.dropDown()
- end
- end
- while turtle.up() do end
- busy = false
- inputReady = true
- else
- modem.transmit(rChnl,chnl,{type="requestResponse",success=false,error="Not enough items"})
- print("Request: failure (not enough items)")
- end
- else
- print("Request: failure (item does not match)")
- end
- else
- modem.transmit(rChnl,chnl,{type="requestResponse",success=false,error="Invalid input"})
- print("Request: failure (invalid input)")
- end
- elseif chnl == modem4 then
- while turtle.up() do end
- local x,y,z = gps.locate()
- if x ~= nil then
- modem.transmit(rChnl,chnl,{type="setupResponse",success=true,id=item,x=x,y=y+1,z=z})
- print("Sent setup: success")
- else
- modem.transmit(rChnl,chnl,{type="setupResponse",success=false,error="GPS not active"})
- print("Sent setup: failure")
- end
- end
- end
- end
- function statusUpdater()
- while true do
- local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
- if chnl == modem1 then
- local x,y,z = gps.locate()
- local inv = {}
- for slot = 1,16 do
- local detail = turtle.getItemDetail(slot)
- if detail == nil then
- inv[slot] = {}
- else
- inv[slot] = detail
- end
- end
- modem.transmit(rChnl,chnl,{type="infoRowResponse",count=count,item=item,fuel=turtle.getFuelLevel(),inventory=inv,location={x=x,y=y,z=z},busy=busy})
- print("Sent info")
- end
- end
- end
- function testUp()
- while true do
- if inputReady then
- turtle.select(1)
- if turtle.suckUp() then
- busy = true
- turtle.select(1)
- while turtle.suckUp() and turtle.getItemDetail(15) == nil do turtle.select(1) end
- local scheduleDump = false
- local loops = 0
- while true do
- if loops >= 50 then
- scheduleDump = true break
- end
- local empty = false
- for slot = 1,15 do
- if turtle.getItemDetail(slot) ~= nil and turtle.getItemDetail(slot).name == item then
- empty = true
- elseif turtle.getItemDetail(slot) ~= nil then --foreign item found; dump through the system.
- scheduleDump = true
- end
- end
- if empty then
- turtle.down()
- for slot = 1,15 do
- if turtle.getItemDetail(slot) ~= nil and turtle.getItemDetail(slot).name == item then
- turtle.select(slot)
- local cnt = turtle.getItemCount(slot)
- if turtle.drop() and turtle.getItemCount(slot) ~= cnt then count = count + cnt print("New count: "..count.." (added "..cnt.." )") end
- end
- end
- else
- break
- end
- loops = loops + 1
- end
- if scheduleDump then
- while turtle.down() do end
- for slot = 1,15 do
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- while turtle.up() do end
- busy = false
- end
- end
- sleep(5)
- end
- end
- parallel.waitForAll(modemListener,testUp,statusUpdater,detectItemCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement