Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Mobfarm, as written by Klazam
- --Section 1: Wrapping any availble peripherals
- --This section hooks up peripherals to the turtle itself, and gives them names for functions related to them
- anvil = peripheral.wrap("right")
- xp = peripheral.wrap("left")
- --Section 2: Setting up function library
- --This section is basically a library of "shortcuts", lines of code that you keep on repeating, and would like to shorten.
- --This function clears the screen and resets the cursor position
- function clear() --This line lets the program know you're setting up a function, and naming it. The () is required at the end of the name of that function.
- term.clear() --This clears the screen
- term.setCursorPos(1,1) --Sets the cursor positon to X,Y (1,1 is top left of the screen)
- end --Lets the program know the function is ended
- --This function waits until a specific key is pressed. (In most cases, the key is #28: enter. Refer to CC wiki (http://computercraft.info/wiki/File:CC-Keyboard-Charcodes.png)
- function rawread(x) -- The () at the end of a function can contain a variable or a string. Numberical variables (like this one will be) is written as such functionname(xyz) and string (letters) is written like this: functionname("xyz")
- while true do --This sets up an infinte loop- this function will keep on going until something stops it
- local sEvent, param = os.pullEvent("key") --Tells the turtle/computer to wait until a key is pressed, and assigns "key" to the param variable
- if sEvent == "key" then --Checks if "sEvent" matches "key". if this is true, do the next thing. if not, do something else. In this case there isnt anything else. So it'll end the function and loop back to the start, thanks to while true do.
- if param == x then --Checks if param is the value you assigned to x, earlier
- break --If the above two conditions are satifised, exit the loop.
- end
- end
- end
- end
- --This function waits until ANY key is pressed, and tells you what that key was.
- function keyread()
- while true do
- local sEvent, param = os.pullEvent("key")
- if sEvent == "key" then
- return(param) --The "return" term means the function, upon completion, will have a value assigned to it. You'll learn why shortly
- end
- break
- end
- end
- -- Section 3: The ACTUAL program the computer/turtle will run
- --Now for the program itself, now that we have set up an function library.
- --There are two parts to any program- one that runs once on activating the program, and one that constanly runs. Some programs may have only one or the other.
- --This section runs only once
- --This calls on peripheal's function, in this case the "xp" peripheal. (this command sets the turtle to always collect any nearby xp orbs in a 2 block radius)
- xp.setAutoCollect(true)
- --This section uses a loop ("while true do" sets up an infinite loop, which starts on activating this program) to run constanly after starting up
- while true do
- print("Hello! I am Merlin!\nYou can get items, enchant your items, anvil them!\nPress Enter to use my services.") --print prints a line of text to the screen. Use \n to have line breaks, and \t to have indentations
- rawread(28) -- this calls the function "rawread(x)" with x assigned to 28, which is the enter key. The purpose of this is to just have the program idle while waiting for input to proceed.
- clear() -- Calls the "clear()" function
- print("Press M to activate Mob Farm\nPress E to activate Enchanting Mode\nPress A to activate Anvil Mode\nPress B to activate Book Mode\nPress L to get Total Levels that I have")
- x = keyread() --Recall the return in the "keyread()" function? This is where it comes in. The program assigns a variable- x in this case- to whatever keyread() returned.
- clear()
- if x == 50 then --This checks if the key pressed is "M"
- print("Mob Farm Selector:\n\t1: Wither Skeleton\n\t2: Angry Zombie\n\t3: Skeleton\n\t4: Creeper\n\t5: Zombie Pigman\n\t6: Blaze\nPlease press the corresponding key to activate that Mob Grinder")
- x = keyread() --Reads the key again
- clear()
- if x == 2 then --Checks the key again, in this case looking for the key "1"
- rs.setBundledOutput("back",colors.white) --This turns the white cable in a bundled cable to "on"
- print("Wither Skeleton \nFarm initialized, press Enter to shut down \n\nYou WILL not be getting wither skull drops. \nSee Klazam33 if you want some. \Reason must be provided.")
- rawread(28) --Waits
- l = xp.getLevels() --Calls a peripheral function and assigns the value it returns to give you the variable "l"
- rs.setBundledOutput("back",0) --Turns off the whole bundled cable
- print("\nFarm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.") --Prints a line of text, with the variable "l" in it. Syntax is as follows: print("blah".. variable .. "blah")
- rawread(28)
- elseif x == 3 then --2
- rs.setBundledOutput("back",colors.black)
- print("Angry Zombie \nFarm initialized, press Enter to shut down")
- rawread(28)
- l = xp.getLevels()
- rs.setBundledOutput("back",0)
- print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
- rawread(28)
- elseif x == 4 then --3
- rs.setBundledOutput("back",colors.red)
- print("Skeleton \nFarm initialized, press Enter to shut down")
- rawread(28)
- l = xp.getLevels()
- rs.setBundledOutput("back",0)
- print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
- rawread(28)
- elseif x == 5 then --4
- rs.setBundledOutput("back",colors.yellow)
- print("Creeper \nFarm initialized, press Enter to shut down\n\nYou WILL not be getting gunpowder drops. \nSee Klazam33 if you want some. \Reason must be provided.")
- rawread(28)
- l = xp.getLevels()
- rs.setBundledOutput("back",0)
- print("\n Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
- rawread(28)
- elseif x == 6 then --5
- rs.setBundledOutput("back",colors.green)
- print("Zombie Pigman \nFarm initialized, press Enter to shut down")
- rawread(28)
- l = xp.getLevels()
- rs.setBundledOutput("back",0)
- print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
- rawread(28)
- elseif x == 7 then --6
- rs.setBundledOutput("back",colors.gray)
- print("Blaze \nFarm initialized, press Enter to shut down")
- rawread(28)
- l = xp.getLevels()
- rs.setBundledOutput("back",0)
- print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
- rawread(28)
- elseif x == 1 then -- Makes ESC key do nothing
- else --What if keyread() doesn't return a key that you want? Instead of just ending the program and looping back to the start, leaving you unsure if stuff worked, this section lets you know you pressed something wrong.
- print("Not valid input!\nPress Enter to return to main menu.")
- rawread(28)
- end
- elseif x == 38 then --L
- l = xp.getLevels()
- print("Levels I have:" .. l .. "\nPress Enter to return to main menu.")
- rawread(28)
- elseif x == 18 then --E
- print("Enchanting mode:\nPlease put item to be enchanted in Slot 1 \nPress Enter when ready to proceed.")
- rawread(28)
- if turtle.getItemCount(1) == 1 then --This checks how much items are in (Slot). In this case, it's slot 1.
- turtle.select(1) --Selects slot 1
- xp.enchant(30) --Calls a peripheral function and runs it.
- turtle.dropDown() --Drops item from selected slot, to the bottom of the turtle. Either it'll go on the ground or it'll go in an inventory such as an chest. Pipes will not work
- print("Done, please take item from chest.\nPress Enter to return to main menu.")
- rawread(28)
- else --If there's too many items in slot one or too few items, things would mess up, so this "else" lets you know theres something wrong and doesn't execute the program
- print("Too many items or not enough items!\nPress Enter to return to main menu.")
- rawread(28)
- end
- elseif x == 30 then --A
- print("Anvil mode:\nPlease put items to be repaired in Slot 2 and 3\nPress Enter when ready to proceed.")
- rawread(28)
- if turtle.getItemCount(2) == 1 and turtle.getItemCount(3) == 1 then -- You can have multiple conditional statements by linking them together with and, or, nor. and means when BOTH is true, do, or means when ONE is true, do, nor means when BOTH is NOT true, do
- turtle.select(2)
- anvil.repair(2,3)
- turtle.dropDown()
- print("Done, please take item from chest.\nPress Enter to return to main menu.")
- rawread(28)
- else
- print("Too many items or not enough items!\nPress Enter to return to main menu.")
- rawread(28)
- end
- elseif x == 48 then --B
- l = xp.getLevels()
- if l > 0 then --If l is greater than 0, then do
- y = math.floor(l / 30) --Some maths. math.floor(variable) rounds down numbers to the greatest whole number, instead of having decimals.
- else
- y = 0 --This skips the math part above. I dont want to risk a division by 0 error.
- end
- print("Book Mode:\nPlace books in slot 15\nEnsure slot 16 is empty\nMax books you can enchant at this time is " .. y .. "\nPress Enter when ready to proceed.")
- rawread(28)
- if turtle.getItemCount(15) > 0 and turtle.getItemCount(16) == 0 then
- print("Running program, please be patient.")
- repeat -- This is a loop that repeats until a specific condition is true
- turtle.select(15)
- turtle.transferTo(16,1) --Transfers items from selected slot to (slot,quantity)
- turtle.select(16)
- xp.enchant(30)
- turtle.dropDown()
- until turtle.getItemCount(15) == 0 --This is the condition in which the loop ends.
- print("Books done, and in chest under me. Please take them all with you.\nPress Enter to return to main menu.")
- rawread(28)
- else
- print("Not enough items or items in wrong slot!\nPress Enter to return to main menu.")
- rawread(28)
- end
- elseif x == 1 then
- else
- print("Not valid input!\nPress Enter to return to main menu.")
- rawread(28)
- end
- clear()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement