Advertisement
BombBloke

Barrel Server (OpenPeripheral)

Jan 24th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. -- Barrel Tracking
  2. -- ----------------------------------------------------------
  3.  
  4. -- If connected to a specified barrel, returns its item count.
  5.  
  6. -- ----------------------------------------------------------
  7.  
  8. -- Initialise important values:
  9.  
  10. local barrel, unknownBarrel, tempSlot = {}, {}
  11. do
  12.     local tempTable = peripheral.getNames()
  13.     while #tempTable < 68 do
  14.         print("I'm missing "..(68-#tempTable).." peripherals, will check again soon...")
  15.         sleep(2)
  16.         tempTable = peripheral.getNames()
  17.     end
  18.    
  19.     local yieldTimer = os.clock() + 5
  20.     for i=1,68 do
  21.         if tempTable[i]:sub(1,6) == "barrel" then
  22.             local tempPeri = peripheral.wrap(tempTable[i])
  23.             local tempSlot = tempPeri.getStackInSlot(1)
  24.  
  25.             if tempSlot then
  26.                 barrel[tempSlot.name.." ("..tempSlot.id..":"..tempSlot.dmg..")"] = tempPeri
  27.             else
  28.                 unknownBarrel[#unknownBarrel+1] = tempTable[i]
  29.             end
  30.         end
  31.        
  32.         if os.clock() > yieldTimer then
  33.             sleep(1)
  34.             yieldTimer = os.clock() + 5
  35.         end
  36.     end
  37. end
  38.  
  39. rednet.open(peripheral.getType("right")=="modem" and "right" or "left")
  40.  
  41. term.clear()
  42. term.setCursorPos(1,1)
  43.  
  44. print("I'm an inventory server.")
  45. print("Other computers talk to me over rednet.")
  46. print("")
  47. print("Go find something else to play with and leave me alone!")
  48. print("")
  49.  
  50. while true do
  51.     sender, incoming = rednet.receive()
  52.    
  53.     if incoming == "Hello InvServer" then
  54.         -- A new client wants to know my ID.
  55.         sleep(1) -- Give the client time to switch to receiving mode.
  56.         rednet.send(sender,"Hello, InvServerClient!")
  57.     elseif not barrel[incoming] then for i=1,#unknownBarrel do
  58.         if not unknownBarrel[i] then break end
  59.         tempSlot = peripheral.call(unknownBarrel[i],"getStackInSlot",1)
  60.         if tempSlot then
  61.             barrel[tempSlot.name.." ("..tempSlot.id..":"..tempSlot.dmg..")"] = peripheral.wrap(unknownBarrel[i])
  62.             table.remove(unknownBarrel,i)
  63.         end
  64.     end end
  65.    
  66.     if barrel[incoming] then
  67.         -- Client is asking about a barrel.
  68.         sleep(1) -- Give the client time to switch to receiving mode.
  69.        
  70.         if barrel[incoming].getStackInSlot(2) then
  71.             if barrel[incoming].getStackInSlot(1).qty > 0 then
  72.                 rednet.send(sender,barrel[incoming].getStackInSlot(2).qty * 63 + barrel[incoming].getStackInSlot(1).qty)
  73.             else
  74.                 rednet.send(sender,barrel[incoming].getStackInSlot(2).qty)
  75.             end
  76.         else
  77.             rednet.send(sender,0)
  78.         end
  79.     end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement