Advertisement
rhn

SimpleItemTransfer

rhn
Mar 20th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. --Simple item transferrer from chosen inventory adjacent to computer, to/from another inventory adjacent to that.
  2. -- X Y
  3. -- C
  4. -- C: ComputerCraft computer adjacent to inventory X
  5. -- X,Y: Inventories adjacent to each other.
  6. -- Install script named "startup". First time run go through the options, selecting side of X is attached to C, which direction C is relative to X(ingame coordinate directions), pull or push mode, stack size and delay.
  7. -- Most efficient in push mode!
  8.  
  9.  
  10. sides={"top","bottom","right","left","front","back"}
  11. directions={"up","down","north","south","west","east"}
  12.  
  13. if (fs.exists("choices") == false) then --Check if this is first time run, no saved choices on file
  14. while true do --Asks for side for attached inventory
  15.         print("Please pick which side of computer has attached inventory.")
  16.         for i=1, #sides do
  17.             print(i..". "..sides[i])
  18.         end
  19.         local answer = read()
  20.         if tonumber(answer)==nill then
  21.             print("-Wrong answer, input not a number-")
  22.         else
  23.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  24.             if (0 < answerv and answerv <= 6) then
  25.                 if peripheral.isPresent(sides[answerv]) then
  26.                     side1=answerv
  27.                     --print("Side ", sides[side1], " chosen")
  28.                     break
  29.                 else
  30.                     print("-No adjacent inventory found at chosen side-")
  31.                 end
  32.             else
  33.                 print("-Wrong answer, input does not match number of sides-")
  34.             end
  35.         end
  36. end
  37. print("-------------------------------")
  38. while true do   --Ask for direction to push items from attached inventory
  39.         print("Please pick direction of secondary inventory.")
  40.         for i=1, #directions do
  41.             print(i..". "..directions[i])
  42.         end
  43.         local answer = read()
  44.         if tonumber(answer)==nill then
  45.             print("-Wrong answer, input not a number-")
  46.         else
  47.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  48.             if (0 < answerv and answerv <= 6) then
  49.                 side2=answerv
  50.                 --print("Direction ", directions[side2], " chosen")
  51.                 break
  52.             else
  53.                 print("-Wrong answer, input does not match number of directions-")
  54.             end
  55.         end
  56. end
  57. print("-------------------------------")
  58. while true do --Push or pull
  59.         print("Push or pull items?")
  60.         print("1. Push items (Recommended)")
  61.         print("2. Pull items")
  62.         local answer = read()
  63.         if tonumber(answer)==nill then
  64.             print("-Wrong answer, input not a number-")
  65.         else
  66.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  67.             if (0 < answerv and answerv <= 2) then
  68.                 operation=answerv
  69.                 break
  70.             else
  71.                 print("-Wrong answer, input does not match range-")
  72.             end
  73.         end
  74. end
  75. print("-------------------------------")
  76. if operation == 2 then
  77. while true do --pull inv size
  78.         print("Size of inventory to pull from?")
  79.         local answer = read()
  80.         if tonumber(answer)==nill then
  81.             print("-Wrong answer, input not a number-")
  82.         else
  83.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  84.             if (0 < answerv and answerv <= 64) then
  85.                 pullsize=answerv
  86.                 break
  87.             else
  88.                 print("-Wrong answer, input does not match range-")
  89.             end
  90.         end
  91. end
  92. end
  93. print("-------------------------------")
  94. while true do --Ask for stack size to move
  95.         print("Please pick stacksize(1-64) for each item transfer.")
  96.         print("(64 Recommended)")
  97.         local answer = read()
  98.         if tonumber(answer)==nill then
  99.             print("-Wrong answer, input not a number-")
  100.         else
  101.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  102.             if (0 < answerv and answerv <= 64) then
  103.                 stack=answerv
  104.                 --print("Stacksize ", stack, " chosen")
  105.                 break
  106.             else
  107.                 print("-Wrong answer, input does not match range of stacksize-")
  108.             end
  109.         end
  110. end
  111. print("-------------------------------")
  112. while true do   --Ask for delay between transfer cycle(cycle = one stack from each slot in inventory)
  113.         print("Please pick delay between transfer cycles in seconds.")
  114.         print("(0.5+, but higher recommended)")
  115.         local answer = read()
  116.         if tonumber(answer)==nill then
  117.             print("-Wrong answer, input not a number-")
  118.         else
  119.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  120.             if (0.5 <= answerv) then
  121.                 delay=answerv
  122.                 --print("Delay ", delay, " chosen")
  123.                 break
  124.             else
  125.                 print("-Wrong answer, delay too small. Do you WANT lag? This is how you get lag!!!-")
  126.             end
  127.         end
  128. end
  129. --Write choices to file
  130. local file = fs.open("choices","w")
  131. file.writeLine("side1="..tostring(side1))
  132. file.writeLine("side2="..tostring(side2))
  133. file.writeLine("stack="..tostring(stack))
  134. file.writeLine("delay="..tostring(delay))
  135. file.writeLine("operation="..tostring(operation))
  136. file.close()
  137.  
  138. print("-------------------------------")
  139. print("     / )")
  140. print("    ' /")
  141. print("---' (_____")
  142. print("       ((__)")
  143. print("    _ ((___)")
  144. print("     -'((__)")
  145. print("--.___((_)")
  146. print("-------------------------------")
  147. end
  148.  
  149. sleep(math.random()) --Random delay to stagger startup of many scripts on chunkload.
  150.  
  151. os.loadAPI("choices") -- Reads choices from file
  152. local side1 = choices.side1
  153. local side2= choices.side2
  154. local stack= choices.stack
  155. local delay= choices.delay
  156. local operation= choices.operation
  157.  
  158. print("Beginning item transfer with following settings:")
  159. print("Side of attached inventory to the: "..sides[side1])
  160. print("Direction of secondary inventory: "..directions[side2])
  161. if operation == 1 then
  162.     print("Pushing items from primary to secondary inventory")
  163. else
  164.     print("Pulling items from secondary to primary inventory")
  165. end
  166. print("Stack size of: "..stack)
  167. print("Cycle delay: "..delay.." second(s).")
  168.  
  169.  
  170. inv = peripheral.wrap(sides[side1])
  171. invsize = inv.size()
  172. while true do
  173.     if operation == 1 then
  174.         sleepcounter=0
  175.         for i=1,invsize do
  176.             if inv.getItemMeta(i) ~= nil then
  177.                 inv.pushItems(directions[side2],i,stack)
  178.                 sleep(0.1)
  179.             else
  180.                 sleepcounter=sleepcounter+1
  181.                 sleep(0.005)
  182.             end
  183.         end
  184.         if sleepcounter ==invsize then
  185.             --print("No work, sleeping 10s")
  186.             sleep(10)
  187.         end
  188.     elseif operation == 2 then
  189.         for i=1,pullsize do
  190.             inv.pullItems(directions[side2],i,stack)
  191.             sleep(0.1)
  192.         end
  193.     end
  194.     sleep(delay)
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement