Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_empty_slot()
- found_empty = false
- for i=1, 16 do
- if turtle.getItemCount() == 0 then
- found_empty = true
- break
- end
- end
- return found_empty
- end
- function starts_with(str, start)
- return str:sub(1, #start) == start
- end
- function ends_with(str, ending)
- return ending == "" or str:sub(-#ending) == ending
- end
- function update_index()
- foreign_items = 0
- for i=1, 16 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if item then
- if item.name == top_item then
- turtle.dropUp()
- else
- if item.name == bottom_item then
- turtle.dropUp()
- else
- foreign_items = foreign_items + item.count
- end
- end
- end
- end
- if foreign_items > 0 then
- print("WARNING: " .. tostring(foreign_items) .. " FOREIGN ITEMS DETECTED")
- end
- if not get_empty_slot() then
- print("WARNING: FULL INVENTORY")
- else
- top_item = nil
- bottom_item = nil
- if turtle.suckUp(1) then
- top_item = turtle.getItemDetail().name
- turtle.dropUp(1)
- end
- if turtle.suckDown(1) then
- bottom_item = turtle.getItemDetail().name
- turtle.dropDown(1)
- end
- end
- print("Top container: " .. tostring(top_item))
- print("Bottom container: " .. tostring(bottom_item))
- end
- rednet.open("left")
- top_item = nil
- bottom_item = nil
- last = nil
- first = nil
- last = not turtle.detect()
- a, b = turtle.inspectDown()
- first = b.name == "minecraft:obsidian"
- if not first then
- update_index()
- end
- neighbor = nil
- if not last then
- neighbor = peripheral.wrap("front")
- end
- if first then
- while true do
- term.write(">>> ")
- command = read()
- if command == "index" then
- rednet.send(neighbor.getID(), {}, "index")
- sender, message, protocol = rednet.receive()
- print("There are " .. tostring(table.getn(message)) .. " nodes connected to the network")
- end
- if command == "reboot" then
- rednet.send(neighbor.getID(), ".", "reboot")
- os.reboot()
- end
- if command == "print" then
- term.write("Enter message: ")
- rednet.send(neighbor.getID(), read(), "print")
- end
- if command == "get" then
- term.write("Enter item name: ")
- item = read()
- term.write("How many: ")
- count = read()
- if count == "" then
- count = "64"
- end
- rednet.send(neighbor.getID(), {item, count}, "get")
- sender, message, protocol = rednet.receive()
- if message == true then
- turtle.suck()
- print("Sucessfully retrieved item")
- else
- print("Failed to find item")
- end
- end
- if command == "store" then
- turtle.select(1)
- item = turtle.getItemDetail()
- if item then
- else
- print("Nothing to store")
- end
- end
- end
- else
- while true do
- sender, message, protocol = rednet.receive()
- print(protocol .. ": " .. tostring(message))
- if protocol == "index" then
- modified_message = message
- table.insert(modified_message, os.getComputerID())
- if last then
- rednet.send(sender, modified_message, "index")
- else
- rednet.send(neighbor.getID(), modified_message, "index")
- neighbor_reply_id, index_reply, index_protocol = rednet.receive(1)
- if neighbor_reply_id then
- rednet.send(sender, index_reply, "index")
- else
- print("No response from neighbor")
- rednet.send(sender, "Unresponsive neighbor", "error")
- end
- end
- end
- if protocol == "get" then
- found = false
- if not (top_item == nil) and ends_with(top_item, message[1]) then
- turtle.suckUp(tonumber(message[2]))
- found = true
- end
- if not (bottom_item == nil) and ends_with(bottom_item, message[1]) then
- turtle.suckDown(tonumber(message[2]))
- found = true
- end
- if found then
- rednet.send(sender, true, "get")
- while turtle.getItemCount() > 0 do
- sleep(0.1)
- end
- update_index()
- else
- if last then
- rednet.send(sender, false, "get")
- else
- rednet.send(neighbor.getID(), message, "get")
- neighbor_reply_id, get_reply, get_protocol = rednet.receive(1)
- if neighbor_reply_id then
- if get_reply == true then
- turtle.suck(64)
- end
- rednet.send(sender, get_reply, "get")
- else
- print("No response from neighbor")
- rednet.send(sender, "Unresponsive neighbor", "error")
- end
- end
- end
- end
- if protocol == "reboot" then
- if not last then
- rednet.send(neighbor.getID(), ".", "reboot")
- end
- os.reboot()
- end
- if protocol == "print" then
- if not last then
- rednet.send(neighbor.getID(), message, "print")
- end
- print("MESSAGE: " .. message)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement