Difference between revisions of "Making a Password Protected Door"

From ComputerCraft Wiki
Jump to: navigation, search
(Why did you mess up this page?)
Line 9: Line 9:
  
  
First, you need to craft a computer, and connect the back to an iron door with redstone, or just have a Computer next to the door.
+
First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:
 +
 
 +
[[File:Tutorial1.png]]
 +
 
 +
[[File:cTutorial2.png]]
  
 
Once you're done with the basics, open the computer and edit the "startup" file. (type in 'edit startup')
 
Once you're done with the basics, open the computer and edit the "startup" file. (type in 'edit startup')
Line 18: Line 22:
 
<pre>
 
<pre>
 
while true do
 
while true do
print ("Enter Password")
+
term.clear()
input = read()
+
term.setCursorPos(1, 1)
 +
print("Please Enter Password:")
 +
input = read("*")
 
</pre>
 
</pre>
  
The first line opens a loop that will continue running forever. The second lines "Prints" the message Enter Password on the screen. you can get rid of this if it's not wanted. The third line is used to change what the characters you type into the computer appear as. If you put ("*") after read, then it will appear in asterisks, While leaving it as is will show the password.
+
The first line opens a loop that will continue running forever, or until we explicitly exit it with the 'break' keyword. The second and third lines clear the screen of whatever may have been on it, and then set the cursor position to the top-left corner of the screen. This ensures that each time the program loops, the text appears in the same place. The fifth line assigns whatever the user types to the variable 'input'. So for example, if I typed "qwerty", it would be assigning "qwerty" to the 'input' variable. In the read call on the third line ('read("*")'), the asterisk character will be used to replace each letter typed by the user, so that the password stays hidden.
  
 
Here are the next few lines added on:
 
Here are the next few lines added on:
Line 28: Line 34:
 
<pre>
 
<pre>
 
while true do
 
while true do
print ("Enter Password")
+
term.clear()
input = read()
+
term.setCursorPos(1, 1)
if input == "password" then
+
print("Please Enter Password:")
print ("Password Accepted")
+
input = read("*")
rs.setOutput("right", true)
+
if input == "password" then
sleep()
+
  redstone.setOutput("back", true)
 +
  sleep(2)
 +
  redstone.setOutput("back", false)
 +
end
 +
end
 
</pre>
 
</pre>
  
The fourth line makes it so that if the word PASSWORD is typed, that it will go on to next steps. change the word "Password" to your desired password. The fifth line tells the computer to print Password Accepted if you correctly input the password. The sixth line outputs a redstone current on the given side. change "right" to the side you want to output a current. the seventh line tells the computer to halt the program for the given amount of time (Meaning it keeps door open for that amount of time)
+
The first line we add, 'if input == "password" then' checks whether or not the 'input' variable contains the string "password". The three lines immediately following are only executed if the password is correct. So if it's correct, 'redstone.setOutput("back", true)' sets the redstone output on the back side to true (on), which opens the door. The program then wait two seconds ('sleep(2)'), and turns the redstone output back off again. The program then reaches the end of the loop and repeats.
  
now, for the FINAL lines.
+
Please note that this is the basic format for a computer protection program as well. We would put this program in the startup file and instead of outputting redstone signals if the password was correct, we would use the break keyword instead to exit the loop and allow access to the rest of the computer.
  
 +
