Advertisement
Noobyhead99

sugarlanmerDepositTurtle

Oct 24th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. local lan
  4. local chest
  5.  
  6. for k,v in pairs(peripheral.getNames()) do
  7.   if peripheral.getType(v) == "LAN NIC" then
  8.     lan = peripheral.wrap(v)
  9.   elseif peripheral.getType(v) == "chest" then
  10.     chest = peripheral.wrap(v)
  11.   end
  12. end
  13.  
  14. if not lan then
  15.   print("[ERROR] " .. textutils.formatTime(os.time()) .. " - No LAN modem attached. The collection host will now terminate.")
  16.   return
  17. end
  18.  
  19. if not chest then
  20.   print("[ERROR] " .. textutils.formatTime(os.time()) .. " - No chest above turtle. The collection host will now terminate.")
  21.   return
  22. end
  23.  
  24. if turtle.getItemCount(16) == 0 then
  25.   print("[ERROR] " .. textutils.formatTime(os.time()) .. " - No item found in slot 16. Place a gold ingot in this slot for item comparison. The collection host will now terminate.")
  26.   return
  27. end
  28.  
  29. turtle.select(16)
  30.  
  31. print("[INFO] " .. textutils.formatTime(os.time()) .. " - The collection host is now active and listening.")
  32. while true do
  33.   local eventData = { os.pullEvent("lan_message") }
  34.   if textutils.unserialize(eventData[5])[1] == 0 then
  35.     print("[INFO] " .. textutils.formatTime(os.time()) .. " - A deposit session was requested by terminal and accepted.")
  36.     lan.send(textutils.serialize({1}))
  37.     local addedIngots = 0
  38.     repeat
  39.       eventData = { os.pullEvent() }
  40.       if eventData[1] == "turtle_inventory" and turtle.compareTo(eventData[2]) then
  41.         addedIngots = addedIngots + 1
  42.       end
  43.     until eventData[1] == "lan_message" and textutils.unserialize(eventData[5])[1] == 3
  44.     lan.send(textutils.serialize({4,addedIngots}))
  45.     print("[INFO] " .. textutils.formatTime(os.time()) .. " - The deposit session was ended and the final value sent to the terminal.")
  46.   end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement