Advertisement
nagoL2015

hopper.lua

Jul 26th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. modem1 = 6565
  2. modem2 = 6566
  3. modem3 = 6567
  4. modem4 = 6568
  5.  
  6. local channel = 3000
  7.  
  8. if fs.exists(".channel") then
  9.     local f = fs.open(".channel","r")
  10.     local n = f.readLine()
  11.     if not tonumber(n) then error("contents of .channel is not a number") end
  12.     channel = tonumber(n)
  13.     f.close()
  14. end
  15.  
  16. os.setComputerLabel("Hopper | "..channel)
  17.  
  18. local modems = {peripheral.find("modem")}
  19.  
  20. if #modems == 0 then error("no modems attached") end
  21. local modem = modems[1]
  22.  
  23. modem.open(channel)
  24.  
  25. while turtle.forward() do end
  26.  
  27. function normalOperation()
  28.     while true do
  29.         local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
  30.         if chnl == channel then
  31.             while turtle.back() do end
  32.             while turtle.forward() do
  33.                 local b,insp = turtle.inspectUp()
  34.                 if b and (insp.name == "minecraft:chest" or insp.name == "minecraft:trapped_chest") then
  35.                     while turtle.suckUp() and turtle.getItemCount(16) == 0 do turtle.select(1) end
  36.                 end
  37.             end
  38.             for slot = 1,16 do
  39.                 if turtle.getItemCount(slot) ~= 0 then
  40.                     turtle.select(slot)
  41.                     turtle.drop()
  42.                 end
  43.             end
  44.         end
  45.     end
  46. end
  47.  
  48. function modemListener()
  49.     while true do
  50.         local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
  51.         if chnl == modem1 then
  52.             local x,y,z = gps.locate()
  53.            
  54.             local inv = {}
  55.            
  56.             for slot = 1,16 do
  57.                 local detail = turtle.getItemDetail(slot)
  58.                 if detail == nil then
  59.                     inv[slot] = {}
  60.                 else
  61.                     inv[slot] = detail
  62.                 end
  63.             end
  64.            
  65.             modem.transmit(rChnl,chnl,{type="infoHopperResponse",fuel=turtle.getFuelLevel(),location={x=x,y=y,z=z},inventory=inv,channel=channel})
  66.             print("Sent info")
  67.         end
  68.     end
  69. end
  70.  
  71. modem.open(modem1)
  72. parallel.waitForAny(normalOperation,modemListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement