Advertisement
QQII

Enchanting Turtle V0.3

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