nSun

CC BowSorter

Aug 22nd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local slot = {
  2.     "input", --1
  3.     "bow",
  4.     "unbreaking_1", --3
  5.     "unbreaking_2", --4
  6.     "unbreaking_3", --5
  7.     "punch_1",      --6
  8.     "power_1",      --7
  9.     "power_2",      --8
  10.     "power_3",      --9
  11. }
  12.  
  13. local function isChestCasual(data)
  14.     return data
  15.     and data.name=="minecraft:chest"
  16.     and data.state.facing=="north"
  17. end
  18. local function isChestUsual(data)
  19.     return data
  20.     and data.name=="minecraft:chest"
  21.     and data.state.facing=="east"
  22. end
  23. local function isChestUnusual(data)
  24.     return data
  25.     and data.name=="minecraft:chest"
  26.     and data.state.facing=="south"
  27. end
  28.  
  29. local function facingChestCasual()
  30.     local test,data=turtle.inspect()
  31.     if isChestCasual(data) then return end
  32.     if isChestUsual(data) then return turtle.turnLeft() end
  33.     if isChestUnusual(data) then
  34.         turtle.turnRight()
  35.     end
  36.     turtle.turnRight()
  37. end
  38. local function facingChestUsual()
  39.     local test,data=turtle.inspect()
  40.     if isChestUsual(data) then return end
  41.     if isChestUnusual(data) then return turtle.turnLeft() end
  42.     if not test then
  43.         turtle.turnRight()
  44.     end
  45.     turtle.turnRight()
  46. end
  47. local function facingChestUnusual()
  48.     local test,data=turtle.inspect()
  49.     if isChestUnusual(data) then return end
  50.     if not test then return turtle.turnLeft() end
  51.     if isChestCasual(data) then
  52.         turtle.turnRight()
  53.     end
  54.     turtle.turnRight()
  55. end
  56.  
  57. local function output(str,line)
  58.     term.setCursorPos(1,line)
  59.     term.clearLine()
  60.     term.write(str)
  61. end
  62.  
  63. local function compare()
  64.     turtle.select(1)
  65.     local s
  66.     for s=2,#slot do
  67.         if turtle.compareTo(s) then
  68.             return slot[s]
  69.         end
  70.     end
  71.     return false
  72. end
  73.  
  74. local function clean()
  75.     if #slot >= 16 then return end
  76.     local s
  77.     for s = (#slot+1),16 do
  78.         if turtle.getItemCount(s)>0 then
  79.             turtle.select(s)
  80.             turtle.transferTo(1)
  81.             turtle.select(1)
  82.             return
  83.         end
  84.     end
  85. end
  86.    
  87.  
  88. term.setCursorPos(1,1)
  89. term.clear()
  90. print(string.format("#%d %s",
  91.     os.getComputerID(),
  92.     os.getComputerLabel()))
  93.  
  94. local t = os.startTimer(.1)
  95.  
  96. while true do
  97.     turtle.select(1)
  98.     output("",3)
  99.     output("",4)
  100.     local e,p = os.pullEvent()
  101.     os.cancelTimer(t)
  102.     if e=="turtle_inventory" or turtle.getItemCount(1)>0 then
  103.         output("Manage inventory",2)
  104.         local test = compare()
  105.         output("manage inventory: "..(test and test or "unknown"),3)
  106.         if not test then
  107.             --unusual enchant
  108.             output("Unusual bow",4)
  109.             facingChestUnusual()
  110.             turtle.drop()
  111.         elseif test=="bow" then
  112.             --casual bow
  113.             output("Casual bow",4)
  114.             facingChestCasual()
  115.             turtle.drop()
  116.         else
  117.             --usual enchant
  118.             output("Usual enchanted bow",4)
  119.             facingChestUsual()
  120.             turtle.drop()
  121.         end
  122.     elseif e=="timer" and p==t then
  123.         --print time
  124.         output(string.format("running: %d",math.floor(os.clock())),2)
  125.     elseif e=="terminate" then
  126.         print("")
  127.     end
  128.     clean()
  129.     t = os.startTimer(1)
  130. end
Advertisement
Add Comment
Please, Sign In to add comment