Advertisement
QQII

Enchanting Turtle V0.1

Mar 18th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. --Enchanting Turtle V0.1 by QQII
  2. --Badly attempt to find an optimum times to attack before enchanting
  3.  
  4. --Wrap peripherals
  5. local xp = peripheral.wrap("right")
  6.  
  7. --Declerations
  8. local attacks = 165         --The number of attacks it performs before attempting to enchant
  9. local enchantLevel = 0          --The level the turtle initially attemps to perform an enchant at
  10. local enchants = 0          --The number of enchants performed by the turtle
  11. local levels = 0            --The amount of levels collected for the number of attacks performed
  12. local slot = 1              --The slot number that the items to be enchanted are in
  13. xp.setAutoCollect(true)         --Sets the turtle to automatically collect xp
  14.  
  15. --User Inputs
  16. print("Make sure that the mobs fall ontop of the turtle.")
  17. print("Make sure that there is an inventory infront containing books or other items for enchating.")
  18. print("Make sure that there is an inventory below the turtle for it to place the items it has encanted.")
  19. print("Enter the level you want the turtle to enchant at:")
  20.     while enchantLevel <= 0
  21.         enchantLevel = tonumber(read())
  22.     end
  23.  
  24. --Functions
  25. local function clearSlot(slotNumber)            --Attemps to clear the slot slotNumber
  26.     turtle.select(slotNumber)
  27.     if turtle.getItemCount(slotNumber) ~= 0 then
  28.         if not turtle.dropDown() then
  29.             print("ERROR: failure to clear slot number " .. slotNumber)
  30.         end
  31.     end
  32. end
  33.  
  34. local function clearSlots()             --Places all the items into the chest under the turtle
  35.     for i = 1,16 do
  36.         clearSlot(i)
  37.     end
  38. end
  39.  
  40. local function getItem()                --Attemps to take something from the chest above the turtle
  41.     clearSlots()
  42.     turtle.select(slot)
  43.     if not turtle.suck() then
  44.         print("ERROR: failure to obtain item to enchant")
  45.     end
  46.     if turtle.getItemCount(slot) ~= 1 then
  47.         if not turtle.drop(turtle.getItemCount(slot) - 1) then
  48.             print("ERROR: failure to reduce slot number " .. slot .. " to one item")
  49.         end
  50.     end
  51. end
  52.  
  53. local function enchant()                --Attemps to enchant the item
  54.     turtle.select(slot)
  55.     if xp.enchant(enchantLevel) then
  56.         enchants = enchants + 1
  57.         print("Number of enchants performed by the turtle: " .. enchants)
  58.     else
  59.         turtle.drop()               --Replaces the items if it doesn't have enough levels
  60.     end
  61.     clearSlots()
  62. end
  63.  
  64. --Main Script
  65. while true do
  66.     levels = xp.getLevels()
  67.    
  68.     for i = 1,attacks do
  69.         while not turtle.attackUp() do end
  70.     end
  71.    
  72.     xp.collect()
  73.     levels = xp.getLevels() - levels
  74.     print("Gained " .. levels .. " for " .. attacks .. " attacks.")
  75.     if levels >= enchantLevel then          --Assumes all mobs that drop xp (for fine tuning)
  76.         attacks = attacks - 1
  77.     elseif levels < enchantLevel then       --Assumes a constant flow of 1 hit kill mobs that drop 5 xp each
  78.         attacks = attacks + 5 * (enchantLevel - levels)
  79.     end
  80.     print("Turtle will now attack " .. attacks .. " times before attempting to enchant")
  81.    
  82.     getItem()
  83.     enchant()
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement