Advertisement
QQII

Enchanting Turtle V0.2

Mar 18th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Enchanting Turtle V0.2 by QQII
  2. --Declerations
  3. local enchantLevel = 0              --The level the turtle initially attemps to perform an enchant at
  4. local enchants = 0                  --The number of enchants performed by the turtle
  5. local slot = 1                      --The slot number that the items to be enchanted are in
  6.    
  7. --Functions
  8. function wrapPeripheral(thing)
  9.     local Sides = {
  10.                     "left","right",
  11.                     "top","bottom",
  12.                     "front","back",
  13.                     }
  14.  
  15.     for i,v in pairs(Sides) do
  16.         if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
  17.             print("'"..thing.."' found on side '"..v.."'.")
  18.             return peripheral.wrap(v), v
  19.         end
  20.     end
  21.  
  22.     print("ERROR: '"..thing.."' not found!")
  23.     return nil, nil
  24. end
  25.  
  26. local function toLevel(experience)
  27.     if experience <= 272 then
  28.         return experience / 17
  29.     elseif experience > 272 and experience <= 825 then
  30.         return 9.84 + 0.17 * math.sqrt(24 * experience - 5159)
  31.     else
  32.         return 21.6 + 0.075 * math.sqrt(56 * experience - 32511)
  33.     end
  34. end
  35.  
  36. local function clearSlot(slotNumber)    --Attemps to clear the slot slotNumber
  37.     if turtle.getItemCount(slotNumber) ~= 0 then
  38.         turtle.select(slotNumber)
  39.         if not turtle.drop() then
  40.             print("ERROR: failure to clear slot number " .. slotNumber)
  41.         end
  42.     end
  43. end
  44.  
  45. local function clearSlots()             --Places all the items into the chest under the turtle
  46.     for i = 1,16 do
  47.         if turtle.getItemCount(i) ~= 0 then
  48.             clearSlot(i)
  49.         end
  50.     end
  51. end
  52.  
  53. local function getItem()                --Attempts to take something from the chest above the turtle
  54.     clearSlots()
  55.     turtle.select(slot)
  56.     if not turtle.suck() then
  57.         print("ERROR: failure to obtain item to enchant")
  58.     end
  59.     if turtle.getItemCount(slot) ~= 1 then
  60.         if not turtle.drop(turtle.getItemCount(slot) - 1) then
  61.             print("ERROR: failure to reduce slot number " .. slot .. " to one item")
  62.         end
  63.     end
  64. end
  65.  
  66. local function enchant()                --Attemps to enchant the item
  67.     turtle.select(slot)
  68.     if xp.enchant(enchantLevel) then
  69.         enchants = enchants + 1
  70.         print("Enchants Performed: " .. enchants)
  71.     end
  72.     clearSlots()
  73. end
  74.  
  75. --Wrap peripherals
  76. local xp = wrapPeripheral("xp")
  77.     if (not xp) then
  78.         print("ERROR: xp peripheral is necessary")
  79.         os.shutdown()
  80.     end
  81. xp.setAutoCollect(true)             --Sets the turtle to automatically collect xp
  82.  
  83. --User Inputs
  84. print("The turtle will attack mobs above it.")
  85. print("It will look for items to enchant in the inventory infront of it.")
  86. print("It will place enchanted items in an inventory below it.")
  87. io.write("Level to Enchant: ")
  88.     if enchantLevel <= 0 then
  89.         while enchantLevel <= 0 do
  90.             enchantLevel = tonumber(read())
  91.         end
  92.     else
  93.         print(enchantLevel)
  94.     end
  95.    
  96. --Main Script
  97. while true do
  98.     while not turtle.attackUp() do
  99.         xp.collect()
  100.     end
  101.     if xp.getLevels() >= enchantLevel then
  102.         getItem()
  103.         enchant()
  104.     end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement