–CODE AT BOTTOM–
I recently began coding in LUA to automate aspects of Minecraft. I started with using someone else's door lock. After creating my own programs to automate my farms, I decided I wanted to write my own door lock.
The lock I had opened the door for a few seconds when you entered the right password. I started working on a program that would accept multiple inputs (one to open, close, reboot, etc.). I wanted it to act as a switch that requires a password. I play on a very small, personal server so leaving my door open when I'm working is quite a convenience and comes with little risk.
I thought I had it figured out until I started testing what would happen when I would log out and come back to the computer.
Here is what happens sequentially:
Spoiler
Log In.No Computer. Door Closed.
Placed Computer. Door Closed.
Input 123. Door Open.
Input qwe. Door Closed.
Input asd (Reboot). Door Closed.
Log Out.
Log In.
Input 123. Door Open.
Input qwe. Door Closed.
Input asd (Reboot). Door Closed.
Input 123. Door Open.
Log Out.
Log In.
–Door is currently open–
Input qwe. Door Open.
–Door didn't close–
Input 123. Door Open.
Input qwe. Door Closed.
When I relog and try to close the door, it won't. When I relog (and its open) and try to reboot, it does reboot (not shown above). What gives? I am guessing it has something to do with the fact that the chunk gets unloaded when I leave, the redstone signal gets confused and doesn't reset properly. Unfortuantely, I can't tell from the API. I think this is an indicator to a bigger problem involving with the logic of my program.
I would also like a few pointers on lexical conventions. I learned to program on Java and have played with a few other languages, like C++, C#, HTTP, CSS. I am just applying what I already know to LUA; however, I'm sure there's a more accepted manner. I am assuming pastebin links are better for large amounts of code and simply copy/pastes are for small amounts in the forums. I'll just do both.
I have access to the server files (I own/run the server) so I edit my specific computer's program files directly with TextPad through FTP. I have also labeled my computer and labeled the program "startup".
If it is a matter of chunk loading and it can be corrected by keeping the chunk loaded, I can easily do this with the ChickenChunks.
Hopefully its not something small that I've missed! >.<
Thanks!
Brandon
Code:
Spoiler
--[[ Password Enabled Door Switch
Function: Receives input to open or close door
and remains in resulting state
until nexy input.
Also receives input to reboot computer.
No local variables.
]]--
os.pullEvent = os.pullEventRaw --Avoid termination
while true do --Keep it running always
term.clear() --Clear the screen
term.setCursorPos(1, 1) --Reset cursor to top left
print("Password:") --Prompt user
input = read("*") --Hide input with stars
if input == "123" then
redstone.setOutput("right", true) --Open Door
elseif input == "qwe" then
redstone.setOutput("right", false) --Close Door
elseif input == "asd" then --For easy code updating
term.clear()
term.setCursorPos(1, 1)
print("Rebooting...")
sleep(2)
os.reboot() --Reboot OS
end --End if statement
end --End while loop