Note also that if the program fails somehow, pressing and holding Ctrl + T will terminate it and allow you to edit it, see the [http://computercraft.info/wiki/index.php?title=Making_a_Password_Protected_Door#Stop_people_from_terminating_your_lock No termination section] to stop people from exiting your lock.
 +
 +
= Alternative Code =
 +
Type 'edit startup' and type in this code:
 +
(Use Ctrl + T to terminate the program, all code is explained in the comments, the --'s. It's good to read the comments, so you actually know what the code does, you do not need to have the comments in your code, of course)
 
<pre>
 
<pre>
while true do
+
local side = "left" -- Change left to whatever side your door / redstone is on, E.G: left, right, front, back, bottom, top. Be sure to leave the "s around it, though
print ("Enter Password")
+
local password = "bacon" -- Change bacon to what you want your password to be. Be sure to leave the "s around it, though
input = read()
+
local opentime = 5 -- Change 5 to how long (in seconds) you want the redstone current to be on. Don't put "s around it, though
if input == "password" then
+
term.clear() -- Clears the screen
print ("Password Accepted")
+
term.setCursorPos(1,1) -- Fixes the cursor position
rs.setOutput("right", true)
+
write("Password: ") -- Prints 'Password: ' to the screen
sleep()
+
local input = read("*") -- Makes the variable 'input' have the contents of what the user types in, the "*" part censors out the password
rs.setOutput("right", false)
+
if input == password then -- Checks if the user inputted the correct password
else print ("Incorrect Password")
+
term.clear() -- Already explained up top
end
+
term.setCursorPos(1,1)
 +
print("Password correct!") -- Prints 'Password correct!' to the screen
 +
rs.setOutput(side,true) -- Output a redstone current to the side you specified
 +
sleep(opentime) -- Wait the amount of seconds you specifed, then..
 +
rs.setOutput(side,false) -- Stop outputting a redstone current
 +
os.reboot() -- Reboot the computer, reopening the lock
 +
else -- Checks if the user didn't input the correct password
 +
term.clear()
 +
term.setCursorPos(1,1)
 +
print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
 +
sleep(2) -- Waits 2 seconds
 +
os.reboot() -- Reboot the computer, reopening the lock
 
end
 
end
 
</pre>
 
</pre>
 
The eighth line turns OFF the redstone current on the line, thus closing the door. The ninth line indicates that if the password ISN'T correct, that it will print Incorrect Password. The final two lines end the program.
 
  
 
= Stop people from terminating your lock =
 
= Stop people from terminating your lock =
  
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program (Note: This makes it so that you CANNOT access it by normal means):
+
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:
 
<pre>os.pullEvent = os.pullEventRaw</pre>
 
<pre>os.pullEvent = os.pullEventRaw</pre>
 
Okay, so now you want to CHANGE the program, but you can't. Here's the simple fix: Make a new computer and a disk drive. Get a floppy disk and put it in a disk drive that is adjacent to the new computer. Now, type in the computer
 
edit disk/startup. this will make it so you can make it so it runs the disk's program on CPU startup. Now type in it print ("Hello World"). now save the program..... now, get that disk drive and put it NEXT to that computer. Then, put in the disk and enter the computer and do CTRL + R. the message "Hello World" should appear on the screen, allowing access to the startup file again.
 
 
= Turning the password into asterisks =
 
Replace the
 
input = read()
 
line with
 
input = read("*")
 
 
Now when you write your password it should show up as asterisks.
 
 
 
  
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Revision as of 18:03, 24 July 2012

This tutorial covers on how to make a computer output redstone current when the right password is typed in. The current is then used to trigger an iron door.


How to make it

A password protected door is actually pretty easy, if you break it into steps.


First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:

Tutorial1.png

CTutorial2.png

Once you're done with the basics, open the computer and edit the "startup" file. (type in 'edit startup') This will make it so the program will be executed when the computer boots.

Once you access the startup file, enter in these five lines.

while true do
 term.clear()
 term.setCursorPos(1, 1)
 print("Please Enter Password:")
 input = read("*")

The first line opens a loop that will continue running forever, or until we explicitly exit it with the 'break' keyword. The second and third lines clear the screen of whatever may have been on it, and then set the cursor position to the top-left corner of the screen. This ensures that each time the program loops, the text appears in the same place. The fifth line assigns whatever the user types to the variable 'input'. So for example, if I typed "qwerty", it would be assigning "qwerty" to the 'input' variable. In the read call on the third line ('read("*")'), the asterisk character will be used to replace each letter typed by the user, so that the password stays hidden.

Here are the next few lines added on:

while true do
 term.clear()
 term.setCursorPos(1, 1)
 print("Please Enter Password:")
 input = read("*")
 if input == "password" then
  redstone.setOutput("back", true)
  sleep(2)
  redstone.setOutput("back", false)
 end
end

The first line we add, 'if input == "password" then' checks whether or not the 'input' variable contains the string "password". The three lines immediately following are only executed if the password is correct. So if it's correct, 'redstone.setOutput("back", true)' sets the redstone output on the back side to true (on), which opens the door. The program then wait two seconds ('sleep(2)'), and turns the redstone output back off again. The program then reaches the end of the loop and repeats.

Please note that this is the basic format for a computer protection program as well. We would put this program in the startup file and instead of outputting redstone signals if the password was correct, we would use the break keyword instead to exit the loop and allow access to the rest of the computer.

Note also that if the program fails somehow, pressing and holding Ctrl + T will terminate it and allow you to edit it, see the No termination section to stop people from exiting your lock.

Alternative Code

Type 'edit startup' and type in this code: (Use Ctrl + T to terminate the program, all code is explained in the comments, the --'s. It's good to read the comments, so you actually know what the code does, you do not need to have the comments in your code, of course)

local side = "left" -- Change left to whatever side your door / redstone is on, E.G: left, right, front, back, bottom, top. Be sure to leave the "s around it, though
local password = "bacon" -- Change bacon to what you want your password to be. Be sure to leave the "s around it, though
local opentime = 5 -- Change 5 to how long (in seconds) you want the redstone current to be on. Don't put "s around it, though
term.clear() -- Clears the screen
term.setCursorPos(1,1) -- Fixes the cursor position
write("Password: ") -- Prints 'Password: ' to the screen
local input = read("*") -- Makes the variable 'input' have the contents of what the user types in, the "*" part censors out the password
if input == password then -- Checks if the user inputted the correct password
 term.clear() -- Already explained up top
 term.setCursorPos(1,1)
 print("Password correct!") -- Prints 'Password correct!' to the screen
 rs.setOutput(side,true) -- Output a redstone current to the side you specified
 sleep(opentime) -- Wait the amount of seconds you specifed, then..
 rs.setOutput(side,false) -- Stop outputting a redstone current
 os.reboot() -- Reboot the computer, reopening the lock
else -- Checks if the user didn't input the correct password
 term.clear()
 term.setCursorPos(1,1)
 print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
 sleep(2) -- Waits 2 seconds
 os.reboot() -- Reboot the computer, reopening the lock
end

Stop people from terminating your lock

If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:

os.pullEvent = os.pullEventRaw