Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- command by KnightMiner version 1.0
- -- to be used alongside voice
- -- find a modem
- local opened, found
- for _, side in ipairs( peripheral.getNames() ) do
- if peripheral.getType( side ) == "modem" then
- if not rednet.isOpen( side ) then
- rednet.open( side )
- opened = side
- end
- found = true
- break
- end
- end
- if not found then
- print( "No modem found" )
- return
- end
- -- load config file
- local config = {}
- if fs.exists( "command.cfg" ) then
- local configFile = fs.open( "command.cfg", "r" )
- config = textutils.unserialize( configFile.readAll() ) or {}
- configFile.close()
- end
- local turtles = config.turtles or {}
- local timeout = config.timeout or 5
- -- default ID
- local linkedID
- if config.link then
- linkedID = tonumber( config.link ) or turtles[config.link]
- end
- -- run a single command
- local loop
- function sendCommand( data )
- local input = data:gsub( "^([^ ]*).*$", "%1" )
- local command = data:gsub( "^[^ ]*%s*", "" )
- local num = tonumber( input ) or turtles[input]
- -- exit the loop
- if loop and input == "exit" then
- return true
- -- link a new turtle
- elseif loop and input == "link" then
- if command == "" then
- linkedID = nil
- print( "Unlinked from turtle" )
- else
- linkedID = tonumber( command ) or turtles[command]
- if linkedID then
- print( "Now linked to '" .. command .. "'" )
- else
- print( "Turtle name '" .. command .. "' is invalid" )
- end
- end
- -- standard command
- elseif linkedID or ( num and command ~= "" ) then
- local sentTo
- if linkedID then
- sentTo = linkedID
- rednet.send( linkedID, data, "voice-command" )
- else
- sentTo = num
- rednet.send( num, command, "voice-command" )
- end
- if timeout then
- os.startTimer( timeout )
- end
- while true do
- local event = { os.pullEvent() }
- if event[1] == "key" and event[2] == keys["end"] or event[1] == "timer" then
- break
- elseif event[1] == "rednet_message" then
- if event[2] == sentTo and event[4] == "voice-messages" then
- local messages = event[3]
- for _, msg in ipairs( messages ) do
- print( msg )
- end
- break
- end
- end
- end
- else
- print( "ERROR: Turtle '" .. input .. "' does not exist" )
- end
- end
- -- single command
- local args = { ... }
- if args[1] then
- sendCommand( table.concat( args, " " ) )
- -- standalone disabled
- elseif config.disableLoop then
- print( "Usage: command <command>" )
- -- standalone
- else
- print( "Running command by KnightMiner" )
- print( "Type 'exit' to exit" )
- -- main loop
- loop = true
- local history = {}
- local done
- while not done do
- write( "cmd" .. ( linkedID and ":" .. linkedID or "" ) .. "> " )
- local data = read( nil, history )
- table.insert( history, data )
- if data ~= '' then
- done = sendCommand( data )
- end
- end
- end
- -- shutdown tasks
- if opened then
- rednet.close( opened )
- end
Add Comment
Please, Sign In to add comment