Advertisement
Wyvern67

EnderPouchManager: Turtle's script

Jul 17th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. deposit = peripheral.wrap("top")
  2. ender = peripheral.wrap("front")
  3. while ender == nil do
  4.     turtle.turnRight()
  5.     ender = peripheral.wrap("front")
  6. end
  7. lastSlot = ender.getInventorySize()
  8.  
  9. function set(channel, data)
  10.     -- Function originally coded by ilgazzi
  11.     data = tostring(data)
  12.     url = "http://craft-a-cloud.mooo.com/upload.php?name="..channel.."&data="..data
  13.     encoded=textutils.urlEncode(url)
  14.     http.request(url)
  15.     local i=0
  16.     while get(channel) ~= data do
  17.         if i >= 20 then
  18.             return false
  19.         end
  20.         sleep(0.25)
  21.         i = i+1
  22.     end
  23.     return true
  24. end
  25.  
  26. function get(channel)
  27.     -- Function originally coded by ilgazzi
  28.     url = "http://craft-a-cloud.mooo.com/download.php?name="..channel
  29.     encoded=textutils.urlEncode(url)
  30.     response = ""
  31.     while response == "" do
  32.         http.request(url)
  33.         event, returnurl, response = os.pullEvent("http_success")
  34.         response = response.readAll()
  35.     end
  36.     return response
  37. end
  38.  
  39. function suckSlot(chest, slot, qty)
  40.     chest.swapStacks(1,slot)
  41.     if not qty then
  42.         local ret = turtle.suck()
  43.     else
  44.         local ret = turtle.suck(qty)
  45.     end
  46.     chest.swapStacks(1,slot)
  47.     return ret
  48. end
  49. turtle.suckSlot = suckSlot
  50.  
  51. function suckSlotUp(chest, slot, qty)
  52.     chest.swapStacks(1,slot)
  53.     if not qty then
  54.         local ret = turtle.suckUp()
  55.     else
  56.         local ret = turtle.suckUp(qty)
  57.     end
  58.     chest.swapStacks(1,slot)
  59.     return ret
  60. end
  61. turtle.suckSlotUp = suckSlotUp
  62.  
  63. function split(str,splitter)
  64.     if not splitter then return end
  65.     if not str then return end
  66.     words = {}
  67.     i=0
  68.     for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
  69.         i=i+1
  70.         words[i] = part
  71.     end
  72.     return words
  73. end
  74. string.split = split
  75.  
  76. function searchForItem(chest, itemRequest)
  77.     inv = chest.getAllStacks()
  78.     if #inv <= 0 then
  79.         return false, "Inventory is empty"
  80.     end
  81.     for i=1,#inv do
  82.         inv[i] = inv[i].all()
  83.         if type(inv[i]) == "table" then
  84.             if string.lower(inv[i].display_name) == itemRequest then
  85.                 return i
  86.             end
  87.         end
  88.     end
  89.     return false, "Item not found"
  90. end
  91.  
  92. while true do
  93.     slot = ender.getStackInSlot(lastSlot)
  94.     if type(slot) == "table" then
  95.         suckSlot(ender, lastSlot)
  96.         turtle.dropUp()
  97.     else
  98.         commands = get("action")
  99.         commands = string.split(commands, " ")
  100.         if type(commands) == "table" then
  101.             if commands[1] == "nil" then
  102.                 sleep(1)
  103.             else
  104.                 set("action", "nil")
  105.                 print(commands[1])
  106.                
  107.                 if commands[1] == "store" then -- ##store
  108.                     itemRequest = string.lower(string.gsub(commands[2], "_", " "))
  109.                     print("Storing "..itemRequest)
  110.                     slot,sError = searchForItem(ender, itemRequest)
  111.                     if not sError then
  112.                         turtle.suckSlot(ender, slot, tonumber(commands[3]))
  113.                         turtle.dropUp()
  114.                         print("Success.")
  115.                         set("return", true)
  116.                     else
  117.                         print("Error: "..sError)
  118.                         set("return", false)
  119.                     end
  120.                 elseif commands[1] == "get" then -- ##get
  121.                     itemRequest = string.lower(string.gsub(commands[2], "_", " "))
  122.                     print("Pulling "..itemRequest)
  123.                     slot,sError = searchForItem(deposit, itemRequest)
  124.                     if not sError then
  125.                         turtle.suckSlotUp(deposit, slot, tonumber(commands[3]))
  126.                         turtle.drop()
  127.                         print("Success.")
  128.                         set("return", true)
  129.                     else
  130.                         print("Error: "..sError)
  131.                         set("return", false)
  132.                     end
  133.                 elseif commands[1] == "charge" then -- ##charge
  134.                     itemRequest = string.lower(string.gsub(commands[2], "_", " "))
  135.                     print("Charging "..itemRequest)
  136.                     slot,sError = searchForItem(ender, itemRequest)
  137.                     if not sError then
  138.                         turtle.suckSlot(ender, slot)
  139.                         turtle.turnRight()
  140.                         turtle.turnRight()
  141.                         turtle.drop()
  142.                         sleep(10)
  143.                         turtle.suck()
  144.                         turtle.turnLeft()
  145.                         turtle.turnLeft()
  146.                         turtle.drop()
  147.                         print("Success.")
  148.                         set("return", true)
  149.                     else
  150.                         print("Error: "..sError)
  151.                         set("return", false)
  152.                     end
  153.                 else
  154.                     set("return", false)
  155.                 end
  156.                 sleep(1)
  157.             end
  158.         end
  159.     end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement