Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("file")
- parent = 2044
- retriever = 1963
- namesToIDs = file.read("turtleIDs")
- rednet.open("right")
- save = function()
- file.write("turtleIDs", namesToIDs)
- end
- split = function(str, index)
- pos = string.find(str, " ")
- if index == 1 then
- return string.sub(str, 0, pos-1)
- elseif index == 2 then
- return string.sub(str, pos + 1)
- end
- end
- listenForCommands = function()
- while true do
- message = read()
- cmd = split(message, 1)
- args = split(message, 2)
- if cmd == "get" then
- itemName = split(args, 1)
- itemCount = split(args, 2) + 0
- turtleID = namesToIDs[itemName]
- if turtleID ~= nil then
- rednet.send(turtleID, textutils.serialize({["label"]="getItem", ["amount"]=itemCount}))
- else
- print("Unknown item!")
- end
- elseif cmd == "count" then
- itemName = split(args, 1)
- itemCount = split(args, 2)
- turtleID = namesToIDs[itemName]
- if turtleID ~= nil then
- rednet.send(retriever, textutils.serialize({[1]=1}))
- else
- print("Unknown item!")
- end
- elseif cmd == "cmdall" then
- for name, turtle in ipairs(namesToIDs) do
- rednet.send(turtle, textutils.serialize({["label"]="command", ["command"]=args}))
- end
- end
- end
- end
- listenForCalls = function()
- while true do
- ID, message = rednet.receive()
- msg = textutils.unserialize(message)
- if type(msg) ~= "table" then
- msg = {}
- end
- if msg["label"] == "register" then
- namesToIDs[msg["itemname"]] = msg["turtleid"]
- save()
- elseif msg["label"] == "countReturn" then
- print(msg["count"])
- end
- end
- end
- parallel.waitForAny(listenForCommands, listenForCalls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement