ToLazyToThink

enchanting turtle v0.2

Apr 7th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. local bookSlot = 1
  2. local enchantSlot = 16
  3.  
  4. local emptySlotsStart = 2
  5. local emptySlotsEnd = 15
  6.  
  7. local xpSide = "right"
  8. local grinderSide = "left"
  9. local manualSide  = "back"
  10.  
  11. local xp = peripheral.wrap(xpSide)
  12.  
  13. local enchantLevel = 30
  14. local levelLow = 60
  15. local levelHigh = 120
  16.  
  17.  
  18.  
  19.  
  20. function getBook()
  21.   if turtle.getItemCount(bookSlot) < 1 then
  22.     turtle.select(bookSlot)
  23.     turtle.suckUp()
  24.     clearInv()
  25.     if turtle.getItemCount(bookSlot) < 2 then
  26.       return false
  27.     end
  28.   end
  29.  
  30.   return true
  31. end
  32.  
  33. function setGrind(isGrind)
  34.   redstone.setOutput(grinderSide, isGrind)
  35. end
  36.  
  37. local lastLevel
  38. function checkXP()
  39.   local level = xp.getLevels()
  40.   if level ~= lastLevel then
  41.     print("level " .. level)
  42.   end
  43.  
  44.   lastLevel = level
  45.   if level <= levelLow then
  46.     setGrind(true)
  47.   elseif level >= levelHigh then
  48.     setGrind(false)
  49.   end
  50.  
  51.   return level >= enchantLevel
  52. end  
  53.  
  54. function clearInv()
  55.   for i = emptySlotsStart, emptySlotsEnd do
  56.     turtle.select(i)
  57.     turtle.dropDown()
  58.   end
  59.   if turtle.getItemCount(enchantSlot) > 0 then
  60.     turtle.select(enchantSlot)
  61.     if turtle.compareTo(bookSlot) then
  62.       turtle.dropUp()
  63.     else
  64.       turtle.dropDown()
  65.     end
  66.   end
  67.   turtle.select(bookSlot)
  68. end
  69.  
  70. function enchant()
  71.   turtle.select(bookSlot)
  72.   turtle.transferTo(enchantSlot, 1)
  73.   turtle.select(enchantSlot)
  74.   local good = xp.enchant(enchantLevel)
  75.   if good then
  76.     print("Enchant successful")
  77.   else
  78.     print("Enchant failed")
  79.   end
  80.   clearInv()
  81. end
  82.  
  83. function setManual(isManual)
  84.   if isManual then
  85.     setGrind(false)
  86.     xp.setAutoCollect(false)
  87.   else
  88.     xp.setAutoCollect(true)
  89.   end
  90. end
  91.  
  92. function isManualSignal()
  93.   return redstone.getInput(manualSide)
  94. end
  95.  
  96. function main()
  97.   clearInv()
  98.   while true do
  99.     if isManualSignal() then
  100.       setManual(true)
  101.       sleep(1)
  102.     else
  103.       setManual(false)
  104.       if checkXP() and getBook() then
  105.         enchant()
  106.       else
  107.         sleep(5)
  108.       end
  109.     end
  110.   end
  111. end
  112.  
  113. main()
Advertisement
Add Comment
Please, Sign In to add comment