Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------This Program is By EvasiveParacite and
- ---------is used to send commands to the main
- ---------computer
- --------Configuration (you can change this)---------
- local channel = 92 --must be the same as replyChannel on main computer
- local modemSide = "bottom"
- local promptTextColor = colors.orange
- local userTextColor = colors.yellow
- local confirmTextColor = colors.red --color of command when programs asks for confirmation
- local helpTextColor = colors.green
- local helpPageDelay = .8 --delay between writing each like in the help page in seconds
- --------Initializaton (do NOT change this)---------
- local modem = peripheral.wrap(modemSide)
- local command = " "
- local commandStart = " " --first word of command
- local parameters = "" --all but first word of command
- local commands = {"help","/ "}
- local commandDescriptions = {"displays a list of commands, no parameters","runs the parameters as command block text"}
- local answer = " "
- local answered = false
- local isValid = false
- local event = "modem_message"
- local side = modemSide
- local senderChannel = channel
- local replyChannel = channel
- local message = ""
- local distance = 0
- ------------------Functions------------------------
- local function readCommand(string)
- local count = 0
- for i in string.gmatch(string,"%S+") do
- count = count + 1
- if count == 1 then
- commandStart = i
- elseif count == 2 then
- parameters = i
- else
- parameters = parameters.." "..i
- end
- end
- end
- local function moveCursorDown()
- local x = 0
- local y = 0
- x, y = term.getCursorPos()
- term.setCursorPos(1,y+1)
- end
- local function isCommandValid(startString) --string must be commandStart
- local tempBool = false
- for i=1, #commands do
- if startString == commands[i] then
- tempBool = true
- end
- end
- return tempBool
- end
- local function confirm()
- term.setTextColor(promptTextColor)
- term.write("[y/n]: ")
- term.setTextColor(userTextColor)
- answer = io.read()
- term.setTextColor(promptTextColor)
- if answer == "y" or answer == "n" then
- answered = true
- else
- answered = false
- while not answered do
- print("you must enter 'y' or 'n'...")
- term.write("[y/n]: ")
- term.setTextColor(userTextColor)
- answer = io.read()
- if answer == "y" or answer == "n" then
- answered = true
- end
- term.setTextColor(promptTextColor)
- end
- end
- end
- local function prompt()
- term.setTextColor(promptTextColor)
- term.write("Please enter your command or 'help' for more")
- moveCursorDown()
- term.write("info: ")
- term.setTextColor(userTextColor)
- command = io.read()
- print("")
- readCommand(command)
- isValid = isCommandValid(commandStart)
- term.setTextColor(colors.red)
- if commandStart == "help" then
- term.clear()
- term.setCursorPos(1,1)
- answer = "y"
- elseif parameters == nil or parameters == "" then
- print("you have not entered any parameters, your command must be at least 2 words unless it is 'help'")
- elseif isValid == false then
- print("'"..commandStart.."'".." is not a valid command. Would you like to go to the help page?")
- confirm()
- if answer == "y" then
- commandStart = "help"
- commandEnd = ""
- end
- else
- term.setTextColor(confirmTextColor)
- print("'"..commandStart.."' + '"..parameters.."'")
- term.setTextColor(promptTextColor)
- print("Is this the command you would like to use?")
- confirm()
- end
- term.setTextColor(promptTextColor)
- end
- local function displayHelp()
- term.setTextColor(helpTextColor)
- print("Welcome to the help page!")
- print(" ")
- print(" ")
- sleep(helpPageDelay/3)
- print("Command Formatting: command + ' ' + parameters")
- sleep(helpPageDelay/3)
- print(" ")
- print("Commands: ")
- sleep(helpPageDelay)
- print(" ")
- for i=1, #commands do
- term.write("'"..commands[i].."' : ")
- print(commandDescriptions[i])
- sleep(helpPageDelay)
- end
- term.setTextColor(promptTextColor)
- sleep(helpPageDelay)
- print(" ")
- print(" ")
- end
- local function transmit()
- end
- local function waitForReply()
- local count = 0
- local correctReply = false
- local correctEvent = false
- while count < 5 and (not correctReply or not correctEvent) do
- print("listening for a confirmation of command execution... if a valid reply is not found within aproximatly "..60-i*20.." seconds the attemp will be abandoned")
- count = count + 1
- os.startTimer(20)
- event, side, senderChannel, replyChannel, message, distance = os.pullEvent("modem_message")
- if event == "modem_message" then
- correctEvent = true
- end
- if message == distance then
- correctReply == true
- end
- end
- print("")
- if not correctReply and not correctEvent then
- term.setTextColor(colors.red)
- print("Command Execution Failed")
- else
- term.setTextColor(colors.green)
- print("Success! Command Executed")
- end
- term.setTextColor(promptTextColor)
- end
- ------------------Main Program---------------------
- modem.open(channel)
- while true do
- prompt()
- if answer == "y" and isValid == true then
- if commandStart == "help" then
- displayHelp()
- else
- transmit()
- waitForReply()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment