Advertisement
CelticCoder

depositRecieve

Feb 9th, 2024 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. function deposit(atmID, blocklist, valuelist)
  2.   total = 0
  3.   while true do
  4.     senderID, message, protocol = rednet.receive(1)
  5.     if senderID == atmID and message == "stop" then
  6.       print("Deposit Stopping")
  7.       break
  8.     end
  9.     if turtle.suckUp() then
  10.       local itemDetail = turtle.getItemDetail()
  11.       for index, blockName in ipairs(blocklist) do
  12.         if blockName == itemDetail.name then
  13.           total += valuelist[index] * turtle.getItemCount()
  14.           rednet.send(atmID, total)
  15.           rednet.send(serverID, total)
  16.           turtle.drop()
  17.         end
  18.       end
  19.       turtle.dropDown()
  20.     end
  21.   end
  22.   rednet.send(serverID, "stop")
  23. end
  24.  
  25. -- Open a rednet connection
  26. rednet.open("right")
  27.  
  28. serverID = 6
  29. atmID = 0
  30.  
  31. -- Listen for messages
  32. while true do
  33.   local senderID, message, protocol = rednet.receive()
  34.   print("Received")
  35.  
  36.   -- Check if the message is for this turtle
  37.   if senderID == serverID then
  38.     -- Deserialize the received message into a table containing blockList and valueList
  39.     local data = textutils.unserialize(message)
  40.    
  41.     -- Extract blockList and valueList from the received data
  42.     local blockList = data.blockList
  43.     local valueList = data.valueList
  44.    
  45.     -- Do something with the block list and value list
  46.     for i, block in ipairs(blockList) do
  47.       local value = valueList[i]
  48.       print("Received block: " .. block .. ", Value: " .. value)
  49.     end
  50.    
  51.     -- Stop listening for more messages
  52.     break
  53.   end
  54. end
  55.  
  56.  
  57. -- Listen for messages
  58. while true do
  59.   local senderID, message, protocol = rednet.receive()
  60.   print("Received")
  61.   -- Check if the message is for this turtle
  62.   if senderID == atmID then
  63.     if message ~= "deposit" then
  64.       print("Depositing")
  65.       deposit(atmID, blockList, valueList)
  66.     end
  67.   end
  68.   if senderID == serverID then
  69.     print("Updating")
  70.     -- Deserialize the received message into a table containing blockList and valueList
  71.     local data = textutils.unserialize(message)
  72.    
  73.     -- Extract blockList and valueList from the received data
  74.     local blockList = data.blockList
  75.     local valueList = data.valueList
  76.    
  77.     -- Do something with the block list and value list
  78.     for i, block in ipairs(blockList) do
  79.       local value = valueList[i]
  80.       print("Received block: " .. block .. ", Value: " .. value)
  81.     end
  82.   end
  83. end
  84.  
  85. -- Close the rednet connection
  86. rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement