Duncan Geere returns with the second part of his guide to coding in Minecraft (first part here).
Welcome back! When I left you last week, you'd built a computer, explored the filesystem and written your first program. That was easy, so it's time to climb the next rung on the difficulty ladder.
Today, we're going to make a passworded door, an alarm that notifies you when intruders enter your base, and a digital clock for your base. Basically, you'll be the most punctual hermit on the block.
To follow this tutorial you'll need two things - a copy of Minecraft and a copy of the ComputerCraft mod, which can be picked up either direct from the mod website, or through a mod pack like the Technic Pack or Feed The Beast. I'm using version 1.481 of ComputerCraft, included in version 0.5.1 of Tekkit Lite. Oh, and if you haven't done so already, run quickly through last week's tutorial so that you've got a basic understanding of the filesystem.
In your base, you'll need a computer with a disk drive hooked up to it, an iron door, a handful of redstone dust (about fifteen should do it), some glass panes, some stone (not cobblestone), and a pressure plate made from either stone or wood - it doesn't matter which.
We'll start with the door. Wooden doors open with a right-click, but iron doors need to be fed a redstone current before they open. We're going to configure a computer so that it outputs a redstone current when you supply it with the correct password, and position it next to the door - something that looks a bit like this.
We're going to use this terminal as a dedicated door-opener, so you might want to craft another one if you want to keep it passworded for a while. The computer can't do other things while you're running your password program, so having multiple terminals is handy.
Once you've got that set up (remember to set up a disk drive behind the computer and put a disk in it if you want to save the program), right-click the computer to bring up the interface. Then type "edit startup". What we're doing here is editing the code that executes when the computer starts up.
In that code, we want to perform the following steps:
1) Ask for a password
2) Check the entered text against the real password
3) If it's right, open the door
4) If it's wrong, don't open the door
Turning that into code you get the following:
while true do
if input == "rpsrules" then
print("Password correct! Entry granted.")
print ("Password incorrect.")
Type that in, and while you're typing it, here's what each line means:
while true do - this basically says "keep doing this until you're told to stop"
term.clear() - clears the screen
term.setCursorPos(1, 1) - sets the cursor in the top left
print("Enter password:") - display "Enter password"
local input=read("*") - accept an input, but display it as asterisks
if input == "rpsrules" then - if the input is the word "rpsrules" then…
print("Password correct! Entry granted.") - Tell the user they got it right
redstone.setOutput("right", true) - output an on signal to the right (to open the door)
sleep(3) - wait three seconds
redstone.setOutput("right", false) - output an off signal to the right (to close the door)
else - if there's any other word entered
print ("Password incorrect.") - Tell the user they got it wrong
sleep(2) - wait two seconds
end - ends the "if" loop
end - ends the "while" loop
With that sorted, hit control, save, then ctrl, right arrow, enter to exit the editor and type "reboot". With any luck, it'll prompt you for a password, and if you type "rpsrules" in, open the door. If it doesn't, check your code very carefully against what I typed above to make sure there aren't any mistakes.
Done? Excellent! You now know how to ask the user for input, check to see if that matches something, and if it does, send a redstone signal.
Let's invert that and tell the computer to do something when it receives a redstone signal instead. We'll build a security system that alerts you when an intruder shows up. For this, you'll need some monitors. Build those with a pane of glass surrounded by stone.
Find a cave, clearing in a forest, or a flat hilltop for this one, and again - make sure the area's safe so you don't get interrupted by creepers. Make two, and position them so they're touching your computer.
You'll also need to set up a trigger pad. In your base, you'll want to stick this in a chokepoint so anyone coming in *has* to tread on it, but for now, just put it nearby. Hook it up to your computer using wires from a mod like RedPower, included in both modpacks mentioned above, or just redstone dust. If it's working, it should light up when you step on the pad. For fun, those with the IndustrialCraft 2 mod installed can also hook up an industrial alarm or howler alarm to the same circuit.
When you're set up, it should look something like this:
In this program we want the code to:
1) Wait for a redstone input
2) When it gets one, print something on the monitors
3) Reset after a few seconds
Here's the code for that:
print("Intruder alarm 1.0 running.")
print("Hold ctrl-T to quit.")
while true do
if redstone.getInput("left") then
And while you're typing that into your computer, here's what each line means.
print("Intruder alarm 1.0 running.") - Print some status text to the screen
print("Hold ctrl-T to quit.") - Explain how to end the program
monitor=peripheral.wrap("top") - Look for a screen attached to the top of your computer, and call it "monitor"
while true do - Keep doing the following until told to stop
sleep(0) - This gives the computer a quick break, which is important so the program doesn't crash with a "Too long without yielding" error
if redstone.getInput("left") then - If the computer detects a redstone input from the left then…
term.redirect(monitor) - Redirect output to the monitor
term.setCursorPos(1, 1) - Move the cursor to the top-left
monitor.setTextScale(2) - Double the size of the text for MAXIMUM IMPACT
print("Intruder!") - Print a warning to the monitor
sleep(5) - Wait five seconds
term.clear() - Clear the screen
term.restore() - Move the output back to the terminal
end - End the if loop
end - End the while loop
You might need to customise this a bit to suit your particular situation - change line three if your monitor isn't attached to the top of your computer, and line six if the pressure pad input isn't coming in to the left face of the computer. You can also customise any of the text that you want - take some time to fiddle about and make the program your own. If something doesn't work, just change it back to what's above and go from there.
Lastly, let's use what we've learnt about outputting text to a monitor to make a simple digital clock that will show you the in-game time. Build another computer, disk drive and a pair of monitors (or just use your intruder alarm machine if you prefer) and place them in the world, roughly as before.
In this program, we want to:
1) Get the time from the server
2) Format it nicely into hours and minutes
3) Print it to the screen
The code for that runs:
print("Clock 1.0 now running.")
print("Hold ctrl-T to quit.")
while true do
You'll notice a lot of similarities between this and the previous code, but there's a couple of new bits too - which I'll explain as you type it in:
print("Clock 1.0 now running.") - Print a status message
print("Hold ctrl-T to quit.") - Tell the user how to exit
monitor=peripheral.wrap("top") - Find the monitor on the top of the computer (again, change this if yours is elsewhere)
while true do - Do the following until told to stop
sleep(0) - Error-crushing break for the computer
term.redirect(monitor) - Send output to the monitor
term.clear() - Clear the monitor
time=textutils.formatTime(os.time()) - Get the time, and format it nicely
x,y=monitor.getSize() - Find the size of the monitor
term.setCursorPos(x/2-2, y/2) - Put the cursor just to the left of the centre of the monitor
print(time) - Print the time to the monitor
term.restore() - Send output back to the terminal
end - End the while loop
That's where I'm going to leave you this week, and here's a challenge for you. See if you can combine the last two programs together, so that the clock displays normally, but is replaced with a notification if an intruder enters the base, then goes back to the clock again after a few seconds. You've got all of the code you need, you just need to combine it in the right way.
Best of luck. Next week, we'll be dancing with turtles.