Advertisement
Wyvern67

EnderRouter

Jul 22nd, 2015
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. shell.run("pastebin", "get", "jzpwtcvf", "dl")
  2. shell.run("dl", "ini")
  3. assert(os.loadAPI("ini"), "Couldn't download the INI api")
  4.  
  5. function split(str,splitter)
  6.     if not splitter then return end
  7.     if not str then return end
  8.     words = {}
  9.     i=0
  10.     for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
  11.         i=i+1
  12.         words[i] = part
  13.     end
  14.     return words
  15. end
  16. string.split = split
  17.  
  18. function log2(n)
  19.     return math.log(n)/math.log(2)
  20. end
  21.  
  22. function log(n,base)
  23.     return math.log(n)/math.log(base)
  24. end
  25.  
  26. function dropTo(side)
  27.     if side == "top" then
  28.         return turtle.dropUp()
  29.     elseif side == "bottom" then
  30.         return turtle.dropDown()
  31.     elseif side == "left" then
  32.         turtle.turnLeft()
  33.         ret = turtle.drop()
  34.         turtle.turnRight()
  35.         return ret
  36.     elseif side == "right" then
  37.         turtle.turnRight()
  38.         ret = turtle.drop()
  39.         turtle.turnLeft()
  40.         return ret
  41.     elseif side == "front" then
  42.         return turtle.drop()
  43.     elseif side == "back" then
  44.         turtle.turnRight()
  45.         turtle.turnRight()
  46.         ret = turtle.drop()
  47.         turtle.turnRight()
  48.         turtle.turnRight()
  49.         return ret
  50.     else
  51.         return false
  52.     end
  53. end
  54. turtle.dropTo = dropTo
  55.  
  56.  
  57.  
  58. -- Actual code
  59.  
  60. settings = ini.open("settings")
  61.  
  62. enderChest = peripheral.find("ender_chest")
  63. sides = peripheral.getNames()
  64. enderChestSide = sides[1]
  65. print("enderChest: "..enderChestSide)
  66. print("")
  67. if not enderChest then
  68.     error("Usage: Put an ender chest in front, under or on top of the turtle.")
  69. end
  70.  
  71. term.clear()
  72. term.setCursorPos(1,1)
  73.  
  74. inputChest = settings.read("Main", "InputChest")
  75. if inputChest == false then
  76.     inputChest = {}
  77.     inputChest[1],inputChest[2],inputChest[3] = enderChest.getColors()
  78.     settings.write("Main", "InputChest", table.concat(inputChest, ","))
  79. else
  80.     inputChest = string.split(inputChest, ",")
  81. end
  82. print("Input: "..table.concat(inputChest, ","))
  83.  
  84. setting = settings.read("Slots", "1")
  85. chestColors = {}
  86. if setting == false then
  87.     base1, base2, base3 = enderChest.getColors()
  88.     base1 = log2(base1)
  89.     base2 = log2(base2)
  90.     base3 = log2(base3)+1
  91. else
  92.     chestColors[1] = string.split(setting, ",")
  93.     base1 = chestColors[1][1]
  94.     base2 = chestColors[1][2]
  95.     base3 = chestColors[1][3]
  96.     base1 = log2(base1)
  97.     base2 = log2(base2)
  98.     base3 = log2(base3)
  99. end
  100.  
  101. slot = 1
  102. for i=base1,15 do
  103.     for j=base2,15 do
  104.         for k=base3,15 do
  105.             if slot > 27 then
  106.                 i = 16
  107.                 j = 16
  108.                 k = 16
  109.                 break
  110.             end
  111.             chestColors[slot] = {}
  112.             chestColors[slot][1] = 2^(i)
  113.             chestColors[slot][2] = 2^(j)
  114.             chestColors[slot][3] = 2^(k)
  115.             --textutils.pagedTabulate(chestColors[slot])
  116.             --sleep(0.25)
  117.             slot = slot+1
  118.         end
  119.     end
  120. end
  121.  
  122. for slot=1,27 do
  123.     setting = settings.read("Slots", tostring(slot))
  124.     if not setting then
  125.         settings.write("Slots", tostring(slot), table.concat(chestColors[slot], ","))
  126.     else
  127.         chestColors[slot] = string.split(setting, ",")
  128.     end
  129. end
  130. settings.close("settings")
  131.  
  132. print("Settings done")
  133. enderChest.setColors(chestColors[1][1],chestColors[1][2],chestColors[1][3])
  134.  
  135. while true do
  136.     slots = enderChest.getAllStacks()
  137.     for i,v in pairs(slots) do
  138.         slots[i] = slots[i].all()
  139.         if type(slots[i]) == "table" then
  140. --          print("Slot "..i..": "..slots[i].name)
  141.             if i==1 then
  142.                 turtle.suck()
  143.             else
  144.                 enderChest.swapStacks(1,i)
  145.                 turtle.suck()
  146.                 enderChest.swapStacks(1,i)
  147.             end
  148.             enderChest.setColors(chestColors[i][1],chestColors[i][2],chestColors[i][3])
  149.             turtle.dropTo(enderChestSide)
  150.             enderChest.setColors(inputChest[1],inputChest[2],inputChest[3])
  151.         end
  152.     end
  153.     sleep()
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement