IMarvinTPA

Enchanter

Mar 4th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.34 KB | None | 0 0
  1. --pastebin get nAZejNMM enchanter
  2.  
  3. --shell.run("enchanter t d rback 10 30")
  4. --shell.run("enchanter t b")
  5. --shell.run("enchanter t rf")
  6.  
  7. local tArgs = { ... }
  8. local sXPInput, sInput, sOutput, nDelay, nLevel = tArgs[1], tArgs[2], tArgs[3], tArgs[4], tArgs[5]
  9. local filename = shell.getRunningProgram()
  10.  
  11. local function fixSide(aSide)
  12.     if aSide == nil then
  13.         aSide = ""
  14.     end
  15.     aSide = string.lower(aSide)
  16.     if aSide == "top" or aSide == "t" or aSide == "up" or aSide == "u"then
  17.         aSide = "t"
  18.     elseif aSide == "front" or aSide == "f" then
  19.         aSide = "f"
  20.     elseif aSide == "bottom" or aSide == "b" or aSide == "down" or aSide == "d" then
  21.         aSide = "b"
  22.     elseif aSide == "none" or aSide == "n" then
  23.         aSide = "n"
  24.     elseif aSide == "rtop" or aSide == "rt" or aSide == "rup" or aSide == "ru"then
  25.         aSide = "rt"
  26.     elseif aSide == "rfront" or aSide == "rf" then
  27.         aSide = "rf"
  28.     elseif aSide == "rbottom" or aSide == "rb" or aSide == "rdown" or aSide == "rd" then
  29.         aSide = "rd"
  30.     elseif aSide == "rright" or aSide == "rr" then
  31.         aSide = "rr"
  32.     elseif aSide == "rleft" or aSide == "rl" then
  33.         aSide = "rl"
  34.     elseif aSide == "rback" or aSide == "rbk" then
  35.         aSide = "rb"
  36.     else
  37.         aSide = nil
  38.     end
  39.     return aSide
  40. end
  41. sXPInput = fixSide(sXPInput)
  42. sInput = fixSide(sInput)
  43. sOutput = fixSide(sOutput)
  44.  
  45. if sXPInput == nil or sInput == nil or sOutput == nil then
  46.     print( "Usage: " .. filename .. " <input XP side> <input side> <output side> [Delay Time] [Enchant Level]" )
  47.     print( "Side values are Top, Bottom, Front, and None" )
  48.     print( "The turtle will pull from the input side and push into the output side." )
  49.     print( "None can be specified if the turtle is expected to deal with items moving on their own." )
  50.     return
  51. end
  52.  
  53. if nDelay ~= nil then
  54.     nDelay = tonumber(nDelay)
  55. end
  56.  
  57. if nLevel ~= nil then
  58.     nLevel = tonumber(nLevel)
  59. end
  60.  
  61. if nDelay == nil then
  62.     nDelay = 0.5
  63. end
  64.  
  65. if nLevel == nil then
  66.     nLevel = 30
  67. end
  68.  
  69. if nLevel > 30 then
  70.     nLevel = 30
  71. elseif nLevel < 1 then
  72.     nLevel = 1
  73. end
  74.  
  75.  
  76. local function displaySide(aSide)
  77.     if aSide == nil then
  78.         aSide = ""
  79.     end
  80.     aSide = string.lower(aSide)
  81.     if aSide == "t" then
  82.         aSide = "top"
  83.     elseif aSide == "f" then
  84.         aSide = "front"
  85.     elseif aSide == "b" then
  86.         aSide = "bottom"
  87.     elseif aSide == "n" then
  88.         aSide = "none"
  89.     elseif aSide == "rt" then
  90.         aSide = "redstone pulse top"
  91.     elseif aSide == "rf" then
  92.         aSide = "redstone pulse front"
  93.     elseif aSide == "rd" then
  94.         aSide = "redstone pulse down"
  95.     elseif aSide == "rr" then
  96.         aSide = "redstone pulse right"
  97.     elseif aSide == "rl" then
  98.         aSide = "redstone pulse left"
  99.     elseif aSide == "rb" then
  100.         aSide = "redstone pulse back"
  101.     else
  102.         aSide = "unknown (" .. aSide .. ")"
  103.     end
  104.     return aSide
  105. end
  106.  
  107. local function isEmpty()
  108.     local bFull = true
  109.     local nTotalItems = 0
  110.     for n=1,16 do
  111.         local nCount = turtle.getItemCount(n)
  112.         if nCount == 0 then
  113.                 bFull = false
  114.         end
  115.         nTotalItems = nTotalItems + nCount
  116.     end
  117.          
  118.     if nCount == 0 then
  119.         --print( "No more Items to drop." )
  120.         return true
  121.     end
  122.     return false
  123. end
  124.  
  125. local function flashSide(aSide)
  126.     --print("Flash " .. aSide .. " On ")
  127.     redstone.setOutput(aSide, true)
  128.     sleep(0.2)
  129.     --print("Flash " .. aSide .. " Off ")
  130.     redstone.setOutput(aSide, false)
  131.     sleep(0.2)
  132. end
  133.  
  134. local function unload()
  135.     if sOutput == "n" then
  136.         return
  137.     end
  138.     --print( "Unloading items..." )
  139.  
  140.     for n=1,1 do
  141.         --unloaded = unloaded + turtle.getItemCount(n)
  142.         if (turtle.getItemCount(n) > 0) then
  143.             turtle.select(n)   
  144.             if sOutput == "t" then
  145.                 turtle.dropUp()
  146.             elseif sOutput == "f" then
  147.                 turtle.drop()
  148.             elseif sOutput == "b" then
  149.                 turtle.dropDown()
  150.             elseif sOutput == "rt" then
  151.                 flashSide("top")
  152.             elseif sOutput == "rd" then
  153.                 flashSide("bottom")
  154.             elseif sOutput == "rf" then
  155.                 flashSide("front")
  156.             elseif sOutput == "rb" then
  157.                 flashSide("back")
  158.             elseif sOutput == "rl" then
  159.                 flashSide("left")
  160.             elseif sOutput == "rf" then
  161.                 flashSide("right")
  162.             end
  163.            
  164.         end
  165.     end    
  166. end
  167.  
  168. local function load()
  169.     if sInput == "n" then
  170.         return
  171.     end
  172.  
  173.     --print( "Loading items..." )
  174.  
  175.     turtle.select(1)
  176.     n=1
  177.         --unloaded = unloaded + turtle.getItemCount(n)
  178.         if (turtle.getItemCount(n) == 0) then          
  179.  
  180.             if sInput == "t" then
  181.                 res = turtle.suckUp(1)
  182.             elseif sInput == "f" then
  183.                 res = turtle.suck(1)
  184.             elseif sInput == "b" then
  185.                 res = turtle.suckDown(1)
  186.             elseif sInput == "rt" then
  187.                 flashSide("top")
  188.                 turtle.select(n)
  189.                 res = (turtle.getItemCount(n) ~= 0)
  190.             elseif sInput == "rd" then
  191.                 turtle.select(n)
  192.                 flashSide("bottom")
  193.                 res = (turtle.getItemCount(n) ~= 0)
  194.             elseif sInput == "rf" then
  195.                 turtle.select(n)
  196.                 flashSide("front")
  197.                 res = (turtle.getItemCount(n) ~= 0)
  198.             elseif sInput == "rb" then
  199.                 turtle.select(n)
  200.                 flashSide("back")
  201.                 res = (turtle.getItemCount(n) ~= 0)
  202.             elseif sInput == "rl" then
  203.                 turtle.select(n)
  204.                 flashSide("left")
  205.                 res = (turtle.getItemCount(n) ~= 0)
  206.             elseif sInput == "rf" then
  207.                 turtle.select(n)
  208.                 flashSide("right")
  209.                 res = (turtle.getItemCount(n) ~= 0)
  210.             end
  211.            
  212.             if res == false then
  213.                 --print( "No more Items to get." )
  214.                 return false
  215.             end
  216.             --sleep(60)    
  217.     end
  218.     return true
  219. end
  220.  
  221. print( "Enchanting using XP from " .. displaySide(sXPInput) .. " and items from " .. displaySide(sInput) .. " at level " .. nLevel .. " sending to " .. displaySide(sOutput) .. " with a delay of " .. nDelay .. " seconds." )
  222.  
  223. --print("Left: " .. peripheral.getType("left"))
  224. --print("Right: " .. peripheral.getType("right"))
  225. local xp
  226. if peripheral.getType("left") == "xp" then
  227.     xp = peripheral.wrap("left")
  228. elseif peripheral.getType("right") == "xp" then
  229.     xp = peripheral.wrap("right")
  230. end
  231.  
  232. local function getXP()
  233.     if sXPInput == "f" then
  234.         xp.get()
  235.     elseif sXPInput == "t" then
  236.         xp.getUp()
  237.     elseif xpInput == "b" then
  238.         xp.getDown()
  239.     end
  240. end
  241.  
  242. while true do
  243.     getXP()
  244.     xp.collect()
  245.     print("Levels: " .. xp.getLevels())
  246.     if xp.getLevels() >= nLevel then
  247.         if turtle.getItemCount(1) + turtle.getItemCount(2) == 0 then
  248.             load()     
  249.         end
  250.         if turtle.getItemCount(1) > 1 then
  251.             turtle.select(1)
  252.             turtle.transferTo(2, turtle.getItemCount(1) - 1)
  253.         end
  254.         if turtle.getItemCount(1) == 0 and turtle.getItemCount(2) > 0 then
  255.             turtle.select(2)
  256.             turtle.transferTo(1, 1)
  257.         end
  258.         turtle.select(1)
  259.         xp.enchant(nLevel)
  260.         unload()       
  261.     end
  262.     sleep(nDelay)
  263. end
Advertisement
Add Comment
Please, Sign In to add comment